11
Unit B · Telling a computer what to do
When things touch, and starting again
What we are making: two sprites that notice each other — and a
program that is properly ready the second time somebody plays it.
Sprites can see each other
The block is touching, and it is a question, so it goes inside an if, inside a
forever. The same shape as session 8, and the same shape as most of your project.
Catch it, score it, move it somewhere new
when green flag clicked
set score to 0
forever
if touching Apple ? then
change score by 1
go to random position
📷
images/l1-s11-script-touching.png
Screenshot: the catching script, with the sprite named inside the touching block
That last block matters more than it looks. Without it the two sprites stay touching, the
forever loop keeps noticing, and the score shoots up by hundreds. Something has to
change so the condition stops being true.
New words
touching?A question: are these two sprites overlapping right now?
random positionSomewhere on the stage, chosen by Scratch, different every time.
waitPause for a moment. Another way to stop something happening too fast.
Do it
- Add a second sprite. Anything — an apple, a ball, a star.
- Build the program above on the sprite you control with the arrow keys.
- Run it, and drive into the apple. The score goes up and the apple jumps elsewhere.
- Take out
go to random position and run it again. Watch the score explode, then put it back.
The other half: starting again properly
Click the green flag a second time. If your sprite is still where you left it, if the apple
is in the corner, or if the score carries on, your program is not really starting — it
is just continuing.
Every sprite needs its own green-flag script that puts itself back. Not one script
somewhere; one on each sprite, setting its own position and anything else that has to be
true at the start.
On the player sprite
when green flag clicked
go to x: 0 y: -120
point in direction 90
show
On the apple
when green flag clicked
go to random position
show
📷
images/l1-s11-script-resets.png
Screenshot: the two reset scripts, one on each sprite. Note you must click each sprite to see its own
💡 Tip
Test it by playing twice in a row. Green flag, play a bit, green flag again. If the second game does not look exactly like the first one did, something is not being reset. Do this every single time you add a sprite — it takes four seconds and it is the difference between a game that works at the showing and one that does not.
Try it
- Make the apple say
Caught! for half a second when it is caught. - Add a second thing to catch that is worth 5 instead of 1.
- Add something you must not touch, that takes a life away.
| What you see | Why | What to do |
|---|
| The score goes up by hundreds at once | The sprites are still touching on the next loop. | Move one of them, or add a short wait, so the condition stops being true. |
| Nothing happens when they touch | The wrong sprite is named in the block. | Check the dropdown inside touching. |
| The second game starts wrong | A sprite has no green-flag reset. | Every sprite needs one, on itself, setting its own position. |
| One sprite has vanished | It was hidden and never shown again. | Add show to its green-flag script. |
What you learned
- touching is a question, so it lives inside if, inside forever.
- Something must change after a hit, or it counts hundreds of times.
- Every sprite resets itself on the green flag. Every one.
- Playing twice in a row is the test that finds the bug everybody else ships.
You now have everything you need
Movement, a score, and things that notice each other, on top of sequence, loops and
conditions. That is genuinely enough to build a game somebody else will want to play, and
next session you will put it together before the project starts.
Challenge optional — only if you finish early- Add a second thing to catch that is worth five points.
- Add something that must not be caught, that costs a life.