python - 如何重新开始游戏?

标签 python pygame

我想使用按钮 Yes! 可以再次开始游戏,而不是退出程序并定期进入。如何创建?

def game_over():
    Game_over = True
    while Game_over:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
        #[...]
        button.draw(450, 500, 'Yes!')
        button.draw(720, 500, 'No!', beginning)
        pygame.display.update()
        clock.tick(FPS)
def game():
    global health 
    Game = True
    while Game:
        clock.tick(FPS)
        for event in pygame.event.get(): 
            #[...]
            if event.type == pygame.QUIT:
                beginning = False
        if health <= 0:
            game_over()
        #[...]        
        show_health()
        button.draw(20, 80, 'Pause', pause)
        pygame.display.update()
        all_sprites.update()

def beginning():
    Beginning = True
    while Beginning:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
        #[...]
        mini_button = Button2(180, 120)
        mini_button.draw(570, 200, 'PLAY', game)
        mini_button.draw(570, 420, 'QUIT', QUIT)
        pygame.display.update()      
beginning()
pygame.quit()

最佳答案

您的方法的问题在于它是递归的。 beginning 调用gamegame 调用game_over 并且game_over 再次调用beginning :

beginning
  |
   -> game
       |
        -> game_over
            |
             -> beginning
                  |
                   -> game
                        |
                         -> ...

重构您的代码并添加一个游戏状态变量和改变游戏状态的函数:

game_state = 'beginning'

def start_game():
    global game_state
    game_state = 'running'

def pause_game():
    global game_state
    game_state = 'pause'

def game_over():
    global game_state
    game_state = 'game_over'

def quit_game():
    global game_state
    game_state = 'quit'

创建一个游戏循环,根据游戏状态绘制不同的场景。按下某个键时,游戏状态会发生变化:

def main():
    global game_state
    while game_state != 'quit':
        event_list = pygame.event.get() 
        for event in event_list:
            if event.type == pygame.QUIT:
                quit_game()
  
        if game_state == 'beginning':
            mini_button = Button2(180, 120)
            mini_button.draw(570, 200, 'PLAY', start_game)
            mini_button.draw(570, 420, 'QUIT', quit_game)

        elif game_state == 'pause':
            # [...]
        
        elif game_state == 'running':
            for event in event_list:
                # [...]
            if health <= 0:
                game_over()
            #[...]        
            show_health()
            button.draw(20, 80, 'Pause', pause_game)
            all_sprites.update()

        elif game_state == 'game_over':
            button.draw(450, 500, 'Yes!', start_game)
            button.draw(720, 500, 'No!', quit_game)
        
        pygame.display.update()     
        clock.tick(FPS)

main()
pygame.quit()

而不是 global game_state 变量你可以使用 Classe使用类方法。

关于python - 如何重新开始游戏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70350824/

相关文章:

python - 我想打乱这些变量,然后将它们组合到另一个函数中以将图像打印到屏幕上

python - 为什么 pygame.event.wait 在 20 秒左右后停止音乐播放

python - 控制 Raspberry Pi 和 Python 上的 USB 网络摄像头照片捕获时间

python - 使用 PuLP 代码时出现 "int object is not callable"错误

python - 为什么我收到 IOError : [Errno 13] Permission denied?

python - Tkinter,透明背景,Linux

python - 如何使矩形的副本在屏幕上保持绘制状态并且在pygame中不透水

导入代码时删除python QT底层对象

python - tastypie api 不在 json 结果中显示 ForeignKey

python - 在另一个类中使用一个类的方法?