python - 按住键盘而不是多次按下
问题描述
我在 pygame 中进行了“wasd”运动测试,每个人都使用键盘控件进行运动,如果你按住它继续移动直到pygame.KEYUP
,但我需要多次按下才能移动到我想要的位置(而不是像按住在我的世界中),代码:
class Car(pygame.sprite.Sprite):
def __init__(self, x,y):
super(Car, self).__init__()
...
self.rect.x = 0
self.rect.y = 0
def move(self, x,y):
self.rect.x += x
self.rect.y += y
键盘按键检测:
if event.key == pygame.K_w:
# car = Car(x, y), steps = 3
car.move(steps, 0)
# Same^ to the 'a,s,d' key
我现在正在做的是多次按下键盘而不是按住(我想做什么),我怎么做?
解决方案
我得到了解决方案:
pr = 0
while True:
keys = key.get_pressed()
if keys[pygame.K_w]:
pr += 1
print(f"You Press w {pr} times")
推荐阅读
- openssl - 如何在openssl中使用rc4加密公钥?
- javascript - getTime() 在 Safari 上的工作方式与在 Chrome 上的工作方式不同
- flutter - Flutter:在保持纵横比的同时缩小图像以适应行
- java - 如何使用 LatLang 的 ArrayList 填充标记的 ArrayList
- javascript - 猫头鹰旋转木马堆叠物品 - 有多少物品的大小限制?
- css - Jekyll CSS 只为 Index.html 加载
- css - 复杂的 Flex 框布局
- java - Eclipse 无法识别 JavaFX
- racket - 如何比较球拍中的语法对象?
- logic - 比较多个集合的基数并从最大集合的成员中获取特定值