Learning Goals 3 min
By the end of this lesson you will be able to:
- Use the Make a Variable button in the Variables palette to create two different variables — one called score and one called lives — in the same project.
- Read the dropdown on every variable block (set [score v] to (0), change [score v] by (1)) and switch it between your two variables.
- Write one script that changes both variables at the same time — for example, a touch that adds 1 to score and subtracts 1 from lives.
Warm-Up — Aisyah's kuih counter 7 min
Aisyah is making a game at the Saturday coding club in KL. She has one variable called score and her game looks like this:
when flag clicked
set [score v] to (0)
forever
if <touching [Kuih v] ?> then
change [score v] by (1)
end
end
It works. But Aisyah wants her game to end at some point — right now you can score forever. She asks her teacher: "Can I add a second number, called lives, that starts at 3 and goes down when I miss a kuih?"
Reveal: can Scratch do this?
Yes — and that's exactly today's lesson. A Scratch project is not limited to one variable. You can make a second one, a third one, a tenth one. Each gets its own name, its own value, and its own line in the Variables palette. You'll see both in the orange variable blocks' dropdowns once you've made them.
Today Aisyah (and you) will build a project with two variables: score goes up, lives goes down, and the game has a real shape — a beginning, a middle, and an end.
New Concept — two variables, one project 15 min
So far you've only ever made one variable per project. That's a great way to learn — but real games almost always track several things at once. A platformer tracks score, lives, and maybe time-left. A racer tracks speed, lap, and fuel. Each is a separate variable with its own name and its own current value.
Making the second variable
Open the orange Variables palette on the left. At the top you'll see the Make a Variable button. Click it. A small dialog pops up asking for a name. Type lives. Click OK.
Now look at the palette. You'll see both names listed near the top, each with a little checkbox (those are the on-Stage watchers — next lesson). And every variable block now has a dropdown showing both names:
set [score v] to (0)
set [lives v] to (3)
The dropdown is the whole trick
This is the part that confuses everyone at first. There is only one set [ v] to () block. There is only one change [ v] by () block. The [ v] part — the dropdown — is what tells Scratch which variable you mean.
Click the dropdown on any orange variable block and you'll see every variable you've made. Pick the one you want. Pick a different one for the next block. It's that simple.
change [score v] by (1)
change [lives v] by (-1)
Both variables can change in the same script
This is the powerful part. When the player touches an obstacle, you probably want to both add to score (they survived) and subtract from lives (it hurt). One if-then can do both:
when flag clicked
set [score v] to (0)
set [lives v] to (3)
forever
if <touching [Bomb v] ?> then
change [score v] by (1)
change [lives v] by (-1)
wait (0.5) seconds
end
end
Reading both variables
Each variable has its own round reporter (the oval block with just the name on it). You can drop it into a say (), a () = (), anywhere a number fits:
say (score) for (1) seconds
say (lives) for (1) seconds
Worked Example — Daniel's bomb-and-coin game 12 min
Daniel wants a tiny game. A cat moves with the arrow keys. Coins give +1 score. Bombs cost −1 life. When lives hits 0, the game stops. We'll build the variables half today in eight steps.
Step 1 — Start fresh
New Scratch project. Default cat. Add two more sprites from the library: one called Coin (the Donut works fine if there's no coin) and one called Bomb (Ball with a dark colour).
Step 2 — Make the first variable
Click the orange Variables palette. Click Make a Variable. Type score. OK.
Step 3 — Make the second variable
Same button again. This time type lives. OK. Both names now appear in every variable block's dropdown.
Step 4 — Reset both at the start (on the cat)
Click the cat sprite. Build this hat-and-two-setters stack:
when flag clicked
set [score v] to (0)
set [lives v] to (3)
Step 5 — Add the coin-catching script (still on the cat)
Below the setters, add a forever-with-if-then that checks for the Coin sprite:
forever
if <touching [Coin v] ?> then
change [score v] by (1)
wait (0.3) seconds
end
end
Step 6 — Add the bomb-touching script (also on the cat)
You can't have two forevers on one sprite that both work cleanly — so add this as a second hat-block stack on the cat:
when flag clicked
forever
if <touching [Bomb v] ?> then
change [lives v] by (-1)
wait (0.5) seconds
end
end
Step 7 — Show both numbers on the Stage
In the Variables palette, tick the checkbox next to score and the checkbox next to lives. Two little watchers appear in the top-left of the Stage. Drag them where you like.
Step 8 — Click the flag and play
Move the cat (or just drag the coin/bomb onto the cat for testing). Watch score go up. Watch lives go down. Click the flag again — both reset. You now have a game that has a shape: a start, a play phase, and (when lives hits 0 — that's next-next lesson) an end.
The full assembled cat-sprite stack
when flag clicked
set [score v] to (0)
set [lives v] to (3)
forever
if <touching [Coin v] ?> then
change [score v] by (1)
wait (0.3) seconds
end
end
What you just built: the variable layer of a real arcade game. Score-and-lives is the most common pair of variables in all of gaming — from Pac-Man to Mario to your project today.
Try It Yourself — three two-variable drills 15 min
Goal: In a fresh project, make two variables called coins and gems. Set both to 0 at the flag. Then make the space key add 1 to coins, and the up-arrow key add 1 to gems. Tick both watchers so you can see them on the Stage.
when flag clicked
set [coins v] to (0)
set [gems v] to (0)
forever
if <key [space v] pressed?> then
change [coins v] by (1)
wait (0.2) seconds
end
if <key [up arrow v] pressed?> then
change [gems v] by (1)
wait (0.2) seconds
end
end
Think: Two if-thens, two different dropdowns. Same shape of block — totally different variable. The dropdown is the whole story.
Goal: Priya wants a "teh tarik counter". Two variables: cups-pulled (goes up with each space press) and sugar-left (starts at 20, goes down by 1 each cup). One if-then should change both variables.
when flag clicked
set [cups-pulled v] to (0)
set [sugar-left v] to (20)
forever
if <key [space v] pressed?> then
change [cups-pulled v] by (1)
change [sugar-left v] by (-1)
wait (0.3) seconds
end
end
Think: Two change blocks inside one if-then. One press of space, two different variables move in opposite directions. That's the pattern of every "you gain X but lose Y" mechanic in games.
Goal: Build a tiny shop simulator. Three variables: money (starts at 100), apples (starts at 0), and oranges (starts at 0). Pressing a buys one apple (apples +1, money −5). Pressing o buys one orange (oranges +1, money −3). Add a tiny "say" that announces the current money each purchase.
when flag clicked
set [money v] to (100)
set [apples v] to (0)
set [oranges v] to (0)
forever
if <key [a v] pressed?> then
change [apples v] by (1)
change [money v] by (-5)
say (money) for (1) seconds
wait (0.3) seconds
end
if <key [o v] pressed?> then
change [oranges v] by (1)
change [money v] by (-3)
say (money) for (1) seconds
wait (0.3) seconds
end
end
Think: Three variables, two if-thens, and the reporter (money) dropped right inside the say block. You're using a variable as a value, not just a counter — that's a big jump.
Mini-Challenge — Aljay's broken bomb game 5 min
"Why does Aljay's game never end?"
Aljay wrote this for his cat-vs-bombs game. He says: "score goes up fine when I touch a coin, but lives never goes down when I touch a bomb." Look at his stack:
when flag clicked
set [score v] to (0)
set [lives v] to (3)
forever
if <touching [Coin v] ?> then
change [score v] by (1)
end
if <touching [Bomb v] ?> then
change [score v] by (-1)
end
end
What's wrong? Click through it in your head — which dropdown did Aljay forget to change?
Reveal the bug and the fix
The second if-then changes score, not lives. Aljay dragged in a fresh change [ v] by () block but left the dropdown on score (its default), so bombs just subtract from his score — they never touch the lives variable at all. The watcher for lives stays stuck at 3 forever.
The fix is one click — open the dropdown on that block and pick lives:
when flag clicked
set [score v] to (0)
set [lives v] to (3)
forever
if <touching [Coin v] ?> then
change [score v] by (1)
end
if <touching [Bomb v] ?> then
change [lives v] by (-1)
end
end
Same nine blocks. One dropdown was on the wrong variable. When two variables look the same in the script, the dropdown is the only thing telling them apart. Always glance at every dropdown.
Recap 3 min
You learned to make more than one variable in a single project. The Make a Variable button in the Variables palette lets you add as many as you need — score, lives, time-left, anything. Every variable block (set, change, the round reporter) has a dropdown that picks which variable it acts on. One script can change two variables at once — that's how games like score-and-lives work. The dropdown is the small thing that does the big work: a change-by block on the wrong name silently breaks your whole game.
- Make a Variable
- The button at the top of the Variables palette that adds a new variable to your project. You pick its name. After clicking, every variable block can target it.
- Variable dropdown
- The small [name v] selector on every variable block. It picks which variable that block will read or change. Two blocks with different dropdowns act on totally different variables.
- Round reporter
- The oval block in the Variables palette, one per variable, that just reports the current value (e.g. (score)). Drop it anywhere a number fits — say, set, change, operators.
- Reset block
- A set [ v] to () placed at the top of your when ⚑ clicked stack so the variable starts at the same value every game. With many variables, you need many resets.
Homework 2 min
The Two-Number Game. Build a small game in a fresh Scratch project with exactly two variables.
- Make two variables: score and lives.
- At the flag, set [score v] to (0) and set [lives v] to (3).
- Add one sprite called Good Thing (any sprite — a star, an apple) that moves around (you can use a tiny glide loop or arrow keys).
- Add a second sprite called Bad Thing (a bomb, a snake) that also moves.
- On the cat: a forever-with-two-if-thens. Touching Good Thing → change [score v] by (1). Touching Bad Thing → change [lives v] by (-1). Add a wait (0.5) seconds inside each if-then so one touch doesn't count many times.
- Tick both watcher checkboxes so score and lives show on the Stage.
Save as HW-L2-31-Two-Number-Game.sb3. Click the flag, play for 30 seconds, and watch both numbers move.
Bring back next class:
- The
.sb3file. - Your answer to: "What would happen if you forgot to put set [lives v] to (3) at the top? Try it — restart five times in a row and watch what lives does."
Heads up for next class: SCR-L02-32 looks at the little number boxes that appeared on your Stage when you ticked those watcher checkboxes — they have three different display modes, including a slider the player can drag.