首页 > 解决方案 > 按住键盘而不是多次按下

问题描述

我在 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

我现在正在做的是多次按下键盘而不是按住(我想做什么),我怎么做?

标签: pythonpygamecontrols

解决方案


我得到了解决方案:

pr = 0

while True:
    keys = key.get_pressed()
    if keys[pygame.K_w]:
        pr += 1
        print(f"You Press w {pr} times")


推荐阅读