Python 如何退出游戏循环?
如果一组条件为真,我想调用一个函数来检查它们是否为真。 如果函数确认它们为true,我想通过使gameExit=true退出pygame gameloop 但是,即使满足条件,它也不会执行我的函数 上下文:Tic tac toe游戏,想要退出gameloop是所有顶级玩家都有一个十字架Python 如何退出游戏循环?,python,function,loops,pygame,Python,Function,Loops,Pygame,如果一组条件为真,我想调用一个函数来检查它们是否为真。 如果函数确认它们为true,我想通过使gameExit=true退出pygame gameloop 但是,即使满足条件,它也不会执行我的函数 上下文:Tic tac toe游戏,想要退出gameloop是所有顶级玩家都有一个十字架 def is_game_over(current_game,gameExit): if current_game[0][0]=="cross" and current_game[0][1]=="cross
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"
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)
谢谢你的回复!成功了。除了这个调整之外,我还有一个逻辑错误,比如说当前游戏[0][0]==“X”,而不是说当前游戏[0][0]==“交叉”