Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从一个while循环跳回到另一个while循环_Python_Loops - Fatal编程技术网

Python 从一个while循环跳回到另一个while循环

Python 从一个while循环跳回到另一个while循环,python,loops,Python,Loops,我的程序中有两个while循环。第一个用于游戏菜单,第二个用于实际游戏。如果发生gameover事件,我想返回菜单。我不知道该怎么做 # Keep window running (Infinite-Loop) running = 1 # Menu-loop while running == 1: # Start game if event.key == pygame.K_SPACE: running

我的程序中有两个while循环。第一个用于游戏菜单,第二个用于实际游戏。如果发生gameover事件,我想返回菜单。我不知道该怎么做

 # Keep window running (Infinite-Loop)
    running = 1
    
    # Menu-loop
    while running == 1:

        # Start game
        if event.key == pygame.K_SPACE:
             running = 2
    
        # Game While-Loop (Everything that takes place during the game is inside here)
        while running == 2:
    
            # Show gameover-messages when event occurs
            if gameover:
                obstacle_list.clear()
                speed_up = 0
                speed_down = 0
                show_gameover(go_textX, go_textY)
                show_gameover_message()

一种方法是使用函数而不是嵌套的while循环:

def game_loop():
    # do stuff

    while True:
        if gameover:
            # do other stuff

            return # returns back to main_loop

def main_loop():
    running = 1

    while running == 1:
        if event.key == pygame.K_SPACE:
            game_loop()

main_loop() # simply call the main_loop function to start window

设置running=1也可以,但使用函数可以使事情更干净、更有条理。

如何做到这一点,这里是:

running=1
while running == 1:
    # Do stuff as you want
   
    if event.key == pygame.K_SPACE:
         running = 2

    while running==2:
        # Do stuff for running the Game
        if event.key == 'exit': # Here you can take input any key for exit the game
            running = 1
          
设置running=1退出游戏循环。请浏览、和,查看此网站的工作方式,并帮助您改进当前和未来的问题,这可以帮助您获得更好的答案。教我这个基本的语言特性对于堆栈溢出来说是离题的。堆栈溢出不是为了替换现有的教程和文档。