点击“Enter”重启

时间:2014-07-09 19:04:51

标签: python pygame

我不确定为什么我的代码在python上重启我的游戏(使用pygame)不起作用? 完成我的主要功能后(当屏幕上没有更多项目赢得游戏,即空的“熊”数组)时,我将startGame变量更改为“False”: 即

 if len(bears) == 0:
    startGame = False

然后写完后:

   #draw the window
    pygame.display.update()
    mainClock.tick(40)


play_again()
pygame.display.quit()
pygame.quit()

play_again()函数是我尝试定义“重启”按钮。它的代码是:

def play_again():
    windowSurface.blit(text, (90, 104))
    windowSurface.blit(restart, (80,180))
    clock = pygame.time.Clock()
    pygame.display.flip()
    in_main_menu = True
    while in_main_menu:
        clock.tick(50)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                in_main_menu = False
                pygame.display.quit()
                pygame.quit()
            if event.type == KEYDOWN:
                if event.key == K_RETURN:
                    global startGame
                    startGame = True
                    in_main_menu = False

我不确定为什么这种方法不起作用。 (如果有人想知道,我之前已经定义了“文本”和“重新启动”变量并且它们很好)

1 个答案:

答案 0 :(得分:-1)

至于我,你离开主循环,打电话给play_again(),但你在外面循环,所以你可以回到开始代码。

你需要另一个外部循环(在play_again()之后)返回到代码的开头。

可能与此类似:

while StartGame:

    while game:

            # game

    #StartGame = play_again()
    play_again()

pygame.display.quit()
pygame.quit()

请记住:如果您遇到问题,请添加一些print一些信息,以查看代码中的内容 - ("I'm in loop""I leaves loop""variable StarGame is:",等)

相关问题