samedi 25 avril 2020

Run command while key is held in pygame

Hey guys so I want to make a system where a rectangle moves while a key is held. I tried this:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        run = False
    keystate = pygame.key.get_pressed()

    if keystate[pygame.K_RIGHT]:
        x += vel
    if keystate[pygame.K_LEFT]:
        x -= vel
    if keystate[pygame.K_UP]:
        y -= vel
    if keystate[pygame.K_DOWN]:
        y += vel
    win.fill((0,0,0))
    pygame.draw.rect(win, (255, 8, 20),( x, y, width, height))
    pygame.display.update()

but it doesn't work. It just moves 1 pixel at a time. Can anybody help?

Aucun commentaire:

Enregistrer un commentaire