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

Game building blocks

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.

Sub-routinedefinewobbleturn ↻15degreeswait0.1secondsturn ↺30degreeswait0.1secondsturn ↻15degreesKey scriptwhenspacekey pressedwobblechangescoreby1Message scriptwhen I receivewinwobblesayHooray!for1seconds
Worked examples
StepBlockDirection after it
start—90
1turn ↻ (15) degrees105
2turn ↺ (30) degrees75
3turn ↻ (15) degrees90
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.
whenspacekey pressedwobblemove10stepsrepeat4
This script calls wobble four times inside a loop. The sub-routine is run again in full each time.
WhatHow many
calls of wobble4
turn blocks that run4 × 3 = 12
time spent waiting4 × 0.2 = 0.8 seconds
distance moved4 × 10 = 40 steps
direction at the end90, 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?
Sub-routinedefinepulsechange size by10wait0.1secondschange size by10wait0.1secondsset size to100%Main scriptwhenspacekey pressedset size to100%pulsepulsepulse
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?
3
The pulse block from task 1 does nothing when this main script runs. What is the bug?
Empty blockdefinepulsMain scriptwhenclickedpuls
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.