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

Code the algorithm

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.

Resetwhenclickedswitch costume toladybird-ago to x:-120y:-80Go homewhen this sprite clickedstart soundPopglide2secs to x:100y:60sayHome!for2seconds
Worked examples
StepBlockGroup
When the green flag is clickedwhen green flag clickedEvents
Switch costume to ladybird-aswitch costume to [ladybird-a v]Looks
Go to x: −120, y: −80go 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.
StepGroup
When this sprite is clickedEvents
Start sound PopSound
Glide 2 seconds to x: 100, y: 60Motion
Say “Home!” for 2 secondsLooks
The go-home algorithm makes the ladybird fly home. Its steps use blocks from four groups: Events, Sound, Motion and Looks.
I doWhat happens
click the green flagcostume ladybird-a, ladybird at x: −120, y: −80
click the ladybirdPop, 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.
StepInstruction
1When the space key is pressed
2Switch costume to puppy-b
3Wait 2 seconds
4Switch costume to puppy-a
2
Which group is each of these blocks from: go to x: y: · start sound · wait · when green flag clicked?
whenclickedgo to x:0y:0wait1secondsstart soundBoing
3
This algorithm has 5 steps. How many blocks will the program have, and which one is the hat block?
StepInstruction
1When this sprite is clicked
2Start sound Chomp
3Wait 1 second
4Change size by 20
5Say “Yum!” for 2 seconds
4
How long does this script take?
whenclickedsayHi!for2secondswait1secondssayBye!for3seconds