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

Term 3 review

Week 30 · Review: Term 3 · 3 lessons
2

Scratch scripts and sub-routines

Computing 4: Units 9–12, pp. 123–179
GoalRead Scratch scripts with sub-routines, loops and pens, trace them exactly and find a bug.
New words
sub-routine · a named set of blocks that can be used again and againrepeat · a loop block that runs its blocks a set number of timespen down · a block that makes a sprite draw a line when it movesbug · a mistake in a program
Explanation

A game script is made of events, motion, looks, sound and control blocks. A sub-routine is a small group of blocks with its own name. We make one with a define block and call it from other scripts. If we change the define block, every call changes too. The repeat block runs its blocks a set number of times. With the pen blocks, a sprite draws as it moves. To trace a script, follow it block by block and write down the numbers as they change. If the result is different from the plan, there is a bug. Test one part, find the bug and fix it.

BlockWhat it does
define [name]holds the steps of a sub-routine
repeat (n)runs its blocks n times
pen downdraws a line as the sprite moves
pen upstops drawing
Worked examples
Sub-routinedefinejumpchange y by50wait0.3secondschange y by-50Scriptwhenspacekey pressedjumprepeat3
A frog jumps. The define block moves it up 50, waits 0.3 seconds and moves it down 50. Calling jump three times takes 3 × 0.3 = 0.9 seconds, and the frog ends where it began.
whenclickedpen downmove50stepsturn ↻90degreesrepeat4pen up
The pen draws a shape. Four times, the sprite moves 50 steps and turns 90 degrees. That draws a square with sides of 50. The total turn is 4 × 90 = 360 degrees.
whenclickedpen downmove50stepsturn ↻80degreesrepeat4
A bug. This script should draw a closed square, but the turns are 80 degrees. The total turn is 4 × 80 = 320 degrees, not 360. The shape stays open. The fix is to change 80 to 90.
Class activity

Trace it on paper (with family): draw a small square on squared paper. A grown-up reads the script of the square and you walk it on the floor with tape: 4 steps forward, a right-angle turn, and so on. Then try a triangle: what turn is needed? Change the sides and try again. Say which part is the loop.

Practice
1
The frog’s hop sub-routine changes y by 40, waits 0.5 seconds and changes y by −20. The frog starts at y = 0, and the script calls hop twice. Where is the frog at the end, and how long does it take?
Sub-routinedefinehopchange y by40wait0.5secondschange y by-20Scriptwhenclickedset y to0hoprepeat2
2
Write the blocks that draw an equilateral triangle with sides of 60. Each turn is 120 degrees.
3
This script should draw a square, but one side is missing. What is the bug?
whenclickedpen downmove40stepsturn ↻90degreesrepeat3pen up
4
The frog needs the jump in four scripts. The child wants a higher jump of 70 instead of 50. How many places must be changed, and where?