- Learnt27 min · variables, set and change, the monitor
- Basic25 min · add a score to the Shark Game
- Challenge 1–327 min · score it, weight it, time it
- Extrabonus · a best score worth beating
Review — where we got to 5 min
Last lesson you built the Shark Game with things you already knew.
- The controls came from Topic 1.6. The mouth came from 1.7. The random swim came from 1.9.
- Four kinds of fish, each a sprite that clones itself.
- A fish checks two things. Am I touching the shark? Is space pressed?
Quick-fire
You eat twenty fish. How does the game know you have done well?
Reveal the answer
It does not. Nothing in the game remembers anything. Today we fix that. It is the last big idea of Level 1.
Today’s Topic 3 min
- Variables: a box that keeps a number
set [score v] to (0)andchange [score v] by (1)- The monitor on the Stage
- How to end a game and say the score
Learning outcome
By the end of this lesson you will be able to:
- Make a variable and show it on the Stage.
- Reset it at the start of a game. Add to it while you play.
- End a game when something runs out, and show the result.
Learnt — remembering a number 27 min
A box with a name on it
A variable is a box. You give it a name and put a number in it. It keeps that number until something changes it.
- Open Variables in the Blocks Panel.
- Click Make a Variable.
- Call it score and leave “For all sprites” selected.
- A tick box appears next to it. When it is ticked, the number shows on the Stage.
The little display in the top-left corner is called a monitor. It shows what is in the box.
Two blocks do everything
| Block | What it does | Where it goes |
|---|---|---|
set [score v] to (0) | Puts a number in the box. The old number is gone. | Once, at the start, on the green flag. |
change [score v] by (1) | Adds to what is already there. | Every time the player does the thing that scores. |
One box, everybody writing into it
You have four kinds of fish. All four should add to the same score. That is why the variable is for all sprites.
“For this sprite only” gives each kind of fish its own box. Your monitor would show just one of the four. Each fish would count the right way, but the total would be wrong.
Where the change block goes
Put it right next to the delete this clone. That is the moment a fish is eaten.
when I start as a clone
wait (0.5) seconds
forever
if <touching [Shark v]?> then
if <key [space v] pressed?> then
change [score v] by (1)
delete this clone
end
end
end
How to use this: Click the green flag, then click the stage once and use the arrow keys. The space bar does something as well — find out what.
Basic — add the score 25 min
Open your Shark Game from last lesson. You will make a small change to a big project.
Step 1 — make the variable
Variables → Make a Variable → score, for all sprites. Check that the monitor appears on the Stage.
Step 2 — reset it
On the Shark, add set [score v] to (0) to the when flag clicked stack. Put it near the top, with the other blocks that set things up.
Step 3 — count one kind of fish
On your first fish sprite, add change [score v] by (1) just above the delete this clone. Test it. Eat one and watch the number.
Step 4 — the other three
Add the same block to the other three fish sprites. Test each one. It is easy to miss one and not notice.
Step 5 — play a whole round
Eat every fish. Check that the score is the same as the number of fish at the start. If it is too low, one kind of fish is not counting.
Step 6 — move the monitor
Drag the monitor to a corner where it is not in the way. You can right-click it for large readout.
The score keeps going up after I eat every fish.
Your change [score v] by (1) is outside one of the two ifs. So it counts all the time, not just fish. It belongs inside both.
The second game starts at 14.
set [score v] to (0) is missing from the green-flag stack. The box kept its number, just as it should.
A score for your own game
Add a score to the game you built for last lesson’s Challenge 1. It has your own world and your own things to collect.
- Every kind of thing adds to the same score.
- You can see the score while you play.
- It starts at 0 on every green flag.
Not all worth the same
Make some things worth more than others. The player must see which ones before they catch them.
- At least two different values.
- The ones worth more are harder to get. They are small, fast, or far away.
- Looking at the Stage tells you which is which.
Teacher note
This is the first time a design decision has a right answer that is not about code. A score that rewards the easy fish makes the hard ones pointless; one that rewards the hard ones gives the player something to decide. Worth asking the class to play each other’s and say which felt better.
Give it an ending
In a game that never ends, the score does not mean much. Add a timer. It counts down from thirty seconds on the Stage.
When it gets to zero, everything stops. Your character says the final score.
Done when: you can see the clock count down. The game ends by itself at zero. Your character says the final score. A new green flag gives you thirty seconds again and a score of 0.Teacher reveal — the countdown
when flag clicked
set [time v] to (30)
repeat until <(time) = (0)>
wait (1) seconds
change [time v] by (-1)
end
say (join [Time! You got ] (score))
stop [all v]
Two things here are new and both are worth naming. repeat until <> is a loop with a question on it, and join () () from Operators is how a sentence and a number become one thing to say.
Accept a plain repeat (30) too — it is simpler and does the same job here. The repeat until <> version is the one that would still work if something else could also end the game.
A best score worth beating
Add a second variable called best. It remembers the highest score anyone has got on your game.
(score) goes back to 0 every round. (best) does not. When a game beats it, the game says so.
Summary 5 min
- A variable is a named box that remembers a number. The monitor on the Stage shows what is in it.
set [score v] to (0)once at the start.change [score v] by (1)each time it should go up.- “For all sprites” means every sprite shares one box. So four kinds of fish can add to one total.
- Put the
change [score v] by (1)before thedelete this clone, or it never runs. - A variable keeps its number from one run to the next. Most of the time you reset it. Sometimes you want to keep it.
- Variable
- A named box that holds a number or some words. It can change while the game runs.
- Monitor
- The little box on the Stage that shows what is in a variable.
- For all sprites
- One box that every sprite shares. It is not a private box for one sprite.
That is Level 1
Twelve lessons ago the cat could not move. Now you can build a whole game. It has a character you drive and enemies that move by themselves. It has clones, random numbers and a score.
Share your finished game and send the link to your teacher. Next comes Muffin Catcher. It has things that fall, a score and lives. It starts from what you built today.