如何在 Pygame 中按住“键"? [英] How to hold a 'key down' in Pygame?
问题描述
我使用 Pygame 1.9.6 和 Python 3.7.4.我想按住空格键,它会一遍又一遍地连续执行相同的操作.我知道如何使用 KEYDOWN
按下按钮.我看了一下问题:如何在 Pygame 中有效地持有密钥? 的答案,但无法理解一个答案:
I use Pygame 1.9.6 and Python 3.7.4. I want to hold down the space bar and it continuously does the same action over and over. I know how to have have the button get pressed with KEYDOWN
. I looked at the question: How to efficiently hold a key in Pygame? for answers but can't understand the one answer:
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
我没有得到 key.get_pressed()
.它不是来自 Pygame.另外,我假设这是他们编写的函数,但是当我按住键"时,它不会显示我,它会继续运行该操作,而当键"被释放时,它会停止正在调用的操作.关于如何实际按住按钮或如何制作按钮的任何指示?
I don't get the key.get_pressed()
. It's not from Pygame. Also, I'm assuming it's a function they wrote, but this doesn't show me on when I hold down a 'Key' it continues to run that action and when the 'Key' is released it stops the action being called. Any pointers on how to actually hold down a button or how to make one?
推荐答案
pygame.key.get_pressed()
是一个函数形式 pygame.key
模块.它返回一个布尔值列表,表示键盘上每个键的状态.
pygame.key.get_pressed()
is a function form pygame.key
module. It returns a list of boolean values representing the state of every key on the keyboard.
如果你想测试 SPACE 键是否被按下,你必须通过订阅获取 K_SPACE
的状态:
If you want to test if the SPACE key is pressed the you have to get the state of K_SPACE
by subscription:
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
# [...]
这篇关于如何在 Pygame 中按住“键"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!