0
import pygame
from sys import exit

x = 125
y = 125

width = 20
height = 20

vel = 10

win = pygame.display.set_mode((500, 500))

key_press = pygame.key.get_pressed()


pygame.init()
screen = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()


        if key_press == [pygame.K_UP] and y>0:

            y += vel


        pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))

        pygame.display.update()
        clock.tick(60)

Any reason why it's not working? I do not understand, it should just change the variable of the block when the up arrow is pressed yet, it stays still once the program is opened

  • It changes `y` when `W` is pressed, not up arrow. – Thomas May 28 '22 at 16:38
  • Changed it and still does not work, – EverythingDoesn'tMakeSense May 28 '22 at 16:42
  • 1
    Do`key_press = pygame.key.get_pressed()` `if key_press[pygame.K_UP]:` in the application loop (not in the event loop and not before the application loop). See [How can I make a sprite move when key is held down](https://stackoverflow.com/questions/9961563/how-can-i-make-a-sprite-move-when-key-is-held-down)* – Rabbid76 May 28 '22 at 16:48

0 Answers0