如何有效地把握Pygame的关键? [英] How to efficiently hold a key in Pygame?

查看:143
本文介绍了如何有效地把握Pygame的关键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将解决​​方案发布到了那里. [/EDIT]

I've posted the solution down there. [/EDIT]

我发现了两个相关的问题:

I've found two related questions:

  • Pygame hold key down causes an infinite loop
  • pygame - on hold button down

但是我想具体一点.怎么做?

But I want to be specific. How to?

while not done:
    for e in event.get():
        if e.type == KEYDOWN:
            keys = key.get_pressed()
            if e.type == QUIT or keys[K_ESCAPE]:
                done = True
            if keys[K_DOWN]:
                print "DOWN"

当我按下向下箭头时,它会打印,但是只打印一次.如果要再次打印,则需要再次按.

When I press the down arrow, it prints, but it prints just once. If I want to print it another time, I need to press it again.

如果我改用while关键字,

If I use the while keyword instead,

while keys[K_DOWN]:
    print "DOWN"

由于某种晦涩的原因,我陷入了无限循环.

I get an infinite loop for some obscure reason.

这种逻辑选择也没有用:

This logical alternative is also useless:

if ((e.type == KEYDOWN) and keys[K_DOWN]):
    print "DOWN"

还有另一种可以某种方式清除事件的消息,您可以在以下时间使用:

And there is this other one that somehow cleans the events and you can use while:

while not done:
    for e in event.get():
        if e.type == KEYDOWN:
            keys = key.get_pressed()
            if e.type == QUIT or keys[K_ESCAPE]:
                done = True
            while keys[K_DOWN]:
                print "DOWN"
                event.get()
                keys = key.get_pressed()

但是您按下向下键的时间不到一秒钟,它可以打印数千次. (移动一个球员是不可能的,为此调整时钟似乎并不是解决它的正确方法(而且我已经尝试过,但我失败了.).

But you press the down key for less than one second and it prints thousands of times. (Moving a player would be impossible, and adjusting clock for this does not seem to be the right way to deal with it (And I've tried and I've failed miserably.)).

数千次按下并执行该块是没有用的.我想要的是在定义的游戏时钟速度内按下键并在不释放时继续进行操作.

To press and execute the block thousands of times is useless. What I want, is to press the key and keep going with the action while I don't release it, within the defined game clock speed.

推荐答案

请勿混淆event.get()key.get_pressed().

如果您按下或释放一个键,并且事件被放入事件队列中,您可以使用event.get()进行查询.如果您实际上对按键是被物理按下还是释放(实际上是键盘事件)感兴趣,请执行此操作.请注意,根据按键重复设置,将KEYDOWN get添加了多个时间到队列中.

If you press or release a key, and event is put into the event queue, which you query with event.get(). Do this if you're actually interested if a key was pressed down physically or released (these are the actual keyboard events. Note that KEYDOWN get's added multiple time to the queue depending on the key-repeat settings).

此外,在处理KEYDOWN事件时无需查询所有键的状态,因为您已经通过检查event.key

Also, there's no need to query the state of all keys while handling a KEYDOWN event, since you already know which key is pressed down by checking event.key

如果您对是否按住某个键(并忽略您可能想要的重复键)感兴趣,那么您应该简单地使用key.get_pressed().不必使用一堆标志,这会使您的代码混乱.

If you're interested in if a key is hold down (and ignoring the key-repeat, which you probably want), then you should simply use key.get_pressed(). Using a bunch of flags is just unnecessary and will clutter up your code.

因此您的代码可以简化为:

So your code could simplified to:

while not done: 
    keys = key.get_pressed() 
    if keys[K_DOWN]: 
        print "DOWN" 
    for e in event.get(): 
        pass # proceed other events. 
             # always call event.get() or event.poll() in the main loop

这篇关于如何有效地把握Pygame的关键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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