0

I was making a 'wasd' movement test in pygame, Every one uses keyboard controls for movement and if you hold down it keep moving until pygame.KEYUP, But I Need To Press multiple times to just move in the spot that i want(instead of holding like in minecraft), CODE:

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

Keyboard key detect:

if event.key == pygame.K_w:
    # car = Car(x, y), steps = 3
    car.move(steps, 0)
    # Same^ to the 'a,s,d' key

what i'm doing for now is pressing the keyboard multiple times instead of holding(What i want to do), and how I do that?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
kielspiel
  • 61
  • 6
  • 1
    `keys = pygame.key.get_pressed(); if (keys[pygame.K_w]): car.move(steps, 0))` –  Aug 26 '21 at 03:16
  • @user16038533 I need a "KEYHOLD" like function not key pressed – kielspiel Aug 26 '21 at 03:42
  • @EzekielLayba you said that "_I need to press the keyboard multiple times instead of holding_", if so why do you need a "keyhold" like function. – PCM Aug 26 '21 at 03:48
  • @EzekielLayba Its called pressed but I believe it is in fact the function you are looking for. It returns true as long as the key you are checking for is being held down. –  Aug 26 '21 at 03:57

1 Answers1

0

I got the solution:

pr = 0

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

kielspiel
  • 61
  • 6