When exactly should I be using the pygame event module rather than the pygame key and mouse modules? Currently, I use key & mouse for most of my input and only use event to quit the program. So I was wondering if anyone can clarify the difference and the appropriate times to use event vs key & mouse.
Using Pygame event vs key & mouse for user input
Events are useful when you want to catch when things happen - e.g "I just pressed this key" rather than to check the state - e.g. "This key is currently being held down".
It also means you avoid certain bugs - for example, if your game is running slowly, sometimes it's possible that you can click the mouse and release it so fast that your game never notices that the mouse was ever down, i.e. pygame.mouse.get_pressed() might return false when you check it, even though you clicked the button since the last time you called it. The event system won't do that - the mouse click event will be there, waiting for you to handle it.
pygame.event.pump()
keyboard = pygame.key.get_pressed()
if keyboard[pygame.K_UP]:
#move player...
elif keyboard[pygame.K_DOWN]:
#move player...
elif keyboard[pygame.K_LEFT]:
#move player...
elif keyboard[pygame.K_RIGHT]:
#move player...
if keyboard[pygame.K_SPACE]:
#shoot...
for event in pygame.event.get():
if event.type == pygame.QUIT:
#quit...
elif keyboard[pygame.K_i]:
#open player inventory...
Currently, I'm using key to handle continuous move events and shooting and the events queue to handle events that should not be repeated if you hold down the given key. Is this sensible to do? I tried using events for player movement but I would get input lag.
Events wouldn't give you input lag - the processing would happen at exactly the same time in each case. Perhaps you mean something slightly different when you say that. Using events for movement would need slightly different logic as you'd have to switch movement off and on, instead of performing a fixed amount of movement whenever the key is seen to be down.
Your current system is reasonable, so carry on if you're happy with it. However, you don't need to call event.pump if you're calling event.get in the same main loop.
Using events for movement would need slightly different logic as you'd have to switch movement off and on, instead of performing a fixed amount of movement whenever the key is seen to be down.
Ahh, this was my problem makes sense.
However, you don't need to call event.pump if you're calling event.get in the same main loop.