- 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 out of things you already knew.
- The controller from Topic 1.6, the mouth from 1.7, the random swim from 1.9.
- Four kinds of fish, each a sprite that clones itself.
- A fish checks two things: am I touching the shark, and is space down?
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. That is what today fixes, and it is the last big idea of Level 1.
Today’s Topic 3 min
- Variables — a box the project can remember something in
set [score v] to (0)andchange [score v] by (1)- The monitor on the Stage
- Ending a game, and saying the final 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 and add to it during play.
- End a game when something has run out, and report 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, you put a number in it, and 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. Ticked means the number shows on the Stage.
That little display in the top-left corner is called a monitor. It is a window into the box.
Two blocks do everything
| Block | What it does | Where it goes |
|---|---|---|
set [score v] to (0) | Puts a number in the box, throwing away whatever was there. | 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, and all four should add to the same score. That is why the variable was made for all sprites.
“For this sprite only” would give each kind of fish its own private box, and your monitor would show one of the four. The fish would each be counting correctly and the total would be wrong.
Where the change block goes
Right next to the delete this clone — because that is the exact 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. This is a small change to a big project.
Step 1 — make the variable
Variables → Make a Variable → score, for all sprites. Check 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, near the top with the other setup blocks.
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: eat one, watch the number.
Step 4 — the other three
Same line on the other three fish sprites. Test each one — it is easy to add it to three of four and not notice.
Step 5 — play a whole round
Clear the sea and check the score matches the number of fish you started with. If it is short, one kind is not counting.
Step 6 — move the monitor
Drag the monitor to a corner where it does not sit on top of the game. Right-click it for large readout if you prefer.
The score keeps going up after I clear the sea.
Your change [score v] by (1) is outside one of the two ifs, so it is counting frames rather than 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, exactly as it is supposed to.
A score for your own game
Add a score to the game you built for last lesson’s Challenge 1 — your own world, your own collectibles.
- Every kind of collectible adds to the same score.
- The score is visible while you play.
- It starts at 0 every green flag.
Not all worth the same
Make some collectibles worth more than others — and make it possible for the player to tell which, before they catch it.
- At least two different values.
- The valuable ones are harder: smaller, faster, or further 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
A game that never ends has no score worth having. Add a timer: thirty seconds, counting down on the Stage.
When it reaches zero, everything stops and your character says the final score out loud.
Done when: the clock counts down visibly, the game ends by itself at zero, the final score is announced, and a new green flag gives you a fresh thirty seconds 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 — best — that remembers the highest score anyone has ever got on your game.
(score) goes back to 0 every round. (best) does not. When a round beats it, the game says so.
Summary 5 min
- A variable is a named box that remembers a number. The monitor on the Stage is a window into it.
set [score v] to (0)once at the start;change [score v] by (1)every time it should go up.- “For all sprites” means everybody shares one box. That is what lets four kinds of fish add to one total.
- Put the
change [score v] by (1)before thedelete this clone, or it never runs. - A variable keeps its value between runs — which you usually reset, and sometimes want.
- Variable
- A named box holding a number or some words, which can change while the project runs.
- Monitor
- The little display on the Stage showing a variable’s value.
- For all sprites
- One box shared by everybody, as opposed to a private one per sprite.
That is Level 1
Twelve lessons ago the cat could not move. You can now build a complete game with a character you drive, enemies that think for themselves, clones, randomness and a score.
Share your finished game and send the link to your teacher. Next comes Muffin Catcher — things that fall, a score, and lives — and it starts from exactly what you built today.