๐ฎ Lesson 3.2: A Simple Game with Variables & Score
This is the one kids beg to build: a real game. We'll make a "catch the apple" game and keep score โ which means meeting one of the most powerful ideas in coding: the variable.
๐ฏ Learning Objectives
- Understand what a variable is and create one
- Use set and change to keep a score
- Combine everything (loops, if, sensing, broadcasts) into a game
- Show the score on the stage
Estimated Time: 50 minutes โข Builds on: Modules 1โ2 & Lesson 3.1
In This Lesson
๐ Learn It: What's a variable?
A variable is a labeled box that holds a value the computer can remember and change โ like a score, a life count, or a player's name. The box has a name ("score") and a value (0, then 1, then 2โฆ).
Think of a whiteboard on the wall labeled "SCORE." You can read what's on it, erase it, and write a new number. That's a variable.
Variables live in the orange Variables category. The three blocks you'll use constantly:
- set [score] to 0 โ put a specific value in the box
- change [score] by 1 โ add to what's already there
- [score] (the reporter) โ read the current value
๐ Learn It: Make a score variable
Click the Variables category, then โMake a Variable.โ Name it score, keep "For all sprites," and click OK.
Once created, a checkbox appears next to score in the palette โ tick it and the score shows live on the stage. New blocks (set, change, show/hide) now use your variable.
๐บ๏ธ The game plan
Our game: apples fall from the top; move the cat with the mouse to catch them; each catch scores a point.
Set the scene first: add an Apple sprite and keep the cat (or pick a "Bowl"). Optionally add a backdrop.
๐ ๏ธ Try It: Code the apple
Select the Apple sprite. It resets the score, then falls again and again from a random spot:
Read it as a story: start at the top at a random x, fall until caught (bounce back to the top if it hits the bottom edge), and when the cat catches it, add a point โ then do it all again, forever.
๐ก Where these came from
pick random is a green Operators block. touching is a blue-green Sensing block. repeat until and if are Control. You're combining four categories โ real programming!
๐ ๏ธ Try It: Code the catcher
Select the Cat. It simply follows the mouse leftโright along the bottom:
Press the green flag and move your mouse โ the cat slides along the bottom, catching apples, and the score ticks up. You built a game! ๐
โ Make it a real challenge
Add a timer: make a time left variable, set it to 30, and in a loop change time left by -1 / wait 1 second until it hits 0, then broadcast game over and stop all. Now there's a reason to hurry.
๐ Common problems & fixes
| Symptom | Fix |
|---|---|
| Score jumps up by lots at once | The apple keeps touching the cat for several frames. After scoring, add go to the top immediately (as above) so it leaves the cat. |
| Score never goes up | Check the touching block names the right sprite, and the cat and apple actually overlap. |
| Score doesn't reset | Make sure set score to 0 runs on the green flag, before the forever loop. |
๐ฌ Teach It: Design first, code second
Games are where kids' ambitions outrun their skills โ and where frustration lives. Head it off by designing on paper first.
The one-page game design
- Goal: What are you trying to do? (Catch apples.)
- Controls: How do you play? (Move the mouse.)
- Scoring: How do you win/score? (+1 per catch.)
- Lose condition: Can you lose? (Timer runs out.)
"Before we code, tell me your game like you're explaining it to a friend. How do you play? How do you win? Let's write those down โ those are our to-do list."
Explain variables with a real whiteboard
Grab a sticky note labeled "SCORE: 0." Each time you pretend to catch something, cross it out and write the next number. Then say: "the change score by 1 block does exactly this."
๐ง Ages 5โ7
You build most of it; they choose the apple, the catcher, and press GO. The thrill is catching + watching the number grow.
๐ง Ages 8โ11
They can build the catcher and add the score change. Great age to add the timer and a "game over" message.
๐ง Ages 12+
Push for levels, a high-score variable, or a "lives" system. Let them scope it โ then help them cut it down to something finishable.
โ ๏ธ The scope trap
Kids dream up "an open-world game with 50 levels." Gently steer to one working mechanic first. "Let's make catching one apple perfect, then add more." A finished tiny game beats an abandoned huge one โ for their motivation and yours.
๐ฏ Quick Check
Question 1: What is a variable?
Question 2: To add one point to the score, you useโฆ
Question 3: Where should "set score to 0" go?
Summary & What's Next
๐ Key Takeaways
- A variable is a named box that stores a changeable value like a score.
- set puts a value in; change adds to it; tick the checkbox to show it on stage.
- A game combines loops, if, sensing, operators, and variables together.
- Design on paper first, build one working mechanic, then grow it.
๐ Next up
Every project hits bugs, and the best coders are the best fixers. In Lesson 3.3 we learn a calm, kid-friendly way to debug โ plus how to learn from others by exploring and remixing shared projects.