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.
Sprite
Algorithm
Input
Plane
reset
green flag
Plane
action
space key
Cloud
reset
green flag
Cloud
action
space key
Worked examples
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.
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 do
What happens
click the green flag
plane at x: −180, y: 100; cloud-a at x: 150, y: 110
press space
the plane glides across for 3 seconds; after 3 seconds the cloud is cloud-b
press space again
nothing changes: both are already at the end
click the green flag
everything 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.
Plane reset: green flag. Plane action: space key. Cloud reset: green flag. Cloud action: space key.
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?
Before. It changes after 2 seconds, and the plane arrives after 3 seconds. The wait should be 3 seconds.
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?
4. The glide takes 3 seconds, and we want 1 more second: 3 + 1 = 4.
4
Which reset block would put the plane back at the start?
go to x: (−180) y: (100), inside a script that starts with when green flag clicked.