0 XP
Chapter 8

Player Movement

Read keyboard input and move a character around the screen.

Core Concept

pygame.key.get_pressed() reads which keys are held down each frame.

Theory

keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: x -= speed if keys[pygame.K_RIGHT]: x += speed Key constants: K_LEFT, K_RIGHT, K_UP, K_DOWN, K_SPACE, K_RETURN, K_ESCAPE, K_a, K_w, K_s, K_d

Live Demo — Run & Explore

Try it live
Loading...

Output will appear here…

Exercise 1 / 1

📝 Add a speed boost — when the player holds SHIFT, move at double the normal speed.

Your solution
Loading...

Output will appear here…

Drawing Shapes
Sprites & Images