Build a platformer game

Chapter 12 of 1260 min

Make it yours

In this chapter you will:

  • choose one improvement and build it without step-by-step instructions

Up to now you've followed instructions. This chapter gives you directions and hints, and you decide the rest. Building something nobody told you exactly how to build is what turns a tutorial into your game.

Pick one

Each of these is a real feature of real platformers. Start with one, finish it, commit it, then pick another if you like.

  • A face. Draw two small white rectangles with black dots on the player, so it has eyes. Make them look left or right depending on which way the player last moved. (You'll need a variable that remembers the direction.)
  • Lives. Start with 3. Losing takes one life and puts the player back at the start, keeping the coins; at 0 it's game over. Show the lives next to the score. (A new state, or a check where the game is lost.)
  • More levels. Make LEVELS, a list of level lists, and a level_number. Reaching the flag loads the next level; after the last one, you win. (load_level reads LEVELS[level_number] instead of LEVEL.)
  • A timer. Count frames while playing and show the time in seconds (frames ÷ 60). Show it on the win screen, so players can try to beat their time.
  • Real art. Swap rectangles for pictures with pygame.image.load("player.png") and screen.blit(...). Kenney has free game art you're allowed to use in your own games (it's released as CC0; check the licence on each pack you download). Keep image files in your project folder, so pygbag packs them into the web version too.
  • Sound. pygame.mixer.Sound("coin.ogg").play() when a coin is collected. Use .ogg files, which work in browsers as well as on your computer.

Predict, then check

You choose "more levels". What should happen to score when the player reaches the next level, and where in the code does that decision live?

Show answer

That's your design decision. Keeping it means the score counts the whole run, which is usually more fun. Either way, it lives in the "new" state block, which sets score = 0: you'd want a separate state (or a flag) for "next level" that reloads the level but leaves the score alone. Noticing where a decision lives in the code is most of the work of changing it.

There's no solution to reveal for this chapter. Use what you've learned, the pygame-ce docs, and your tutor if you have one.

You built a game

You started with an empty folder. You now have a game with a game loop, gravity, collisions, levels made of text, a scrolling camera, collectables, enemies and a state machine, playable by anyone in a browser. Every line of it is yours, and you can explain every line.

Save your progress

git add .
git commit -m "Chapter 12: my own improvement"
git push