Possible Duplicate:
Polling vs event driven input

I'm trying to teach myself a bit of game development using Pygame, and I'm unsure what's the best way to approach some of the fundamental parts of a game. For example, I've made a tiny 2D game where a sprite can move around the screen, seen from the top like a classic RPG. I'm using an event loop, and whenever I detect the right arrow key being pressed, I start the character walking right, and whenever I detect the key being released, it stops walking right, etc.

My question is if this is the best way to do it, or if it is better to check the state of the key at each step, and move right if the key is down, etc. I originally thought that it would be best to listen for events, as the player might conceivably press and release a key between two timesteps, thus making the game miss the event if it only checks the state of the key at each timestep.

I imagine for things like attack, which would start at or soon after the press of a key, and then last for a certain amount of time regardless of the state of the key, it would be best to listen for events. For things like movement, however, which typically lasts for as long as the key is pressed, it might be better to check the state of the key.

Any thoughts? Also, any links to a tutorial or textbook on game design which discusses these kinds of low level decisions would be great.


Browse other questions tagged or ask your own question.