PyGame does not accept events when 3+ keys are pressed simultaneously

I am developing a simple game in PyGame ... The booster flies and shoots.


Question: Why does pygame stop issuing keyboard events when keys can be pressed at the same time?

About key manipulation: The program has a number of variables, such as KEYSTATE_FIRE, KEYSTATE_TURNLEFTetc.

  • When an event is executed KEYDOWN, it sets the variable KEYSTATE_*to True.
  • When an event is executed KEYUP, it sets the same variable to False.

Problem: If you press UP-ARROWand at the same time LEFT-ARROW, pygame will NOT emit an event KEYDOWNwhen clicked SPACE. It depends on the keys. When I press letters, it seems that I can contain about 5 of them before pygame stops highlighting KEYDOWNevents for additional keys.

Verification: In my main loop, I simply printed out each event received to verify the above behavior.

Code: For reference: here is a (rough) way to handle key events at the moment:

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...

To click 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 result:

  • <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 problem? Is there a workaround? If not, what is the best way to deal with multiple key issues when using pygame?

+3
4

, - , ? , . , .

, , .

- SPACE , .

+10

, ( , ) . , . "-", , .

( ) / .

+5

. 3 .

+2

This can be very keyboard dependent. My current keyboard without a name only supports two keys pressed at a time, often a pain in games.

+1
source

Source: https://habr.com/ru/post/1703664/


All Articles