AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Sprites that interact

Week 1 · Unit 1: Be a designer · 3 lessons
3

A chain of three sprites

Computing 5: Unit 1, pp. 8–19
GoalPlan, trace and debug a program in which three sprites pass messages one after another.
New words
trace · to follow a program step by step to see exactly what it doesmessage chain · sprites that start one another by sending messages in turnbug · a mistake in an algorithm or a programdebug · to find a bug and fix it
Explanation

Some programs have three or more sprites. Each sprite gets its own script, and messages pass the job from one sprite to the next. The first sprite starts with the green flag and broadcasts a message. The second sprite starts when it receives that message. When it has finished, it broadcasts a new message for the third sprite. To plan a chain, write an algorithm for each sprite and draw an arrow for every message. To check it, trace the program and write down who acts at each moment. If the order is wrong, debug the program. Look for message names that do not match or for a missing broadcast block.

Rabbit: moves, sends pass1Turtle: receives pass1, sends pass2Snail: receives pass2
Worked examples
Rabbitwhenclickedmove50stepsbroadcastpass1Turtlewhen I receivepass1move30stepsbroadcastpass2Snailwhen I receivepass2move10stepssayFinished!for2seconds
This is the whole chain. Only the rabbit starts with the green flag. The turtle and the snail both start with ‘when I receive’.
OrderSpriteAction
1Rabbitmove 50 steps, broadcast pass1
2Turtlemove 30 steps, broadcast pass2
3Snailmove 10 steps, say Finished!
A trace table shows the order. The rabbit moves 50 steps, then the turtle moves 30 steps, then the snail moves 10 steps and says ‘Finished!’. The sprites move 50 + 30 + 10 = 90 steps in all.
when I receivepass1move30steps?
A missing block is a common bug. Here the turtle’s script has no broadcast block, so the snail never receives pass2. The chain stops after the turtle.
Class activity

Human message chain (4 or more players). Each player is a sprite and holds a script card with three lines: ‘When I hear …’, an action, and ‘Say …’ (broadcast). The first player starts when the teacher says ‘flag’. Run the chain. Then swap two of the cards. What happens? Find the bug, fix it and run it again. If you have a computer or tablet, build the rabbit, turtle and snail program and trace it before you run it.

Practice
1
The three scripts are shown out of order. In which order do the sprites speak?
Henwhen I receivewakesayCluck!for1secondsbroadcastfeedFarmerwhen I receivefeedsayBreakfast!for2secondsSunwhenclickedsayMorning!for2secondsbroadcastwake
2
In the same program, at which second does the Farmer start to speak, and when does the whole program end?
3
Aisha wants the dog to bark when the cat meows, and the bird to fly away when the dog barks. How many messages does she need, and who sends each one?
4
The snail moves as soon as the green flag is clicked, not after the turtle. What is the bug?
whenclickedmove10stepssayFinished!for2seconds