Jump to content
  • Advertisement
Sign in to follow this  
ares623

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

This topic is 3358 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how do I delay the main loop when receiving an event, and then continue after receiving another event? pygame.time.wait() only delays for a set amount of time..

Share this post


Link to post
Share on other sites
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!