CHAPTER 8
Creating a Reaction Game with Variables
How many porcupines can you poke? In this chapter, you’ll build your very first game — a fast-paced game called Porcupine Pop-Up! Think Whack-a-Mole, but with cheeky porcupines popping up in a wetland scene and daring you to poke them before they disappear. To build the game, you’ll add a key skill to your coding practice, that of working with variables — special containers in your code that store and update important information.
In this game, you’ll make a score variable to keep track of pokes. You’ll practice initializing the variable (setting it to a starting value) and incrementing the variable (increasing its value). You’ll also apply what you learned in earlier projects, writing a function to clone and scatter the porcupines around the screen using randomness. Then you’ll create two new custom functions: one to make the porcupines pop up and vanish, and another to respond when the player pokes them. This keeps your code organized and modular, just like real game developers do!

Brainstorm
As always, feel free to make the game your own. Instead of porcupines, you could code bouncing kangaroos, hot potatoes, or stealth airplanes! Pick any character and setting you want, just make sure it’s fun, fast, and full of surprises. Let’s start coding (and poking)!
Start a New Project
Begin creating your Porcupine Pop-Up program ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access