GoalReview sprites, messages, sub-routines and the pen, and trace a script step by step.
New words
broadcast · to send a message that other scripts can wait forsub-routine · a named group of blocks that we define once and call many timespen down · a block that makes the sprite draw while it movestrace · to follow a program one block at a time
Explanation
A program is built from sprites, scripts and blocks. When one sprite must make another act, it sends a message. The block ‘broadcast’ sends it, and ‘when I receive’ starts the scripts that wait for it. A sub-routine is a named group of blocks. We define it once and call it as often as we need. The Pen extension lets a sprite draw. ‘Pen down’ starts the line and ‘pen up’ stops it. To check a program we trace it and write down every change. The turns of a closed shape add up to 360 degrees, so the sprite ends facing the way it began.
Idea
Blocks
What it does
message
broadcast, when I receive
one sprite starts the scripts of other sprites
sub-routine
define, then the new block
a group of steps used again and again
pen
pen down, pen up, erase all
draws lines as the sprite moves
loop
repeat
does the same steps several times
Worked examples
The Drummer broadcasts a message each time the space key is pressed. Every message makes the Dancer turn 15 degrees. After 4 presses the Dancer has turned 4 × 15 = 60 degrees.
Trace this program. Each hop moves the sprite up 20 and then down 20, so y does not change, and then x grows by 10. After 3 repeats the sprite is at x = 30, y = 0. Each hop waits 0.3 seconds, so the waiting takes 3 × 0.3 = 0.9 seconds.
A rectangle program: repeat 2 times: move 80 steps, turn 90 degrees, move 40 steps, turn 90 degrees. With the pen down it draws a rectangle 80 steps long and 40 steps high. The four turns of 90 degrees add up to 360, so the sprite ends where it started.
Class activity
Robot cards (3 players). Player 1 is the Programmer and writes a program on cards, for example ‘define clap: clap twice, then stamp once’. Then the main program says ‘call clap’, ‘step forward’, ‘call clap’. Player 2 is the Robot and does exactly what the cards say. Player 3 is the Checker and counts how many claps and stamps happen. Is the total what you predicted? Swap roles and change the define card. What changes in the whole program?
Practice
1
The Robot sprite turns 20 degrees each time it receives ‘ring’. The space key is pressed 5 times. How far has the Robot turned in total?
Each press sends one message, so the Robot turns 5 × 20 = 100 degrees.
2
Trace the program. Where is the sprite at the end? How many seconds does it wait in total?
Each slide moves x by 25 − 5 = 20. After 4 slides, x = −50 + 80 = 30 and y = 0. It waits 4 × 0.5 = 2 seconds.
3
A pupil’s script should draw a rectangle, but nothing appears on the stage. Which block is missing?
The ‘pen down’ block is missing. Without it the sprite moves but does not draw. It must come before the repeat block, and ‘erase all’ is a good block to put first.
4
Which is better, a message or a sub-routine? (a) Three sprites must all start dancing when the first sprite is clicked. (b) One sprite must use the same six blocks at three places in its script.
(a) A message: the first sprite broadcasts, and each of the other sprites has a ‘when I receive’ script. (b) A sub-routine: we define the six blocks once and call them three times.