- Learnt27 min · two numbers, two paths, one ending
- Basic25 min · add score and lives to your stall
- Challenge 1–327 min · score it, risk it, win it
- Extrabonus · a star that gives a life back
Review — where we got to 5 min
Last lesson you built a stall where muffins fall and a bowl catches them.
- A clone starts at the top, at a random x.
- It comes down inside a
repeat (10), ten pixels at a time. - It deletes itself two ways: caught by the bowl, or landed on the ground.
Quick-fire
You play for a minute and catch loads of muffins. What does the game know about that?
Reveal the answer
Nothing at all. It is the Shark Game problem again. Today the two endings a muffin already has become the two places the game counts.
Today’s Topic 3 min
- Two variables in one game —
scoreandlives change [score v] by (1)on the caught pathchange [lives v] by (-1)on the missed pathif <(lives) = (0)> thenandstop [all v]
Learning outcome
By the end of this lesson you will be able to:
- Make two variables and reset both when the flag is clicked.
- Add to one and take from the other, in the right two places.
- End the game when a number runs out, and say how the player did.
Learnt — winning, losing, and the two paths 27 min
A game needs two things your stall has not got. Something to try for, and something to lose. Both are numbers, and you already know the block that makes one.
Two boxes, not one
A variable is a box with a name on it, from Topic 1.11. This game needs two of them, and they behave in opposite ways.
| Variable | Starts at | What happens to it |
|---|---|---|
(score) | 0 | Goes up by 1 every time a muffin is caught. |
(lives) | 3 | Goes down by 1 every time a muffin lands on the ground. |
The two paths were already there
Look at last lesson’s falling stack. It has two delete this clone blocks, and they mean different things. One is a catch. One is a miss.
That is where the two numbers go. Nothing else in the stack changes.
when I start as a clone
show
go to x: (pick random (-200) to (200)) y: (170)
repeat (34)
change y by (-10)
if <touching [Bowl v]?> then
change [score v] by (1)
delete this clone
end
end
change [lives v] by (-1)
delete this clone
Resetting, and why it matters
Both boxes need setting when the flag is clicked. Without that, your second game starts with your first game’s score, and with zero lives.
- Open Variables and click Make a Variable. Name it
score. - Make a second one called
lives. - Tick both boxes in the palette so they show on the Stage.
- On the Bowl, under
when flag clicked, addset [score v] to (0). - Add
set [lives v] to (3)under it. - Click the flag twice. Both numbers should jump back to 0 and 3.
How to use this: Click the green flag, then click the stage once and hold ← or → to slide the Bowl.
The ending
A game with no ending is not a game. When lives reaches 0, everything should stop.
when flag clicked
set [score v] to (0)
set [lives v] to (3)
wait until <(lives) = (0)>
say (join [Game over! You caught ] (score))
stop [all v]
wait until <> is a Control block that sits and waits for a question to become true. (join [] []) from Operators glues a sentence and a number together so the bowl can say both.
Use the plain say [Hello!] here, not the one with seconds on it. A timed say holds the script for three seconds before the stop, and in those three seconds more muffins land and lives drops to −2. With the plain one the stop is instant, and the bubble stays on screen anyway, because stopping does not clear a bubble.
How to use this: Click the green flag, then click the stage once and hold ← or → to slide the Bowl.
Basic — make it count 25 min
Open your stall from last lesson. Build in this order and test after every step.
How to use this: Click the green flag, then click the stage once and hold ← or → to slide the Bowl.
Step 1 — two boxes
- Make the variables
scoreandlives. - Tick both so they show on the Stage.
- Drag the monitors to a corner where no muffin will land on them.
Step 2 — reset them
- On the Bowl, add the two set blocks under the flag hat.
- Click the flag. Check both numbers change to 0 and 3.
Step 3 — count the catches
- On the muffin, put
change [score v] by (1)inside the touching-if, above the delete. - Play. Every catch should add exactly one.
- If it jumps by two, you have the block in two sprites, or twice in one.
Step 4 — count the misses
- Put
change [lives v] by (-1)below the repeat, above the last delete. - Let a muffin land. Lives should go from 3 to 2.
- Catch one. Lives should not move.
Step 5 — the ending
- Add the waiting stack from the Learnt section to the Bowl.
- Let three muffins land on purpose.
- The bowl should announce your score, and everything should stop.
Step 6 — play it twice
- Press the flag again after a game over.
- Score 0, lives 3, muffins falling. If not, a reset is missing.
My score goes up by two for one muffin.
You have more than one kind of muffins and you added the block to both — that is correct — but you also left a copy inside the second sprite’s falling loop outside the touching-if. Check where each one sits.
Lives drops even when I catch it.
The delete this clone is missing from inside the touching-if, so a caught muffins keeps falling and reaches the miss path as well.
The game ends the moment I press the flag.
set [lives v] to (3) is missing, or it is below the wait until <(lives) = (0)>. Lives starts at 0, so the wait is over immediately.
Score your own stall
Take the game you made for last lesson’s Challenge 1 — your own catcher, your own falling things — and make it count.
- Every kind of falling thing adds to the same score.
- The score is visible while you play.
- It is back to 0 on every green flag.
Three misses and you are out
Now make it possible to lose. Three lives, shown on the Stage, gone one at a time.
- A miss costs a life. A catch never does.
- At zero, the game says something and stops.
- The message includes the score.
Teacher note — the number that makes it fun
Three lives with muffin every second is brutal. Three with muffin every 1.5 seconds is about right for a first go. Let the class find their own number by playing each other’s games rather than being told.
A game you can win
Losing is only half of it. Give your stall a target — twenty muffin, say — and a different ending when the player reaches it.
- Two endings, and they say different things.
- Whichever comes first ends the game.
- Neither ending can happen twice in one run.
How to use this: Click the green flag, then click the stage once and hold ← or → to slide the Bowl.
Teacher reveal — two waits, one race
when flag clicked
wait until <(lives) = (0)>
say (join [Game over! You caught ] (score))
stop [all v]
when flag clicked
wait until <(score) > (19)>
say [Menang! Twenty muffin.]
stop [all v]
Two stacks, both waiting, and whichever question comes true first stops everything — including the other stack. That is why neither ending can happen twice.
The win test is <(score) > (19)> rather than = 20 on purpose. A score that can jump by two steps straight over an exact number, and then the game never ends. Accept = 20 from a pupil whose muffin are all worth one, and come back to it next lesson, when they stop being worth one.
The star that gives a life back
Once in a while, something falls that is worth more than points. Catch it and you get a miss back.
How to use this: Click the green flag, then click the stage once and hold ← or → to slide the Bowl.
- It falls much less often than the muffins do.
- Catching it adds a life, not a point.
- Lives never go above three, however many stars you catch.
- Missing a star costs nothing.
Teacher notes — the cap is the hard part
Adding the life is one block. Stopping it going past three needs a question around it:
if <touching [Bowl v]?> then
if <(lives) < (3)> then
change [lives v] by (1)
end
delete this clone
end
Two ifs, one inside the other. The delete stays outside the inner one, so a star at full lives still disappears — it is just wasted.
Going further: set [ghost v] effect to (50) from Looks would let you make the star fade in and out as it falls, so it looks special. Nobody has met graphic effects yet.
Summary 4 min
A game needs a reason to play and a way to lose. Both are numbers.
scorestarts at 0 and goes up on the caught path.livesstarts at 3 and goes down on the missed path.wait until <(lives) = (0)>sits quietly until the game is over, then ends it.
You did not change how the muffins fall. You added two blocks to a stack you already had, in the two places it already forked.
- Lives
- A number that counts down. When it reaches zero the game ends.
- wait until
- A Control block that holds a script until a question becomes true.
- join
- An Operators block that glues two things into one, so a sprite can say both.
- Game over
- An ending the game reaches by itself, with a message and a stop.
Next lesson: not every muffin is worth the same, and one of them you must not catch at all.