按下键时如何退出while循环?

How to exit while loop when key is pressed?

你能告诉我如何在使用 pygame 按下一个键时退出 while 循环而不退出整个程序吗?提前致谢。 我认为问题出在下面的代码中:

 while True:
    for x in pygame.event.get():
        if x.type == KEYDOWN and x.key == K_ESCAPE:
            pygame.quit()
            sys.exit()

        if x.type == KEYDOWN and x.key == K_UP:
            if playerY > 0:
                playerSpeedY = playerFlyingSpeed
                playerFlying = True
                GAME_SOUNDS["fly"].play()
        if x.key == pygame.K_p:
                pause()

def pause(): 
loop = True
while loop: 
    for x in pygame.event.get(): 
        if x.type == pygame.QUIT:
            loop = False
        if x.type == pygame.KEYDOWN: 
            if x.type == pygame.K_SPACE:
                loop = False
            if x.type == pygame.K_p:
                loop = True

你的 while look 需要在主循环里面,你按某个按钮进入它,并以同样的方式退出它。 您可以像这样对游戏的每个屏幕执行此操作:

while True:
    running = True
    while running:
        # Your main loop

    while pause:
        # Your pause menu

这里是我如何用我的一个项目做到这一点的例子:

def main():

    # Initialize the pygame modules
    pygame.init()

    # General display settings
    pygame.display.set_caption("McGyver escape")
    SCREEN = pygame.display.set_mode((32*15, 32*16))
    # Framerate control
    clock = pygame.time.Clock()
    FPS = 60

    running = True

    # Main game loop
    while running:

        # Initialize the game
        game = Game()

        while game.main_game:
            clock.tick(FPS)

            Player.interaction_check(game)
            interface.screen_update(clock, FPS, SCREEN, game)

            # This loops through all events of the game
            for event in pygame.event.get():

                # Check if we are quitting the game
                if event.type == pygame.QUIT:
                    running = False
                    game.main_game = False
                    pygame.quit()

                # Placed here, holding the key will not produce
                # several movements.
                if event.type == pygame.KEYDOWN:
                    Player.player_movements(event, game)

        # Lose screen enabled when you encounter a guard and don't have all
        # items
        while game.lose_screen:

            interface.render_lose_screen(clock, SCREEN, FPS)

            for event in pygame.event.get():

                # Check if we are quitting the game
                if event.type == pygame.QUIT:
                    running = False
                    game.lose_screen = False

                if event.type == pygame.KEYDOWN:
                    running = False
                    game.lose_screen = False

        # Win screen enabled when getting to the exit
        while game.win_screen:

            interface.render_win_screen(clock, SCREEN, FPS)

            for event in pygame.event.get():

                # Check if we are quitting the game
                if event.type == pygame.QUIT:
                    running = False
                    game.win_screen = False

                if event.type == pygame.KEYDOWN:
                    running = False
                    game.win_screen = False

        # Exit the game
        running = False