Week 29 · Unit 12: Be a problem solver · 3 lessons
1
Algorithm to blocks
Computing 3: Unit 12, pp. 178–193
GoalTurn a linear algorithm into a Scratch program, one block for each step.
New words
block group · a set of blocks of one colour and kind, such as Motion or Lookshat block · the first block of a script, which says what starts itrun · to start a program and see what it does
Explanation
We can turn an algorithm into a Scratch program. The first step of the algorithm becomes the hat block, taken from the Events group. Each step after that becomes one block, taken from its own group. Motion has blocks such as “go to” and “glide”. Looks has “switch costume to” and “say”. Sound has “start sound”, and Control has “wait”. We type the numbers and pick the names from the lists. Then we run the program and compare what happens with the algorithm.
Worked examples
Step
Block
Group
When the green flag is clicked
when green flag clicked
Events
Switch costume to ladybird-a
switch costume to [ladybird-a v]
Looks
Go to x: −120, y: −80
go to x: (−120) y: (−80)
Motion
The reset algorithm puts the ladybird in its start place. Each of its 3 steps becomes one block, and the first one becomes the hat block.
Step
Group
When this sprite is clicked
Events
Start sound Pop
Sound
Glide 2 seconds to x: 100, y: 60
Motion
Say “Home!” for 2 seconds
Looks
The go-home algorithm makes the ladybird fly home. Its steps use blocks from four groups: Events, Sound, Motion and Looks.
I do
What happens
click the green flag
costume ladybird-a, ladybird at x: −120, y: −80
click the ladybird
Pop, glide to x: 100, y: 60, say Home!
Now we run the program. Click the green flag: the ladybird changes costume and goes to the left. Click the ladybird: it plays Pop, glides for 2 seconds and says “Home!” for 2 seconds. The pop takes no time, so the script takes 2 + 2 = 4 seconds.
Class activity
Block cards (in pairs): write each step of the go-home algorithm on its own card and the matching block on another card. Mix up all the cards, and match each step to its block. Then sort the block cards into four piles: Events, Sound, Motion and Looks. Colour the piles yellow, pink, blue and purple. Then change one number, such as the glide time. Does the total time change? If you have a computer or tablet, build both algorithms in Scratch.
Practice
1
Write the blocks for this algorithm.
Step
Instruction
1
When the space key is pressed
2
Switch costume to puppy-b
3
Wait 2 seconds
4
Switch costume to puppy-a
when [space v] key pressed, switch costume to [puppy-b v], wait (2) seconds, switch costume to [puppy-a v].
2
Which group is each of these blocks from: go to x: y: · start sound · wait · when green flag clicked?
go to x: y: is Motion. start sound is Sound. wait is Control. when green flag clicked is Events.
3
This algorithm has 5 steps. How many blocks will the program have, and which one is the hat block?
Step
Instruction
1
When this sprite is clicked
2
Start sound Chomp
3
Wait 1 second
4
Change size by 20
5
Say “Yum!” for 2 seconds
5 blocks, one for each step. The hat block is when this sprite clicked, made from the first step.