Pygame majority of the key inputs are not working, eg(w, a, s, d). Only the arrow keys seem to be working

Question:

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.

Asked By: Jason Huang

||

Answers:

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()
Answered By: Rabbid76
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.