0
votes
def walk():
    x = 0
    y = 0
    dist = 5
    frame = 0
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            key = pygame.key.get_pressed()
            if key[pygame.K_UP]:
                x = x - 5

This is part of some code to make my sprite move. But it tried to get it to print it while i was pressing the button for some reason it only print "yay" once until i press the key again. Someone please help.

1

1 Answers

2
votes

You have to dedent the line key = pygame.key.get_pressed(): and the two lines below. They are executed only once per event in the event queue because they are in the event loop (for event in pygame.event.get():), but they should be executed in the while loop every frame.

def walk():
    x = 0
    y = 0
    dist = 5
    frame = 0
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        key = pygame.key.get_pressed()
        if key[pygame.K_UP]:
            x = x - 5