当同时按下3+键时,PyGame无法接收事件 [英] PyGame not receiving events when 3+ keys are pressed at the same time

查看:207
本文介绍了当同时按下3+键时,PyGame无法接收事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 PyGame 中开发一个简单的游戏...一艘飞来飞去的火箭飞船射击东西.

I am developing a simple game in PyGame... A rocket ship flying around and shooting stuff.

问题:为什么pygame一次也可能按下键时为什么会停止发出键盘事件?

Question: Why does pygame stop emitting keyboard events when too may keys are pressed at once?

关于密钥处理:该程序具有许多变量,例如KEYSTATE_FIRE, KEYSTATE_TURNLEFT等...

About the Key Handling: The program has a number of variables like KEYSTATE_FIRE, KEYSTATE_TURNLEFT, etc...

  1. 处理KEYDOWN事件时,会将相应的KEYSTATE_*变量设置为True.
  2. 处理KEYUP事件时,会将相同的变量设置为False.
  1. When a KEYDOWN event is handled, it sets the corresponding KEYSTATE_* variable to True.
  2. When a KEYUP event is handled, it sets the same variable to False.

问题: 如果同时按下UP-ARROWLEFT-ARROW,则在按下SPACE时pygame不会发出KEYDOWN事件.此行为因按键而异.当按下字母时,在pygame停止发出其他键的KEYDOWN事件之前,我似乎可以握住其中的5个.

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

要按以下顺序进行操作:

  • 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)
  • 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)

以下是输出:

  • <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})>
  • <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})>

这是常见问题吗?有解决方法吗?如果没有,使用pygame时处理多键控制问题的最佳方法是什么?

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?

推荐答案

这听起来像是输入问题,而不是代码问题-您确定问题不在于键盘本身吗?大多数键盘对可以同时按下的键数有限制.通常,您一次按下的键不能超过几个.

This sounds like a input problem, not a code problem - are you sure the problem isn't the keyboard itself? Most keyboards have limitations on the number of keys that can be pressed at the same time. Often times you can't press more than a few keys that are close together at a time.

要进行测试,只需开始按住键盘上的字母,然后看看何时不再出现新字母即可.

To test it out, just start pressing and holding letters on the keyboard and see when new letters stop appearing.

我的建议是尝试将SPACE映射到其他地方的另一个键,看看会发生什么.

My suggestion is to try mapping SPACE to a different key somewhere else and see what happens.

这篇关于当同时按下3+键时,PyGame无法接收事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆