- Learnt27 min · the method, five suspects, watchers, probes
- Basic25 min · fix the broken Star Catcher
- Challenge 1–327 min · the suspect check, your watchers, a bug log
- Extrabonus · a debug switch
Review — where we got to 5 min
- A plan answers six questions and draws the Stage.
- The core loop is the part you play, over and over.
- Catchers, dodgers and clickers all use clones that appear, move and get deleted.
Quick-fire
Your stars stop appearing after a few minutes. Nothing else is wrong. What is the bug?
Reveal the answer
Clones that never get deleted. Scratch allows 300 clones at once. When they are used up, create clone of [myself v] quietly does nothing.
Today’s Topic 3 min
- What a bug is: the game does something the plan did not say
- The five usual suspects
- Watchers: numbers you can see while you play
- One script at a time, and
say []as a probe
Learning outcome
By the end of this lesson you will be able to:
- Describe a bug in two lines: what should happen, and what does.
- Check every sprite in a project for the five usual suspects.
- Use watchers and probes to find the script a bug lives in.
Learnt — how to hunt a bug 27 min
1 · What a bug is
A bug is when the game does something your plan did not say. Write every bug in two lines.
SHOULD A star that reaches the bottom disappears. DOES Stars pile up along the bottom.
Then follow the same five steps every time.
- Say what should happen.
- Say what does happen.
- Find the one script that does that job.
- Change one thing.
- Test again, the same way as before.
2 · The five usual suspects
Most bugs in a Level 2 game are one of these five. Check them first.
| # | Suspect | What you see | What to look for |
|---|---|---|---|
| 1 | Missing hat | A script never runs. | A stack with no hat block on top. |
| 2 | Wrong message name | A broadcast reaches nobody. | Two names that are nearly the same. |
| 3 | Variable not reset | A new game starts with old numbers. | No set [score v] to (0) at the start. |
| 4 | Clone never deleted | Things pile up, then stop coming. | A clone script with no delete this clone at the end. |
| 5 | A loop in the way | Blocks after a loop never run. | Two jobs in one forever. |
Suspect 1 · the missing hat
A stack with no hat has nothing to start it. It sits there for ever.
hide
forever
create clone of [myself v]
wait (1) seconds
end
when flag clicked
hide
forever
create clone of [myself v]
wait (1) seconds
end
Suspect 2 · the wrong message name
Scratch matches message names exactly. game over and gameover are two different messages.
broadcast [game over v]
when I receive [gameover v]
say [Game over!] for (2) seconds
broadcast [game over v]
when I receive [game over v]
say [Game over!] for (2) seconds
Suspect 5 · a loop in the way
A forever never finishes, so nothing fits under it. A wait until <> inside it holds up the rest of the loop. Two jobs at once need two scripts.
when flag clicked
forever
wait until <(lives) < (1)>
broadcast [game over v]
if <key [right arrow v] pressed?> then
change x by (10)
end
end
when flag clicked
forever
if <key [right arrow v] pressed?> then
change x by (10)
end
end
when flag clicked
wait until <(lives) < (1)>
broadcast [game over v]
3 · Watchers
A watcher is the orange box on the Stage that shows a variable. You can see it change while you play.
- Open the Variables drawer.
- Tick the box next to a variable. Its watcher appears on the Stage.
- Play the game and keep one eye on the watcher.
- Ask: did it change when my plan says it should? Only then?
You can also make a variable just to watch. (stars falling) counts the star clones alive right now. It should stay near three.
when flag clicked
hide
set [stars falling v] to (0)
forever
create clone of [myself v]
wait (1) seconds
end
when I start as a clone
change [stars falling v] by (1)
go to x: (pick random (-210) to (210)) y: (170)
show
repeat until <(y position) < (-170)>
change y by (-4)
if <touching [Cat Flying v]?> then
change [score v] by (1)
change [stars falling v] by (-1)
delete this clone
end
end
change [stars falling v] by (-1)
delete this clone
delete this clone.How to use this: Click the green flag, then click the stage once. Hold ← or → to move the cat, and watch the three watchers in the corner.
4 · One script at a time
In the editor, you can run one script on its own. That is how you test a suspect.
- Stop the game with the red stop sign.
- Right-click an empty part of the Script Area. Choose Clean up Blocks.
- Look for any block that is not joined to a stack. A loose block never runs.
- Click one script. It glows yellow while it runs. Watch what it does alone.
- Read it out loud, block by block. The block you cannot explain is your suspect.
5 · say as a probe
A probe is a block you add only to find a bug. say [] makes a sprite tell you what it knows.
when I start as a clone
go to x: (pick random (-210) to (210)) y: (170)
show
repeat until <(y position) < (-170)>
change y by (-6)
say (round (y position))
if <touching [Cat Flying v]?> then
say [hit!]
change [lives v] by (-1)
wait (0.5) seconds
delete this clone
end
end
delete this clone
How to use this: Click the green flag, then click the stage once. Hold ← or → to move the cat into a falling rock. Each rock says where it is, and hit! when it gets you.
Basic — fix the broken Star Catcher 25 min
This Star Catcher has five bugs planted in it, one for each suspect. Build it, then find and fix all five.
The broken game
Build these stacks exactly as printed. The mistakes are on purpose.
when flag clicked
set [lives v] to (3)
go to x: (0) y: (-130)
set rotation style [left-right v]
forever
wait until <(lives) < (1)>
broadcast [game over v]
if <key [right arrow v] pressed?> then
point in direction (90)
change x by (10)
end
if <key [left arrow v] pressed?> then
point in direction (-90)
change x by (-10)
end
end
when I receive [gameover v]
say [Game over!] for (2) seconds
stop [all v]
when flag clicked
hide
forever
create clone of [myself v]
wait (1) seconds
end
when I start as a clone
go to x: (pick random (-210) to (210)) y: (170)
show
repeat until <(y position) < (-170)>
change y by (-4)
if <touching [Cat Flying v]?> then
change [score v] by (1)
delete this clone
end
end
hide
forever
wait (pick random (2) to (4)) seconds
create clone of [myself v]
end
when I start as a clone
go to x: (pick random (-210) to (210)) y: (170)
show
repeat until <(y position) < (-170)>
change y by (-6)
if <touching [Cat Flying v]?> then
change [lives v] by (-1)
delete this clone
end
end
delete this clone
What the player sees
Five bugs, five symptoms. They show up in roughly this order.
- The cat will not move, whatever you press.
- No rocks ever fall.
- Stars that reach the bottom stay there, in a growing pile.
- Lives go to 0, then −1, and the game never ends.
- A new game starts with the last game’s score.
How to use this: Click the green flag, then click the stage once. Hold ← or → to move the cat. Lose all three lives to see the game end.
Step 1 — build it
- Build the three sprites and their stacks exactly as printed.
- Make
(score)and(lives), for all sprites.
Step 2 — play it and write it down
- Play for thirty seconds. Then press the flag and play again.
- Write one should / does pair for every problem you see.
Step 3 — turn on the watchers
- Tick score and lives in the Variables drawer.
- Play again. Which watcher does something your plan did not say?
Step 4 — check the suspects
- Choose Clean up Blocks on each sprite.
- Go through the five suspects for each sprite, one at a time.
- Mark the block you think is wrong.
Step 5 — fix one, test, repeat
- Fix one bug only.
- Press the flag and test the same way as before.
- Tick that bug off your list. Go on to the next.
Step 6 — compare
- Play yours and the fixed game above, one after the other.
- They should feel the same. If not, there is a bug left.
Check your fixes — only after you have tried
when flag clicked
set [score v] to (0)
go to x: (0) y: (-130)
set rotation style [left-right v]
forever
if <key [right arrow v] pressed?> then
point in direction (90)
change x by (10)
end
if <key [left arrow v] pressed?> then
point in direction (-90)
change x by (-10)
end
end
when flag clicked
set [lives v] to (3)
wait until <(lives) < (1)>
broadcast [game over v]
when I receive [game over v]
say [Game over!] for (2) seconds
stop [all v]
The Star gets a delete this clone after its repeat until. The Rock’s first stack gets its when flag clicked back.
I fixed the cat, and now the game ends straight away.
The wait until <> script checks lives before anything sets them. Put set [lives v] to (3) at the top of that same script.
I cannot find bug 5 — the score one.
It only shows on the second game. Play once, then press the flag again and look at the score watcher.
The suspect check
Now your own game. Check every sprite for all five suspects.
- A written table: your sprites down the side, the five suspects along the top.
- A tick in each box you checked and found fine.
- A cross where you found a bug, and a fix made for each cross.
Watch your numbers
Make every number in your game visible while you play.
- A watcher for every variable in your game.
- A clone counter for every sprite that makes clones.
- One minute of play, watching the numbers, not the sprites.
Teacher note — the counter that climbs
A clone counter that climbs slowly is the most common find. The usual cause is a second way out of the clone’s loop with no delete after it — a clone that leaves by the side, say, when the pupil only checked the bottom.
The bug log
Find three real bugs in your own game, and write each one down properly.
- Three entries. Each has should, does, where and fix.
- Each fix was one change, tested before the next.
- Then the cold test: leave it for ten minutes.
- Play it with every script window shut, following only what is on the screen.
Teacher note — finding a bug is a success
Pupils often hide bugs, as if they were mistakes. Treat every log entry as a win. A pupil who says “I have no bugs” has not looked yet.
Where two pupils happen to be at this point together, swapping games for the cold test is a nice bonus. It is never the requirement.
A debug switch
Real games keep their tools, hidden. One key shows the watchers and turns the probes on. The same key hides them again.
How to use this: Click the green flag, then click the stage once. Hold ← or → to move the cat. Press D to show the watchers and the rock probes, and D again to hide them.
Teacher notes — the switch, on the Stage
when [d v] key pressed
if <(debug) = (0)> then
set [debug v] to (1)
show variable [score v]
show variable [lives v]
show variable [stars falling v]
else
set [debug v] to (0)
hide variable [score v]
hide variable [lives v]
hide variable [stars falling v]
end
In the rock’s loop, the probe becomes say (round (y position)) inside an if <(debug) = (1)> then.
Going further: in Level 3 a list can keep a diary of what happened, with add (join [lives: ] (lives)) to [log v].
Summary 4 min
Bugs are normal. Finding them is a skill, and you now have the tools.
- Write a bug as should and does.
- Check the five suspects: hat, name, reset, delete, loop.
- Watchers show numbers changing while you play.
- Run one script at a time, and read it out loud.
- A probe makes a sprite say what it knows.
- Change one thing, then test again.
- Bug
- The game does something the plan did not say.
- Debugging
- Finding a bug and fixing it, one change at a time.
- Watcher
- The box on the Stage that shows a variable.
- Probe
- A block added only to find a bug, like a say.
Next lesson: your game works. Now we make it feel like a real game — title screen, sounds, and a best score.