Build a platformer game

Chapter 8 of 1220 min

A scrolling camera

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

In this chapter you will:

  • make the level three screens wide
  • learn the difference between where things are and where they're drawn
  • add a camera that follows the player

A bigger level

Replace LEVEL with this one. It's 60 tiles wide, three times the window:

main.py
LEVEL = [
    "                                                            ",
    "                                                            ",
    "                                                            ",
    "                                                            ",
    "                                                            ",
    "                              ####                          ",
    "                                                            ",
    "            ###            ##                               ",
    "  P                        ##                               ",
    "######  ############  ############   #######################",
    "######  ############  ############   #######################",
    "######  ############  ############   #######################",
]

Run it and walk right. The player walks straight out of the window and keeps going where you can't see it. (If you added edge limits in chapter 4's practice, delete them now: they'd stop the player at the edge of the window.)

World and screen

Everything in your game has a position in the world: the player might be at x = 1500, far into the level. The window only shows an 800-pixel slice of that world. The camera decides which slice.

If the camera is at camera_x = 1100, the window shows the world from x = 1100 to 1900. So something at world x = 1500 must be drawn at 1500 − 1100 = 400 on the screen. That's the whole idea: to draw, subtract camera_x.

The world positions never change. Collisions still use them, so the collision code doesn't change at all. Only the drawing does.

Follow the player

Under walls, start = load_level() and the player line, add:

main.py
level_width = len(LEVEL[0]) * TILE

Then replace the draw step:

main.py
    # 3. Draw
    camera_x = player.centerx - WIDTH // 2
    camera_x = max(0, min(camera_x, level_width - WIDTH))
 
    screen.fill((135, 206, 235))
    for wall in walls:
        pygame.draw.rect(screen, (90, 160, 70), wall.move(-camera_x, 0))
    pygame.draw.rect(screen, (40, 70, 200), player.move(-camera_x, 0))
    pygame.display.flip()
    clock.tick(60)
  • player.centerx - WIDTH // 2 puts the camera so the player is in the middle of the window. (// divides and drops the remainder, so the result stays a whole number.)
  • The second line keeps the camera inside the level: never left of 0, and never so far right that it shows the empty space past the level's end.
  • wall.move(-camera_x, 0) gives you a copy of the rectangle, moved left by camera_x, just for drawing. The real wall stays where it is.
python main.py

You should see

The player in the middle of the window, far into a wider level

Walk right: the level scrolls and the player stays in the middle. At the start and the end, the camera stops and the player walks to the edge.

Predict, then check

What would you see without the max(0, min(...)) line, standing at the very start of the level?

Show answer

At the start, player.centerx - WIDTH // 2 is negative, about −300. The camera would show 300 pixels of nothing to the left of the level: empty sky. The max(0, ...) part stops that. At the far end, the min(...) part does the same on the right.

Practice

Most platformers let you see further ahead than behind. Change the camera so the player sits a third of the way across the window instead of in the middle.

Show a solution
main.py
    camera_x = player.centerx - WIDTH // 3

Only this line changes. The player is now 266 pixels from the left edge, so you see more of what's coming.

Save your progress

git add .
git commit -m "Chapter 8: scrolling camera"

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