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

Scripts that run together

Week 9 · Unit 4: Be a designer · 3 lessons
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.

Birdwhenspacekey pressedgo to x:-150y:0glide2secs to x:150y:0Cloudwhenspacekey pressedgo to x:100y:120glide4secs to x:-100y:120
Worked examples
SecondsBird: xCloud: x
0−150100
1050
21500
4150−100
The bird flies from the left to the right in 2 seconds. The cloud drifts from the right to the left in 4 seconds. Both start when the space key is pressed. Look at where each one is as the time goes by. The glide is smooth, so after 1 second the bird is half-way.
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.
one computer
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?
Dogwhenspacekey pressedgo to x:-150y:-80glide2secs to x:100y:-80Ballwhenspacekey pressedgo to x:-50y:-120glide3secs to x:150y:-120
2
The dog sprite begins at x: −150 and glides to x: 100 in 2 seconds. Where is it after 1 second?
x 240−240y 180−180start x: −150end x: 100
3
Does the ball script wait for the dog script to finish?
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.