Recommended Posts

I have just started working with pygame and wrote a simple program that loads a bmp blits it to a surface and then flips the surface. It also has an event handler to print and events other than QUIT (like mouse movements, key presses etc.)
Everything works fine except for the event handler. It correctly prints any events but does not handle QUIT correctly. If I click the close box on the window I get the following error:

This method works, of course, but it introduces an object with no ownership semantics, and thus no access controls - the global flag, running. A second approach avoids this pitfall, but complicates your main loop slightly:

This method works, but now you're going to be instrumenting your main loop to handle all the various exceptions you may raise within your code for non-error purposes. Ugh. Plus, the exception mechanism is being used as a secondary event queue... hey, wait a minute!

You can make this more robust by using another user-created event type as the loop sentinel, so that your application can respond to a QUIT event, perform some processing, then post your own loop exit event to the queue:

You can do a lot of very powerful things with the PyGame event system. You can maintain rock-solid frame updates and game heartbeat by generating corresponding events using a timer:

pygame.time.set_timer(NULL_EVENT, 1000.0/30) # generate a NULL_EVENT 30 times a second

By using pygame.event.set_blocked, pygame.event.set_allowed and pygame.event.get, you can event priority sort your events, such that screen refreshes, etc, are reliably handled on time. I'm looking into building a preemptive layer on top of this foundation, and I'll share what I find.

0

Share this post

Link to post

Share on other sites

I'm running into another problem with it. It seems that sometimes the program will display my events like mouse movements, button presses etc. However sometimes it doesn't register any input and goes into (not responding) and crashes. If I run from the command line the python command line crashes too. If I run from the IDE it does not crash.

0

Share this post

Link to post

Share on other sites

Thanks guys. I found I had an indent error and was never actually calling my event handler due to it. Amazingly the backspace key was the answer. Not used to thinking about things the Python way yet, was sure there was some not so obvious C++ like window setup problem.