如何在 Pygame 中有效地持有密钥? [英] How to efficiently hold a key in Pygame?

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

问题描述

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

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

虽然没有完成:对于 event.get() 中的 e:如果 e.type == KEYDOWN:键 = key.get_pressed()如果 e.type == QUIT 或 keys[K_ESCAPE]:完成 = 真如果键[K_DOWN]:打印向下"

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

如果我改用 while 关键字,

while 键[K_DOWN]:打印向下"

由于某些不明原因,我得到了一个无限循环.

这个合乎逻辑的替代方案也无用:

if ((e.type == KEYDOWN) and keys[K_DOWN]):打印向下"

还有另一个以某种方式清理事件,您可以使用 while:

虽然没有完成:对于 event.get() 中的 e:如果 e.type == KEYDOWN:键 = key.get_pressed()如果 e.type == QUIT 或 keys[K_ESCAPE]:完成 = 真而键[K_DOWN]:打印向下"事件.get()键 = key.get_pressed()

但是你按下向下键不到一秒钟,它会打印数千次.(移动玩家是不可能的,为此调整时钟似乎不是处理它的正确方法(我已经尝试过,但我失败了.)).

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

解决方案

不要混淆 event.get()key.get_pressed().

<小时>

如果你按下或释放一个键,事件被放入事件队列,你用event.get()查询.如果您真的对物理按下或释放某个键感兴趣,请执行此操作(这些是实际的键盘事件.请注意,KEYDOWN get 会根据键重复设置多次添加到队列中).

此外,在处理 KEYDOWN 事件时无需查询所有键的状态,因为您已经通过检查 event.key<知道按下了哪个键/p><小时>

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

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

虽然没有完成:键 = key.get_pressed()如果键[K_DOWN]:打印向下"对于 event.get() 中的 e:pass # 继续其他事件.# 总是在主循环中调用 event.get() 或 event.poll()

I've found two related questions:

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.

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.

解决方案

Don't mix up event.get() and key.get_pressed().


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

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


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天全站免登陆