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

Reusable blocks and loops

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.

Sub-routinedefinewiggleturn ↻15degreeswait0.1secondsturn ↺30degreeswait0.1secondsturn ↻15degreesA script that calls itwhenclickedgo to x:0y:0wiggle
Worked examples
Up arrowwhenup arrowkey pressedchange y by25wiggleDown arrowwhendown arrowkey pressedchange y by-25wiggleClickwhen this sprite clickedsayBlub!for1secondswiggle
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 wiggleWith wiggle
Up arrow script1 hat + 1 + 5 = 71 hat + 1 + 1 = 3
Down arrow script73
Click script73
define wigglenone1 hat + 5 = 6
All scripts21 blocks15 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.
definewiggleturn ↻20degreeswait0.1secondsturn ↺40degreeswait0.1secondsturn ↻20degrees
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.
Startwhenclickedgo to x:0y:0helloClickwhen this sprite clickedchange size by10helloSub-routinedefinehellosayHello!for1secondsplay soundMeowuntil 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.
Key awhenakey pressedmove10stepsturn ↻90degreeswait0.2secondsturn ↺90degreesKey bwhenbkey pressedchange y by10turn ↻90degreeswait0.2secondsturn ↺90degreesKey cwhenckey pressedsayLook!for1secondsturn ↻90degreeswait0.2secondsturn ↺90degrees
3
Give two reasons why a programmer uses a sub-routine.
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.