Build a platformer game

Chapter 3 of 1220 min

A window and the game loop

  1. Game loop
  2. Player
  3. Platforms
  4. Level
  5. Camera
  6. Coins
  7. Enemies
  8. Online

In this chapter you will:

  • open a game window
  • write the game loop, with its three steps
  • make your game run at a steady 60 frames a second

The whole program

Replace everything in main.py with this:

main.py
import pygame
 
WIDTH = 800
HEIGHT = 480
 
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pixel Run")
clock = pygame.time.Clock()
running = True
 
while running:
    # 1. Input
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
 
    # 2. Update: nothing moves yet
 
    # 3. Draw
    screen.fill((135, 206, 235))
    pygame.display.flip()
    clock.tick(60)
 
pygame.quit()

Short as it is, every game you'll ever write starts like this. Here's what each part does.

Setting up

  • WIDTH and HEIGHT are the window size in pixels. Names in capitals are constants: values you set once and don't change while the game runs.
  • pygame.init() starts pygame.
  • pygame.display.set_mode((WIDTH, HEIGHT)) opens the window and gives you screen, the picture you draw on.
  • pygame.time.Clock() makes a clock you'll use to keep the speed steady.
  • running = True is the switch that keeps the game going.

The loop

1. Input. pygame.event.get() hands you everything that happened since the last frame: key presses, mouse clicks, and the window's close button, which is the event pygame.QUIT. When it arrives, running = False ends the loop.

2. Update. Nothing moves yet. You'll fill this in from chapter 4.

3. Draw. screen.fill((135, 206, 235)) paints the whole picture one colour. Colours are written as (red, green, blue), each from 0 to 255; this one is sky blue. Then pygame.display.flip() shows the finished picture in the window. You always draw the whole frame first and show it at the end, so the player never sees a half-drawn picture.

Finally clock.tick(60) waits just long enough that the loop runs 60 times a second, no faster.

After the loop ends, pygame.quit() closes the window cleanly.

python main.py

You should see

An empty sky-blue window

A sky-blue window called Pixel Run. Close it with its close button.

Predict, then check

What would happen if you deleted the whole for event in pygame.event.get(): block?

Show answer

The window would freeze, and your computer would soon say the program is not responding. Your operating system expects a window to collect its events regularly. And with nothing setting running = False, the close button would do nothing; you'd have to stop it with Ctrl+C in the terminal. Try it, then put the block back.

Practice

Change the sky to a sunset orange. (Hint: lots of red, some green, little blue.)

Show a solution
main.py
    screen.fill((250, 150, 80))

Any colour with a high first number, a medium second and a low third looks orange. Put the sky blue back when you're done, because the screenshots in later chapters use it.

Save your progress

git add .
git commit -m "Chapter 3: window and game loop"

Stuck, or want to compare? The finished code for this chapter is tagged ch03: run git diff ch03, or see it on GitHub.