Sign in to follow this  
ares623

[Python/Pygame] how to delay the game when receiving an event?

Recommended Posts

I doubt you actually want to delay the main loop.

I assume you wish to implement something like pause, right? It depends on the current structure of your main loop, but assuming you have something like the following:
running = True
...
while running:
for evt in pygame.event.get():
# event handling
...
# execute main loop

then it can be as simple as adding a new flag that determines whether to run the loop:
running = True
paused = False
...
while running:
for evt in pygame.event.get():
if evt.type == pygame.KEYDOWN:
if evt.key == pygame.P:
paused = not paused
...
if not paused:
# execute main loop

Essentially, you add a new boolean flag that is toggled by the "delaying" event, and then you test that flag before executing your main loop iteration.

Share this post


Link to post
Share on other sites
I'm confused.. if I use a flag, how does the game continue?
because the main loop depends on the flag, and if I pause it, and the main loop stops, how do I unpause it if I can't check for the KEYDOWN event?

Share this post


Link to post
Share on other sites
You don't need to put your key event checking in the same function as your overall game updates. Oluseyi demonstrates this in his example above. You still perform a loop that checks the events, just not updating the game each time if it's paused.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kylotan
You don't need to put your key event checking in the same function as your overall game updates. Oluseyi demonstrates this in his example above. You still perform a loop that checks the events, just not updating the game each time if it's paused.

Indeed. Examine the example closely: operation of the overall loop, including event checking, is controlled by running (such that the program exits once running is false), but only the game logic update portion would be controlled by paused, since the portion that iterates over the event queue is outside of that if statement's body.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this