Skip to main content

๐ŸŽฎ 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

Ages 5โ€“7 (with help) Ages 8โ€“11 Ages 12+
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.

The 'New Variable' dialog box in Scratch, with a name field, options for 'For all sprites' or 'For this sprite only', and Cancel/OK buttons. Behind it, the Variables palette shows blocks like 'set my variable to 0' and 'change my variable by 1'.
Figure 1: Making a variable. Name it clearly (like score) and choose "For all sprites."

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.

flowchart TD A["๐ŸŸฉ Green flag: set score = 0"] --> B["Apple: go to top, random x"] B --> C["Apple glides down"] C --> D{"Touching the cat?"} D -->|Yes| E["change score by 1"] D -->|No, hit bottom| F["missed"] E --> B F --> B style A fill:#FF8C1A,color:#fff,stroke:#333 style D fill:#5CB1D6,color:#fff,stroke:#333 style E fill:#FF8C1A,color:#fff,stroke:#333

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:

when โš‘ clicked
set score to 0
forever
go to x: pick random -200 to 200 y: 170
repeat until touching Cat?
change y by -8
if touching edge? then
go to x: pick random -200 to 200 y: 170
change score by 1

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:

when โš‘ clicked
forever
go to x: mouse x y: -140

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
SymptomFix
Score jumps up by lots at onceThe 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 upCheck the touching block names the right sprite, and the cat and apple actually overlap.
Score doesn't resetMake 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.