【问题标题】:How do I exit a game loop?如何退出游戏循环?
【发布时间】:2020-06-12 02:11:33
【问题描述】:

如果一组条件为真,我想调用一个函数来检查它们是否为真。 如果函数确认它们是真的,我想通过在函数中设置 gameExit=True 来退出我的 pygame 游戏循环。

但是,即使条件已经满足,它也没有执行我的功能。

上下文:井字游戏,要退出游戏循环是所有顶部方块都有一个叉号。

def is_game_over(current_game,gameExit):
    if current_game[0][0]=="cross" and current_game[0][1]=="cross" and current_game[0][2]=="cross":
        gameDisplay.fill(white)
        pygame.display.flip()
        gameExit=True

while gameExit == False:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            gameExit=True


        if event.type == pygame.MOUSEBUTTONDOWN:

            placement = (place_clicked(pygame.mouse.get_pos(), "none"))  # get the co-ordinates of place they clicked

           *Below is a bunch of if statements, which call the function is_game_over if i want to exit the game loop"

【问题讨论】:

    标签: python function loops pygame


    【解决方案1】:

    is_game_over 中的 gameExit 变量引用的值与 while 循环中的 gameExit 变量不同。阅读有关 python 的参数传递以了解更多信息。

    您需要从函数中返回新值,并使用它在 while 循环中设置 gameExit 变量。

    * gameExit no longer a parameter (return True, False instead)
    def is_game_over(current_game):
        if current_game[0][0]=="cross" and current_game[0][1]=="cross" and current_game[0][2]=="cross":
            gameDisplay.fill(white)
            pygame.display.flip()
            return True
        return False
    
    * set gameExit within while loop
    gameExit = is_game_over(current_game)
    

    【讨论】:

    • 感谢您的回复!有效。除了这个调整,我还有一个逻辑错误,我说的是 current_game[0][0]=="X" ,而不是 current_game[0][0]=="cross"
    猜你喜欢
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多