007 AZx
007 AZx

Reputation: 133

Pygame keys convention

I want to use w a s d keys for movement rather than arrows key But I can't figure out how to do so with the following piece of code. I want to replace pygame.KEYDOWN with a button for s but I can't figure out how to do so.

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
        Pacman.changespeed(-30, 0)
    if event.key == pygame.K_RIGHT:
        Pacman.changespeed(30, 0)
    if event.key == pygame.K_UP:
        Pacman.changespeed(0, -30)
    if event.key == pygame.K_DOWN:
        Pacman.changespeed(0, 30)

Upvotes: 1

Views: 84

Answers (1)

Rabbid76
Rabbid76

Reputation: 210909

You don't need to change the event type. w, a, s, d are represented by pygame.K_w, pygame.K_a, pygame.K_s and pygame.K_d, respectively, but the event type is still pygame.KEYDOWN:

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_d:
        Pacman.changespeed(-30, 0)
    if event.key == pygame.K_a:
        Pacman.changespeed(30, 0)
    if event.key == pygame.K_w:
        Pacman.changespeed(0, -30)
    if event.key == pygame.K_s:
        Pacman.changespeed(0, 30)

Note: KEYDOWN does not mean the "down" key, but that a key is pressed down. The KEYDOWNevent occurs when a key is pressed and the KEYUP event occurs when a key is released. See pygame.event and pygame.key.

Upvotes: 1

Related Questions