The game loop: a game's heartbeat
A game looks alive because it redraws itself many times a second. That repeating cycle — clear, update, draw — is the game loop. It's the heartbeat of every game.
The big idea
The game loop repeats three steps every frame: clear the stage, update where things are, then draw them.
See it in code
A loop's job is to repeat. Here the same three steps — clear, update, draw — run three times in a row. That repeat is the whole idea:
for frame in range(3):
print("clear")
print("update")
print("draw")clear update draw clear update draw clear update draw
Three trips around the loop, three sets of steps. A game does this too — the same steps, again and again.
A game loop also counts how many times it runs. Each trip we add 1 to frames, and the loop keeps going until frames reaches 5:
frames = 0
while frames < 5:
frames = frames + 1
print("Frame", frames)
print("The loop ran", frames, "frames")Frame 1 Frame 2 Frame 3 Frame 4 Frame 5 The loop ran 5 frames
The loop ran until frames reached 5, then stopped. A real game loop runs like this — just way more times, and it moves things too.
Now the real thing. Each time around the loop we do three things: Stage.clear wipes the last picture, move_down updates the position a little, and draw paints the new one. Over and over, that slides the enemy down the screen:
from game import Stage, Enemy
screen = Stage.create()
enemy = Enemy(240, 60)
frames = 0
while not enemy.at_bottom():
Stage.clear(screen) # 1. clear the stage
enemy.move_down() # 2. update positions
enemy.draw(screen) # 3. draw everything
frames += 1
print("The loop ran", frames, "frames")The loop ran 500 frames

It took 500 trips around the loop to bring the enemy down. In a real game the loop keeps going with while running:. It does clear, update, draw many times a second. That's why everything looks so smooth.
Every game has this beating heart: clear, update, draw, repeat. It's how a phone game and your game both keep the action going. Change what happens in update and draw, and you change the whole game.
Try it yourself
Give the enemy enemy.speed = 3 before the loop, and watch the frame count drop — bigger steps mean fewer frames. Then add a second enemy and move and draw it inside the same loop.
The common mistake
Forgetting to Stage.clear each frame. Skip it, and the old pictures stay on screen, so the enemy leaves a smeary trail instead of moving cleanly. Clear first, then draw fresh.
What it unlocks
The loop is powered by while loops and drives frames and motion, sprites, and game state.