【问题标题】:How to exit while loop when key is pressed? [closed]按下键时如何退出while循环? [关闭]
【发布时间】:2021-09-01 07:29:10
【问题描述】:

你能告诉我如何在使用 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

【问题讨论】:

  • 在 if 块中使用break
  • 我试过了,但是卡住了。
  • 你能给我们看看代码吗?
  • 请发布您的代码,以便我们为您提供帮助。
  • 您可能想参考this answer

标签: python while-loop pygame exit


【解决方案1】:

您的 while 外观需要在主循环内,您可以通过按某个按钮进入它,然后以同样的方式退出它。 您可以像这样在游戏的每个屏幕上做到这一点:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多