Week 24 · Unit 10: Be a game developer · 3 lessons
2
One block, many scripts
Computing 6: Unit 10, pp. 134–149
GoalMake a sub-routine and call it from different scripts, and trace how many times it runs.
New words
sub-routine · a named block of code that does one job and can be used by many scriptscall · to run a sub-routine from another scriptMy Blocks · the part of Scratch where you make your own blocksmain script · a script that calls other scripts to do part of its work
Explanation
A sub-routine can be used by more than one algorithm. In a game, the same move may be needed when a key is pressed, when a sprite is clicked and when a message arrives. We write the move once under a define block and then call it in each script. When a script calls the sub-routine, all the blocks under the define block run, and then the script carries on. To trace a program, find each call and run the whole sub-routine every time it is called. If we change the define script, every script that calls it changes too. This saves time and makes the program shorter.
Worked examples
Step
Block
Direction after it
start
—
90
1
turn ↻ (15) degrees
105
2
turn ↺ (30) degrees
75
3
turn ↻ (15) degrees
90
Trace the wobble sub-routine for a sprite that points in direction 90. The turns cancel out, because 15 − 30 + 15 = 0, so the sprite ends facing the way it started.
This script calls wobble four times inside a loop. The sub-routine is run again in full each time.
What
How many
calls of wobble
4
turn blocks that run
4 × 3 = 12
time spent waiting
4 × 0.2 = 0.8 seconds
distance moved
4 × 10 = 40 steps
direction at the end
90, the same as at the start
Count what happens in the loop script above. Each call runs three turn blocks and two wait blocks of 0.1 seconds.
Class activity
Paper sub-routine (no computer): write a card ‘define spin’ with two body moves, for example turn around once and clap. Write three script cards: ‘when I clap’, ‘when I stamp’ and ‘when I hum’. Each of them has two blocks, and one of the blocks is ‘spin’. A reader runs the program by pointing at the cards, and the players act. Count how many times each body move happens in a whole run. Then change the define card, for example turn around twice, and run again. What changed? If you have a computer or tablet, make the same sub-routine in Scratch.
Practice
1
The sprite starts with size 100. What is its size at the end, and how long does the script wait in all?
Each call of pulse makes the sprite 10 bigger twice (110, then 120) and then sets the size back to 100. So the script ends with size 100. Each call waits 0.1 + 0.1 = 0.2 seconds, so three calls wait 3 × 0.2 = 0.6 seconds.
2
Ana’s game has a ‘celebrate’ block that is called in 3 different scripts. She changes one block in the define script. How many scripts does she need to edit?
Only one, the define script. The change appears in all 3 scripts that call it.
3
The pulse block from task 1 does nothing when this main script runs. What is the bug?
The main script calls puls, which is a different block from pulse. The block puls has no blocks under its define, so nothing happens. The call must use the block that has the steps.
4
Write the blocks for a sub-routine called flip that switches to costume2, waits 0.3 seconds and switches back to costume1. Then write a script that runs it when the sprite is clicked and plays a sound.
define [flip]: switch costume to [costume2 v], wait (0.3) seconds, switch costume to [costume1 v]. Main script: when this sprite clicked, flip, start sound [Pop v].