如何将游戏的“菜单”代码与实际游戏代码集成在一起?

时间:2019-05-19 14:20:46

标签: python python-3.x pygame

我已经创建了一个基本游戏。现在,我想扩展基本游戏并向其中添加视频游戏菜单。我有两个程序:

  1. 基本游戏代码
  2. 菜单代码

我想将两种代码集成为一个,以便我的游戏更具功能性。我只是一个初学者,需要有关如何操作的指导。感谢您的帮助。

基本游戏代码:

            import pygame
            import random
            import sys
            pygame.init()
            w=800
            h=600
            red=(251,63,75)
            blue=(104,41,171)
            yellow=(255,255,0)
            player_size=25
            player_pos=[w/2,h-(2*player_size)]
            enemy_size=25
            enemy_pos=[random.randint(0,w-enemy_size),0]
            enemy_list=[ ]
            bg_color=(0,0,0)
            screen=pygame.display.set_mode((w,h))
            game_over=False
            speed=10
            score=0
            clock=pygame.time.Clock()
            myFont=pygame.font.SysFont("monospace",35)
            def set_level(score,speed):
                if score<10:
                    speed=5
                elif score<20:
                    speed=6
                elif score<30:
                    speed=8
                elif score<40:
                    speed=10
                elif score<50:
                    speed=13
                elif score<200:
                    speed=15
                else:
                    speed=20
                return speed    

            def drop_enemies(enemy_list):
                delay=random.random()
                if len(enemy_list)<6 and delay<0.1:
                    x_pos=random.randint(0,w-enemy_size)
                    y_pos=0
                    enemy_list.append([x_pos,y_pos])
            def draw_enemies(enemy_list):
                for enemy_pos in enemy_list:
                    pygame.draw.rect(screen,blue, 
                    (enemy_pos[0],enemy_pos[1],enemy_size,enemy_size))
            def update_enemy_pos(enemy_list,score):
                for idx,enemy_pos in enumerate(enemy_list):
                   if enemy_pos[1]>=0 and enemy_pos[1]<h:
                    enemy_pos[1]+=speed
                   else:
                       enemy_list.pop(idx)
                       score+=1
                return score    
            def detect_collision(player_pos,enemy_pos):
                p_x=player_pos[0]
                p_y=player_pos[1]
                e_x=enemy_pos[0]
                e_y=enemy_pos[1]
                if (e_x>=p_x and e_x<(p_x+player_size)) or (p_x>=e_x and p_x<(e_x+enemy_size)):
                    if (e_y>=p_y and e_y<(p_y+player_size)) or (p_y>=e_y and p_y<(e_y+enemy_size)):
                        return True
                return False
            def collision_check(enemy_list,player_pos):
                for enemy_pos in enemy_list:
                    if detect_collision(enemy_pos,player_pos):
                        return True
                return False
            while not game_over:
                for event in pygame.event.get():
                    if event.type==pygame.QUIT:
                        sys.exit()
                    if event.type==pygame.KEYDOWN:
                        x=player_pos[0]
                        y=player_pos[1]
                        if event.key==pygame.K_LEFT:
                            x-=player_size
                        elif event.key==pygame.K_UP:
                            y-=player_size
                        elif event.key==pygame.K_RIGHT:
                            x+=player_size
                        elif event.key==pygame.K_DOWN:
                            y+=player_size
                        player_pos=[x,y]


                screen.fill(bg_color)
                drop_enemies(enemy_list)
                score=update_enemy_pos(enemy_list,score)
                speed=set_level(score,speed)
                text='Your Score is:' + str(score)
                label=myFont.render(text,1,yellow)
                screen.blit(label,(w/2,h-40))
                if collision_check(enemy_list,player_pos):
                    game_over=True
                    break
                draw_enemies(enemy_list)
                pygame.draw.rect(screen,red, 
                (player_pos[0],player_pos[1],player_size,player_size))
                clock.tick(30)
                pygame.display.update()
                pygame.display.flip()

游戏菜单代码:

            import pygame
            import random
            import sys
            pygame.init()
            w=800
            h=600
            bg_color=(34,139,34)
            red=(255,0,0)
            blue=(0,0,125)
            bright_blue=(0,0,255)
            font_size=35
            b1_pos=[w/2-50,h/2]
            b1_size=[105,50]
            screen=pygame.display.set_mode((w,h))
            myFont=pygame.font.SysFont("freesansbold.tff",font_size)
            def button(b1_pos,b1_size):
                mouse_pos=pygame.mouse.get_pos()
                click=pygame.mouse.get_pressed()
                if (b1_pos[0]<mouse_pos[0]<(b1_pos[0]+b1_size[0])) and (b1_pos[1]<mouse_pos[1]<(b1_pos[1]+b1_size[1])):
                    pygame.draw.rect(screen,bright_blue,(b1_pos[0],b1_pos[1],b1_size[0],b1_size[1]))
                    if click[0]==1:
                        print("Left click")
                else:
                    pygame.draw.rect(screen,blue,(b1_pos[0],b1_pos[1],b1_size[0],b1_size[1]))
                text='START'
                label=myFont.render(text,1,red)
                screen.blit(label,(w/2-38,h/2+5)) 
            game_over=False
            while not game_over:
                for event in pygame.event.get():
                    if event.type==pygame.QUIT:
                        sys.exit()
                screen.fill(bg_color)
                button(b1_pos,b1_size)
                pygame.display.update()

1 个答案:

答案 0 :(得分:0)

创建游戏时,您需要一个名为The MainLoop的东西。每个循环中需要更新的所有代码都在此循环内。该循环永远运行直到游戏终止。选中这些video tutorialsthis

让我给你举个例子:

import pygame

#Import literals such as QUIT
from pygame.locals import *

pygame.init()

w = 800
h = 600

screen = pygame.display.set_mode((w,h))
running = True




def MenuCode():
    pass


def GameCode():
    pass


#This is the Game Loop.
while running:

    #Events are used to check if a key has
    #been pressed or a mouse button or a mouse movement.
    for event in pygame.event.get():

        #If the user pressed the x button.
        #terminate the while loop.
        if event.type == QUIT:
            running = False
            break;

    #In every frame you must clean the window with a color
    #in order to draw anythin else above it.
    screen.fill((0,0,0))

    #Here draw anything you like

    #Also somewhere here call all the code that needs to be
    #called every frame (Menu code, Game code, etc)
    MenuCode()
    GameCode()


    #Lastly you must update the display.
    #The most important thing that this does
    #is called buffer swapping, check it out.
    pygame.display.update()



#When the loop terminates, terminate pygame.
pygame.quit()

如果您想了解update()方法对double buffering所做的检查,