暂停用户输入的while循环

时间:2014-04-11 01:18:49

标签: python pygame

使用pygame,在我用于战斗的循环中,我希望它在等待用户输入时暂停。然而,似乎正在发生的事情是它仍然在循环中运行。

以下内容位于主游戏循环中。

战斗中的同时:#fighting loop

    if player.speed > enemy.speed:
        player_turn = True
    else:
        player_turn = False

    while player_turn:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:  # looks for an exit command
                pygame.quit()  # quits pygame
                sys.exit()  # closes system

        label = myfont.render("Press space to attack or enter to wait", 1, (255, 255, 0))

        key = pygame.key.get_pressed()

        screen.fill(colour)

        screen.blit(player.bitmap, player.player_rect)
        screen.blit(enemy.bitmap, enemy.enemy_rect)
        screen.blit(label, (200, 400))

        player.player_rect.x = 100
        player.player_rect.y = 300

        enemy.enemy_rect.x = 700
        enemy.enemy_rect.y = 300

        if key[pygame.K_SPACE]:
            player.player_attack(enemy)
        elif key[pygame.K_RETURN]:
            break

        if enemy.hp <= 0:
            fighting = False

        pygame.display.flip()  # updates screen
        clock.tick(30)  # limits fps to 30

        player_turn = False

        break

    while not player_turn:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:  # looks for an exit command
                pygame.quit()  # quits pygame
                sys.exit()  # closes system

        label = myfont.render("Press space to continue", 1, (255, 255, 0))

        key = pygame.key.get_pressed()

        screen.fill(colour)

        screen.blit(player.bitmap, player.player_rect)
        screen.blit(enemy.bitmap, enemy.enemy_rect)
        screen.blit(label, (200, 400))

        player.player_rect.x = 100
        player.player_rect.y = 300

        enemy.enemy_rect.x = 700
        enemy.enemy_rect.y = 300

        if key[pygame.K_SPACE]:
            enemy.enemy_attack(player)

        pygame.display.flip()  # updates screen
        clock.tick(30)  # limits fps to 30

        player_turn = True

        break

我想要它做的是当它到达用户输入部分时,它在循环内等待输入,一旦接收到输入,它继续循环。我知道这可能是一件小事,但我一直在看着它,但却没有成功。

0 个答案:

没有答案