1
votes

I have a problem moving the objects continuously while pressing a key. As far as I know, pygame events only trigger when receiving a new signal and that the key pressed is not a signal that continues with every frame. From the code below, when I press w for example, the object only moves when pressing and when releasing the key. I can't achieve the object to move as long as I keep pressing the key. I've also tried out the if statements by checking event.key instead of checking the get_pressed() list, but I came up with the same result.

while not crashed:
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        crashed = True

    keys = pygame.key.get_pressed()
    print(keys[pygame.K_w])

    if keys[pygame.K_w] and keys[pygame.K_d]:
        x_change = 3
        x += x_change
        y_change = -3
        y += y_change
    elif keys[pygame.K_w] and keys[pygame.K_a]:
        x_change = -3
        x += x_change
        y_change = -3
        y += y_change
    elif keys[pygame.K_s] and keys[pygame.K_d]:
        x_change = 3
        x += x_change
        y_change = 3
        y += y_change
    elif keys[pygame.K_s] and keys[pygame.K_a]:
        x_change = -3
        x += x_change
        y_change = 3
        y += y_change

    elif keys[pygame.K_a]:
        x_change = -3
        x += x_change
    elif keys[pygame.K_d]:
        x_change = 3
        x += x_change
    elif keys[pygame.K_w]:
        y_change = -3
        y += y_change
    elif keys[pygame.K_s]:
        y_change = 3
        y += y_change


    #ERASE OLD
    screen.fill(WHITE)
    #FILL NEW
    all_sprites_list.draw(screen)
    wall.changePosition(x,y)
    player.draw(start_x,start_y)

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

1 Answers

1
votes

It looks like the line keys = pygame.key.get_pressed() and the following lines are in your event loop, and that means they are only executed once per event in the event queue. They should actually be in the outer while loop, so just dedent these lines:

while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

    keys = pygame.key.get_pressed()

    if keys[pygame.K_w] and keys[pygame.K_d]:
        x_change = 3
    # etc.

You can also shorten your code quite a bit:

x_change = 3
y_change = 3

crashed = False
while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

    keys = pygame.key.get_pressed()

    if keys[pygame.K_a]:
        x += -x_change
    elif keys[pygame.K_d]:
        x += x_change

    if keys[pygame.K_w]:
        y += -y_change
    elif keys[pygame.K_s]:
        y += y_change

Another alternative would be to remove the key.get_pressed lines and just set the x_change and y_change in the event loop:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        crashed = True
    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_d:
            x_change = 3
        # etc.
    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_d and x_change > 0:
            x_change = 0
        # etc.