Stage 4 is where your game finally moves. You'll build a little rectangle near the bottom of the screen โ your spaceship โ and steer it left and right with the arrow keys.
Before we dive in โ can you remember from Lesson 3 (Colours)?
(255, 0, 0) make?Pure red. Full red light, no green, no blue. Each number is 0โ255: 0 means off, 255 means full brightness.
RED = (255, 0, 0) instead of using the numbers everywhere?So the code reads like a sentence. pygame.draw.rect(screen, RED, player) is instantly understandable โ pygame.draw.rect(screen, (255, 0, 0), player) is not.
โถ
โ
You're on Stage 4 of 10. Halfway up the rocket!
In pygame, a "thing" on the screen โ your spaceship, an enemy, a bullet โ is almost always a rectangle. Even when we draw a circle or an image on top, the position is tracked by a rectangle behind the scenes. So Stage 4 starts with the simplest thing in the world: a coloured rectangle.
We give the rectangle four numbers: x (left edge),
y (top edge), width, and height.
Pygame stores all four together in a thing called a Rect.
player = pygame.Rect(370, 520, 60, 40)
# โ โ โ โโ height (40 pixels tall)
# โ โ โโโโโโ width (60 pixels wide)
# โ โโโโโโโโโโโ y (top edge, 520 pixels down)
# โโโโโโโโโโโโโโโโ x (left edge, 370 pixels across)
Why 370 and 520? Our window is 800ร600. We want the spaceship near the bottom and roughly in the middle, so its left edge starts at 370 (so it's centred) and its top edge starts at 520 (so it sits about 80 pixels from the bottom). Check the Pixel Cheat Sheet if those numbers feel confusing.
Animation in games is a magic trick: nothing really "moves." Each frame (60 times a second!) we change a number, then redraw the rectangle in its new place. Do that fast enough, and your eyes see smooth motion.
For the player, the trick is:
player.x -= 5 (move 5 pixels left).player.x += 5 (move 5 pixels right).In pygame code, "ask if a key is held down" looks like this:
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.x -= 5
if keys[pygame.K_RIGHT]:
player.x += 5
Without any limits, you can keep pressing LEFT until the spaceship vanishes off the edge of the world. We don't want that. So we add a tiny check: only move left if there's room left, only move right if there's room right.
if keys[pygame.K_LEFT] and player.x > 0:
player.x -= 5
if keys[pygame.K_RIGHT] and player.x < WIDTH - 60: # 60 = the player's width
player.x += 5
Read it like a sentence: "if LEFT is held AND there's still room on the left, move left." That's it.
# Stage 1 โ Imports
import pygame
# Stage 2 โ Setup the Screen
pygame.init()
WIDTH = 800
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Galaxy Defender")
clock = pygame.time.Clock()
# Stage 3 โ Colours
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
SPACE = (0, 0, 50)
# Stage 4 โ Player โจ
player = pygame.Rect(370, 520, 60, 40)
player_speed = 5
# Game loop (preview of Stage 10)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# read the keyboard
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player.x > 0:
player.x -= player_speed
if keys[pygame.K_RIGHT] and player.x < WIDTH - 60:
player.x += player_speed
# draw everything
screen.fill(SPACE)
pygame.draw.rect(screen, GREEN, player)
pygame.display.flip()
clock.tick(60)
pygame.quit()
Run it. Hold LEFT โ the green rectangle slides left. Hold RIGHT โ it slides right. Stop pressing โ it stops. That's your spaceship. ๐
๐ฎ Want to play with more code? Scroll down to the Lesson 4 Code Library (6 movement games). Copy any program, paste it into the editor below, and press the green โถ Run button.
Type the code above into the editor, then click โถ Run.
player_speed = 5 to 15.pygame.Rect(370, 520, 60, 40) to pygame.Rect(370, 520, 120, 80). (Hint: also update the boundary check to WIDTH - 120.)y from 520 to 100. Where does it sit now?YELLOW (don't forget to define it in your colours section).Print these, keep them open in another tab, or download them to your computer.
๐ Lesson 4 Code Library โ 6 brand-new pygame mini-games about movement: The Player, Move Left/Right, Stay on Screen, Up/Down Player, Two Players, Auto-Bouncer.
๐ Lesson 4 Code Library (6 movement games) ๐ Lesson 3 Code Library ๐ Lesson 2 Code Library ๐ Lesson 1 Code Library
๐บ๏ธ The big-picture handouts โ keep these by your side for the whole course.
๐ Rocket Ladder ๐ Code Comic ๐ Pixel Cheat Sheet ๐ Code Recipe ๐ผ๏ธ 6 Parts of a Turtle Program
๐ค Want to try your own idea? Ask an AI like Claude or ChatGPT something like "Write me a short pygame program where I steer a smiley face with the arrow keys", then paste the code into the Trinket above and click Run.