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

Term 1 review

Week 11 · Review: Term 1 · 3 lessons
1

Programs: messages, sub-routines and pen

Computing 5: Units 1–4, pp. 8–57
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.

IdeaBlocksWhat it does
messagebroadcast, when I receiveone sprite starts the scripts of other sprites
sub-routinedefine, then the new blocka group of steps used again and again
penpen down, pen up, erase alldraws lines as the sprite moves
looprepeatdoes the same steps several times
Worked examples
Drummerwhenspacekey pressedbroadcastbeatDancerwhen I receivebeatturn ↻15degrees
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.
definedefinehopchange y by20wait0.3secondschange y by-20mainwhenclickedgo to x:0y:0hopchange x by10repeat3
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.
x 240−240y 180−180
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?
Bellwhenspacekey pressedbroadcastringRobotwhen I receiveringturn ↻20degrees
2
Trace the program. Where is the sprite at the end? How many seconds does it wait in total?
definedefineslidechange x by25wait0.5secondschange x by-5mainwhenclickedgo to x:-50y:0sliderepeat4
3
A pupil’s script should draw a rectangle, but nothing appears on the stage. Which block is missing?
whenclickedgo to x:0y:0move80stepsturn ↻90degreesmove40stepsturn ↻90degreesrepeat2
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.