Editor

Moving with the keyboard

Pygame fundamentals · Lesson 3 of 7

Keep where things are in variables, change those variables every frame, and let the keys decide by how much.

Held keys: key.get_pressed()

pygame.key.get_pressed() tells you which keys are down right now. Checking it every frame moves the square for as long as a key is held: speed pixels each frame.

max and min stop the square at the edges. The window is 480 wide and the square 40, so x may go from 0 to 440.

import pygame

pygame.init()
screen = pygame.display.set_mode((480, 320))
clock = pygame.time.Clock()
x, y = 220, 140
speed = 4

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

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        x -= speed
    if keys[pygame.K_RIGHT]:
        x += speed
    if keys[pygame.K_UP]:
        y -= speed
    if keys[pygame.K_DOWN]:
        y += speed
    x = max(0, min(x, 480 - 40))
    y = max(0, min(y, 320 - 40))

    screen.fill((16, 18, 22))
    pygame.draw.rect(screen, (255, 212, 59), (x, y, 40, 40), border_radius=6)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

Single presses: KEYDOWN events

Some actions should happen once per press, however long the key is held: jumping, firing, pausing. Those belong in the event loop, where a KEYDOWN event arrives once each time a key goes down.

Here Space changes the square's colour. Try holding it: the colour changes once, not sixty times a second.

import pygame

pygame.init()
screen = pygame.display.set_mode((480, 320))
clock = pygame.time.Clock()
colors = [(255, 212, 59), (90, 162, 224), (235, 95, 85), (110, 200, 120)]
color = 0
x = 220

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            color = (color + 1) % len(colors)  # once per press

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        x = max(0, x - 4)
    if keys[pygame.K_RIGHT]:
        x = min(440, x + 4)

    screen.fill((16, 18, 22))
    pygame.draw.rect(screen, colors[color], (x, 140, 40, 40), border_radius=6)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

Which keys have names

Every key is a constant: pygame.K_a to pygame.K_z for letters, pygame.K_0 to pygame.K_9 for digits, pygame.K_SPACE, pygame.K_RETURN, pygame.K_ESCAPE, and the arrows pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT. Many games accept both the arrows and W, A, S and D.

In the browser, click the game first: keys go to the game only while it has the keyboard, so typing in the editor never moves your player.

Your turn

Change the programs above in the game panel, then press Run.

  • Let W, A, S and D move the square as well as the arrows.
  • Make Shift a sprint key: while it is held (pygame.K_LSHIFT), the speed doubles.
  • Make Space move the square back to the middle of the screen.

Pong moves your paddle with held keys and starts with a single press. Open Pong.