1
Two scripts at once
Computing 3: Unit 4, pp. 60–78
GoalExplain that one key press can start a script on each sprite, and that the scripts run at the same time.
New words
script · a stack of blocks that starts with a hat blockevent · something that makes a script start, such as a key pressat the same time · both things happening together
Explanation
A Scratch project can have many sprites. Each sprite has its own scripts. If two scripts start with the same event, such as the space key, they both start when the key is pressed. They run at the same time. One script does not wait for the other one. So we write one algorithm for each sprite. Each sprite follows only its own algorithm, but all of them begin together.
Worked examples
| Seconds | Bird: x | Cloud: x |
|---|---|---|
| 0 | −150 | 100 |
| 1 | 0 | 50 |
| 2 | 150 | 0 |
| 4 | 150 | −100 |
Two children each have a list of jobs. A teacher says “Go!” and both start together. One child finishes quickly, and the other takes longer, but neither waits for the other. That is how two scripts behave.
Computers do many things at once. A car’s computer can check the lights, work the radio and watch the engine sensors at the same time. Each job follows its own algorithm.
Class activity
Two actors (with a friend): each of you has a card with a short script, for example “Walk 3 steps, clap, sit down” and “Hop 2 times, say hello, turn around”. An adult says “Go!” and you both start your own script at the same time. Do you finish together? Which of you finishes first? Why?
Practice
1
Which event starts both scripts, and how many scripts run when it happens?
The space key being pressed starts both. Two scripts run, one on the dog and one on the ball.
2
The dog sprite begins at x: −150 and glides to x: 100 in 2 seconds. Where is it after 1 second?
At x: −25. It is half-way: (−150 + 100) ÷ 2 = −25.
3
Does the ball script wait for the dog script to finish?
No. Both scripts start together when the key is pressed, and each one runs on its own.
4
Write an algorithm for a fish that swims from the left to the right and one for a crab that walks from the right to the left, both starting when the space key is pressed.
For example: Fish: when space key pressed, go to x: −150, glide 3 seconds to x: 150. Crab: when space key pressed, go to x: 150, glide 3 seconds to x: −150.