是我还是pygame.key.get_pressed()无法正常工作? [英] Is it me or is pygame.key.get_pressed() not working?

查看:420
本文介绍了是我还是pygame.key.get_pressed()无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我正在做一个基本的太空飞船游戏.

okay, so I am making a basic space-ship game.

我无法旋转,因为它会打乱位图,但这是另一个问题.我什至还要使用gif吗?还有其他文件类型建议吗?

I can't get rotation to work because it scrambles the bitmap, but that's for another question.Should I even use a gif? any other filetype suggestions?

回到这里的实际点,所以:

back to the actual point here, so:

k = pygame.key.get_pressed()

是的,自我解释.这是行不通的,因为它会返回按下的每个键.

yeah, self explanatory. this doesn't work, as it returns each key as pressed.

所以,在其他地方:

d = k[pygame.K_d]

和另一行:

print d

和另一个:

if d:

因此,按下键盘上的每个键都会返回k.

So, k returns as each key on the keyboard pressed.

d无限期地返回0,无论是否按下d.

d returns 0 indefinitely, whether or not d is pressed.

d始终为0.

因此,关于d的陈述永远是不正确的.

the statement about d therefore is never true.

为什么会这样?

推荐答案

You might be confused by what get_pressed() is actually doing. From the docs:

返回一个 布尔值序列,表示每个键的状态 键盘.使用键常量值为数组建立索引.真实价值 表示按下了该按钮.

Returns a sequence of boolean values representing the state of every key on the keyboard. Use the key constant values to index the array. A True value means the that button is pressed.

使用此功能获取按钮列表不是 处理来自用户的文本输入的正确方法.你没办法知道 按键的顺序和快速按下的按键可以完全 在两次调用pygame.key.get_pressed()之间未被注意到.也有 无法将这些按键转换为完全翻译的方法 字符值.请参阅事件队列上的pygame.KEYDOWN事件以获取 此功能.

Getting the list of pushed buttons with this function is not the proper way to handle text entry from the user. You have no way to know the order of keys pressed, and rapidly pushed keys can be completely unnoticed between two calls to pygame.key.get_pressed(). There is also no way to translate these pushed keys into a fully translated character value. See the pygame.KEYDOWN events on the event queue for this functionality.

换句话说,当您调用get_pressed()时,您将得到在调用get_pressed()时键盘状态的表示形式.

In other words, when you call get_pressed(), you are getting a representation of the state of the keyboard at the time of get_pressed() being called.

例如,假设您进入游戏的一秒钟称为get_pressed().您将获得一个结构,该结构列出了键盘上的所有键以及是否按了它们(所有键均为假).

For example, let's say one second into your game you call get_pressed(). You'll get back a structure that lists all of the keys on the keyboard and if they are pressed (they will all be false).

游戏开始两秒钟后,您按下一个键.如果您查看的结构与之前的结构相同,则仍会表示未按下所有内容,因为您仍在查看键盘的状态,就像之前一样.但是,如果再次调用get_pressed(),则会返回一个新的,更新的结构,并且该新结构应显示已按下键.

At two seconds into your game, you press a key. If you look at the same structure that you were looking at earlier, it will STILL say that everything is not pressed, because you're still looking at the state of the keyboard as it was a second ago. However, if you called get_pressed() again, you'd get back a new, updated structure, and this new structure should show that the key is pressed.

解决此问题的一种方法是执行以下操作:

One way to solve this would be to do the following:

while True:
    # Update Stuff
    # Draw Stuff
    state = pygame.key.get_pressed()
    # Now check the keys

现在,您正在通过键盘获取最新信息.

Now, you're getting up-to-date information on the keyboard.

应注意的一件事是,使用上述功能,您可能仍然会错过键盘按键的操作.如果更新功能花费了很长时间,则可能会在很短的时间内按下某个键然后将其按下,以至于您在按下该键时不会调用get_pressed().

One thing that should be noted is that using the functionality above, you could STILL potentially miss a keyboard press. If the update functionality took a long time, a key could potentially be pressed then unpressed in a small enough time that you wouldn't have called get_pressed() when the key was down.

如果这可能是一个问题,则您可能需要使用事件循环.像...

If this might be a problem, you will probably want to use the event loop instead. Something like...

is_moving = False

while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN and event.key == pygame.K_d:
            is_moving = True
        elif event.type == pygame.KEYUP and event.key == pygame.K_d:
            is_moving = False

这篇关于是我还是pygame.key.get_pressed()无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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