简体   繁体   中英

Pygame Mixing Repeated Key Presses and Singular Key Presses

In Pygame, you can set repeated (held down) Key detection with pygame.key.set_repeat().

In my case, I want to mix keys that can be held down and keys that will only be recognized once. Specifically, I want to be able to Move (by holding down key(s)), but I want to require one key press per Fire Missile.

To be clearer, I want to recognize held-down key presses for movement keys (Up, Down, Left, Right, etc.) only. Other keys, when held down, should only be recognized once.

You can keep a state tracker for the keys you only want to recognize once per press - set a flag to True when you see a KEYDOWN event, and then set it to False again when you see a matching KEYUP . Then only respond to a KEYDOWN event if the flag wasn't already True .

The other option is that you can keep a state tracker for the keys you do want to repeat and just poll that state every so often (eg every X milliseconds, see if the key is marked as 'down' and if so, do whatever should be done for that keypress).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM