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.
Block
What it does
define [name]
holds the steps of a sub-routine
repeat (n)
runs its blocks n times
pen down
draws a line as the sprite moves
pen up
stops drawing
Worked examples
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.
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.
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?
Each hop raises the frog 40 − 20 = 20, so after two hops it is at y = 40. Each hop waits 0.5 seconds, so it takes 2 × 0.5 = 1 second.
2
Write the blocks that draw an equilateral triangle with sides of 60. Each turn is 120 degrees.
when green flag clicked; pen down; repeat (3): move (60) steps, turn ↻ (120) degrees; pen up.
3
This script should draw a square, but one side is missing. What is the bug?
The loop repeats only 3 times, so only 3 sides are drawn. Change repeat (3) to repeat (4).
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?
One place: the define block of the jump sub-routine. All four scripts call it.