- 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). It moves ten pixels at a time. - It deletes itself in two ways. The bowl catches it, or it lands on the ground.
Quick-fire
You play for a minute and catch lots of muffins. What does the game know about that?
Reveal the answer
Nothing at all. It is the same problem as the Shark Game. A muffin already has two endings. Today the game counts at those two places.
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. Reset both when the flag is clicked.
- Add to one and take away from the other, in the right two places.
- End the game when a number runs out. Say how the player did.
Learnt — winning, losing, and the two paths 27 min
A game needs two things your stall does not have yet. It needs something to try for, and something to lose. Both are numbers. You already know the block that makes one.
Two boxes, not one
A variable is a box with a name on it. You met it in Topic 1.11. This game needs two of them. They work 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. 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 to be set when the flag is clicked. If not, your second game starts with your first game’s score. It also starts 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. The numbers should go 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 needs an ending. When lives gets to 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. It waits until a question becomes true. (join [] []) from Operators sticks words and a number together. Then the bowl can say both.
Use the plain say [Hello!] here, not the one with seconds. A timed say holds the script for three seconds before the stop. In those three seconds more muffins land, and lives drops to −2. With the plain one, the stop happens straight away. The bubble stays on screen, because stopping does not clear it.
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. 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 they are not in the way.
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. Put it above the delete. - Play. Every catch should add one.
- Does it go up by two? Then 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. Put it 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 say your score. Then everything should stop.
Step 6 — play it twice
- Press the flag again after a game over.
- You should see score 0, lives 3, and muffins falling. If not, a reset is missing.
My score goes up by two for one muffin.
You have more than one kind of muffin. You added the block to both, and that is right. But you also left a copy in the second sprite’s falling loop, outside the touching-if. Check where each one is.
Lives drops even when I catch it.
The delete this clone is missing from inside the touching-if. So a caught muffin keeps falling. It gets to 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 straight away.
Score your own stall
Take the game you made for last lesson’s Challenge 1. It has your own catcher and your own falling things. Now give it a score.
- Every kind of falling thing adds to the same score.
- You can see the score while you play.
- It is back to 0 on every green flag.
Three misses and you are out
Now make it so you can lose. Give the player three lives, shown on the Stage. They go one at a time.
- A miss costs a life. A catch never does.
- At zero, the game says something and stops.
- The message has the score in it.
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, like twenty muffin. Make a different ending for when the player gets there.
- There are two endings. They say different things.
- The one that 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 [You win! Twenty muffins.]
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, no matter how 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)>waits until the game is over. Then it ends it.
You did not change how the muffins fall. You added two blocks to a stack you already had. They went in the two places where it already split.
- Lives
- A number that counts down. When it gets to zero, the game ends.
- wait until
- A Control block that makes a script wait until a question becomes true.
- join
- An Operators block that sticks two things into one. Then a sprite can say both.
- Game over
- An ending the game gets to by itself, with a message and a stop.
Next lesson: not every muffin is worth the same. And there is one you must not catch at all.