1

I have problem with my code:

 for event in pygame.event.get():
        if event.type == pygame.QUIT:
            continuer = False
            pygame.quit()
        elif event.type == pygame.KEYDOWN:
            if (event.key == pygame.K_a):
                game.sound.play('zawarudo')
                time.sleep(2)
                for game.ennemy in game.all_ennemy:
                    game.ennemy.velocity = 0
                    time.sleep(5)
                    game.ennemy.velocity = 2

When I pressed the key A, the loop "for" is executed immediately, not after 2s . Also, my game freeze and control doesn't work during the sleep function (2s).

The Goal of this code is simple. When I pressed the key A, a song is played and, after 2s, the loop begin. After 5s of waiting, the loop end.

How I can make a wait function with no game freezing, and functional.

Thanks.

BerretMan
  • 95
  • 6
  • Using `time.sleep()` or [pygame.time.wait()](https://www.pygame.org/docs/ref/time.html#pygame.time.wait) will prevent the rest of your program from running which you don't want; it stops your program from doing anything else. Here's [an example](https://stackoverflow.com/a/60880122/2280890) using a timer in pygame to generate events. – import random Jul 22 '21 at 13:16

1 Answers1

0

Try using pygame.time.wait(2000), I think this is the correct way to use time sleeps in pygame. It takes the time in milliseconds, that's why it has to bee 2000.

Edgedancer
  • 127
  • 9
  • It's a good idea, but the control is always freeze / lock. I want to move my player during the pygame.time.wait(2000) – BerretMan Jul 22 '21 at 14:27