1
import pygame

pygame.init()
screen = pygame.display.set_mode((500, 500))

running = True

x = 50
y = 50

clock = pygame.time.Clock()

while running:
    
    clock.tick(60)
    
    screen.fill((0, 255, 0))
    pygame.draw.rect(screen, (255,0,0), (x, y, 30, 30))
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_d:
            x += 5
        if event.key == pygame.K_a:
            x -= 5    
        if event.key == pygame.K_s:`enter code here`
            y += 5
        if event.key == pygame.K_w:
            y -= 5    
        
    pygame.display.update()

Trying to move my second sprite with the w, a, s, d, keys, however it does not seem to be working. When I change pygame.K_d to pygame.K_RIGHT, it works well just as normal. Wondering if I've made a mistake in my code or it is an error with my keyboard. Thanks a lot.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • You need to indent the `if event.type == pygame.KEYDOWN` block so it is in the for loop. Otherwise it won't process the events. The reason it works with arrow keys is because the event variable is the last event in the event queue, which for non text input is not masked by TEXTINPUT events after KEYDOWN events. – Starbuck5 Jun 09 '21 at 00:28
  • Aight got it! Thanks mate – Jason Huang Jun 09 '21 at 08:54

1 Answers1

0

It is a matter of Indentation. The event needs to be evaluated in the event loop:

while running:
    
    clock.tick(60)
    
    screen.fill((0, 255, 0))
    pygame.draw.rect(screen, (255,0,0), (x, y, 30, 30))
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # INDENTATION
    #-->|
            
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_d:
                x += 5
            if event.key == pygame.K_a:
                x -= 5    
            if event.key == pygame.K_s:`enter code here`
                y += 5
            if event.key == pygame.K_w:
                y -= 5    
        
    pygame.display.update()

However, the keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.

If you want to achieve a continuously movement, you have to use pygame.key.get_pressed(). pygame.key.get_pressed() returns a list with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement:

while running:
    
    clock.tick(60)
    
    screen.fill((0, 255, 0))
    pygame.draw.rect(screen, (255,0,0), (x, y, 30, 30))
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_d]:
        x += 5
    if keys[pygame.K_a]:
        x -= 5    
    if keys[pygame.K_s]:
        y += 5
    if keys[pygame.K_w]
        y -= 5    
        
    pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174