Week 24 · Unit 10: Be a game developer · 3 lessons
1
Blocks you can reuse
Computing 4: Unit 10, pp. 138–151
GoalExplain what a sub-routine is, read a program that uses one and say why it is better than copying the same blocks again and again.
New words
sub-routine · a named set of instructions that a program can use again and againdefine block · the block that holds the steps of a sub-routinecall · to use a sub-routine in a script
Explanation
In a game, the same few actions are often needed in many places. Instead of copying them each time, we put them in a sub-routine. A sub-routine is a small set of instructions with its own name. In Scratch we make one with the My Blocks group. A define block holds the steps. Any script can then call the sub-routine by using the block with its name. This makes the program shorter. If we want to change the actions, we change them once, in the define block, and every script that calls it is changed too.
Worked examples
A fish game. Each of these three scripts has its own job, and each ends by calling the sub-routine wiggle. Pressing the up arrow raises the fish 25 steps and wiggles it. Pressing the down arrow lowers it and wiggles it. Clicking the fish makes it say “Blub!” and wiggle.
Without wiggle
With wiggle
Up arrow script
1 hat + 1 + 5 = 7
1 hat + 1 + 1 = 3
Down arrow script
7
3
Click script
7
3
define wiggle
none
1 hat + 5 = 6
All scripts
21 blocks
15 blocks
Count the blocks, including the hat blocks. Without a sub-routine, every script has its own copy of the five wiggle blocks. With a sub-routine, the five blocks are written only once.
Changing the wiggle is easy. To make the wiggle bigger, change the numbers 15 and 30 to 20 and 40 in the define block only. All three scripts wiggle more at once. The net turn is still 20 − 40 + 20 = 0, so the fish ends facing the way it started.
Class activity
Dance cards (with a friend): write a three-move dance on a card, for example clap, spin, jump, and name it “mini dance”. Now read these scripts out loud, one at a time, and act them out. When 1 is pressed: step forward, then do the mini dance. When 2 is pressed: step back, then do the mini dance. When 3 is pressed: wave, then do the mini dance. Count how many times you did each move. Then change one move on the card. How many cards had to change?
Practice
1
Name the sub-routine. List what happens, in order, when the sprite is clicked once.
The sub-routine is hello. When the sprite is clicked it first grows by 10, then says “Hello!” for 1 second, then plays the meow sound until it is done.
2
These three scripts all use the same three blocks. Which blocks should go into a sub-routine? Give it a name and write its define block.
The three blocks turn ↻ 90, wait 0.2 and turn ↺ 90 repeat in every script. For example: define [look around] with those three blocks inside, and each script calls look around.
3
Give two reasons why a programmer uses a sub-routine.
The program is shorter, and a change or a bug fix is made in one place only instead of in every copy.
4
Which job would you turn into a sub-routine? A: playing a pop sound once at the start. B: a four-block spin that three different keys all need. C: setting the starting position.
B. It is used in more than one place, and the other two jobs happen only once.