I am developing a simple game in PyGame... A rocket ship flying around and shooting stuff.
Question: Why does pygame stop emitting keyboard events when too may keys are pressed at once?
About the Key Handling: The program has a number of variables like KEYSTATE_FIRE, KEYSTATE_TURNLEFT, etc...
- When a
KEYDOWNevent is handled, it sets the correspondingKEYSTATE_*variable to True. - When a
KEYUPevent is handled, it sets the same variable to False.
The problem:
If UP-ARROW and LEFT-ARROW are being pressed at the same time, pygame DOES NOT emit a KEYDOWN event when SPACE is pressed. This behavior varies depending on the keys. When pressing letters, it seems that I can hold about 5 of them before pygame stops emitting KEYDOWN events for additional keys.
Verification: In my main loop, I simply printed each event received to verify the above behavior.
The code: For reference, here is the (crude) way of handling key events at this point:
while GAME_RUNNING:
FRAME_NUMBER += 1
CLOCK.tick(FRAME_PER_SECOND)
#----------------------------------------------------------------------
# Check for events
for event in pygame.event.get():
print event
if event.type == pygame.QUIT:
raise SystemExit()
elif event.type == pygame.KEYDOWN and event.dict['key'] == pygame.K_UP:
KEYSTATE_FORWARD = True
elif event.type == pygame.KEYUP and event.dict['key'] == pygame.K_UP:
KEYSTATE_FORWARD = False
elif event.type == pygame.KEYDOWN and event.dict['key'] == pygame.K_DOWN:
KEYSTATE_BACKWARD = True
elif event.type == pygame.KEYUP and event.dict['key'] == pygame.K_DOWN:
KEYSTATE_BACKWARD = False
elif event.type == pygame.KEYDOWN and event.dict['key'] == pygame.K_LEFT:
KEYSTATE_TURNLEFT = True
elif event.type == pygame.KEYUP and event.dict['key'] == pygame.K_LEFT:
KEYSTATE_TURNLEFT = False
elif event.type == pygame.KEYDOWN and event.dict['key'] == pygame.K_RIGHT:
KEYSTATE_TURNRIGHT = True
elif event.type == pygame.KEYUP and event.dict['key'] == pygame.K_RIGHT:
KEYSTATE_TURNRIGHT = False
elif event.type == pygame.KEYDOWN and event.dict['key'] == pygame.K_SPACE:
KEYSTATE_FIRE = True
elif event.type == pygame.KEYUP and event.dict['key'] == pygame.K_SPACE:
KEYSTATE_FIRE = False
# remainder of game loop here...
For pressing this sequence:
a (down)s (down)d (down)f (down)g (down)h (down)j (down)k (down)a (up)s (up)d (up)f (up)g (up)h (up)j (up)k (up)
Here is the output:
<Event(2-KeyDown {'scancode': 30, 'key': 97, 'unicode': u'a', 'mod': 0})><Event(2-KeyDown {'scancode': 31, 'key': 115, 'unicode': u's', 'mod': 0})><Event(2-KeyDown {'scancode': 32, 'key': 100, 'unicode': u'd', 'mod': 0})><Event(2-KeyDown {'scancode': 33, 'key': 102, 'unicode': u'f', 'mod': 0})><Event(3-KeyUp {'scancode': 30, 'key': 97, 'mod': 0})><Event(3-KeyUp {'scancode': 31, 'key': 115, 'mod': 0})><Event(3-KeyUp {'scancode': 32, 'key': 100, 'mod': 0})><Event(3-KeyUp {'scancode': 33, 'key': 102, 'mod': 0})><Event(2-KeyDown {'scancode': 36, 'key': 106, 'unicode': u'j', 'mod': 0})><Event(2-KeyDown {'scancode': 37, 'key': 107, 'unicode': u'k', 'mod': 0})><Event(3-KeyUp {'scancode': 36, 'key': 106, 'mod': 0})><Event(3-KeyUp {'scancode': 37, 'key': 107, 'mod': 0})>
Is this a common issue? Is there a workaround? If not, what is the best way to handle multiple-key control issues when using pygame?