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

Code the algorithm

Week 29 · Unit 12: Be a problem solver · 3 lessons
3

Two sprites, four scripts

Computing 3: Unit 12, pp. 178–193
GoalPlan and test a program with two sprites, each with a reset algorithm and an action algorithm.
New words
reset algorithm · an algorithm that puts a sprite back at its startaction algorithm · an algorithm that makes a sprite do somethingtogether · at the same time
Explanation

In a program with two sprites, each sprite has its own scripts. A sensible plan gives every sprite a reset algorithm and an action algorithm. The reset algorithms start with the green flag. The action algorithms start with an input from the player, such as a key. Both sprites can use the same key, so they start together. To keep them in time, we use wait blocks. We test by clicking the green flag first, then the action key, and then the key again after another green flag.

SpriteAlgorithmInput
Planeresetgreen flag
Planeactionspace key
Cloudresetgreen flag
Cloudactionspace key
Worked examples
Plane resetwhenclickedgo to x:-180y:100Plane actionwhenspacekey pressedglide3secs to x:60y:100
The plane flies across the sky, and the cloud starts to rain when the plane arrives. The plane glides for 3 seconds. The cloud waits for 3 seconds, and then it changes.
Cloud resetwhenclickedswitch costume tocloud-ago to x:150y:110Cloud actionwhenspacekey pressedwait3secondsswitch costume tocloud-b
The cloud has two scripts. Its reset script sets its costume and its place. Its action script waits for 3 seconds and then switches to the rainy costume.
I doWhat happens
click the green flagplane at x: −180, y: 100; cloud-a at x: 150, y: 110
press spacethe plane glides across for 3 seconds; after 3 seconds the cloud is cloud-b
press space againnothing changes: both are already at the end
click the green flageverything is back at the start
Test the whole program in order. Press the green flag, then space. After 3 seconds the plane has arrived and the cloud is raining. If we press space again without the green flag, nothing seems to happen, because the plane is already at the end and the cloud has already changed.
Class activity

Plane and cloud on paper (in a group of three): one of you is the plane and another is the cloud. The third is the player. The player says “green flag!”: the plane stands at one side of the room and the cloud holds up a card saying “cloud-a”. Then the player says “space!”. The plane walks slowly across while counting “one thousand one, one thousand two, one thousand three”, and the cloud waits for three counts and then turns the card to “cloud-b”. Say “space!” again without the green flag. What do you see?

Practice
1
List the four algorithms for the plane and the cloud, and the input of each.
2
The plane glides for 3 seconds. The cloud waits for 2 seconds and then changes. Does the cloud change before or after the plane arrives?
Planewhenspacekey pressedglide3secs to x:60y:100Cloudwhenspacekey pressedwait2secondsswitch costume tocloud-b
3
Now the cloud should change 1 second after the plane arrives. The plane still glides for 3 seconds. What number goes in the cloud’s wait block?
Planewhenspacekey pressedglide3secs to x:60y:100Cloudwhenspacekey pressedwait?secondsswitch costume tocloud-b
4
Which reset block would put the plane back at the start?