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…
Ask AI Tutor
AI Tutorgemini-3.1-flash-lite-preview
AI
Hi! I'm your AI tutor for Player Movement. 🎓
Ask me anything about this lesson — what a variable is, why loops are useful, or "why is my code not working?". I'm here to help you understand, not just give you answers!
What's on your mind? 😊