Jump to content
  • Advertisement
Sign in to follow this  
mrpeed

Using Pygame event vs key & mouse for user input

This topic is 492 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

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.

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.
 
And thanks for pointing this out.

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!