Week 24 · Unit 10: Be a game developer · 3 lessons
2
Each part has its algorithm
Computing 3: Unit 10, pp. 147–160
GoalWrite an algorithm for each sub-task, build each one in Scratch and change one without breaking the other.
New words
hat block · the block that starts a script, such as “when green flag clicked”script · a stack of blocks that runs togetheredit · to change something
Explanation
Every sub-task gets its own algorithm. The picture shows the two scripts of a rabbit game. The first line of the algorithm says what starts it: the green flag, a click or a key. The other lines are the steps. Each line becomes one block. The first block is the hat block. The scripts for different sub-tasks are separate, so we can edit one of them and the others do not change. We test each sub-task on its own: click the green flag for the first one, and click the sprite or press the key for the second one.
Worked examples
Step
Instruction
1
When the space key is pressed
2
Change x by 40
3
Next costume
4
Start sound Boing
This is the algorithm for sub-task 2 of the rabbit game. Each of the four lines is one block, and the first line is the hat block.
We edit sub-task 2 so the rabbit hops further: 40 becomes 60. Sub-task 1 stays the same. The edit does not change the other script.
What I do
What should happen
click the green flag
the rabbit goes to x: −150 and says “Ready?”
press space 1 time
x is −110, next costume, a Boing sound
press space 3 times
x is −30
Test each sub-task alone. After the green flag the rabbit is at x: −150. Each press of the space key adds 40, so after 3 presses it is at −150 + 40 + 40 + 40 = −30.
Class activity
Paper rabbit (in pairs): draw a long paper strip and mark these places: −150, −110, −70, −30, 10, 50. A coin is the rabbit. Sub-task 1: put the coin on −150. Sub-task 2: each time your partner says “space”, move the coin to the next mark. Say the new x number aloud each time. Then change the hop to 60 and mark −150, −90, −30, 30. What changes? If you have a computer or tablet, build both scripts in Scratch and check your numbers.
Practice
1
Write the algorithm for this sub-task: when the up arrow key is pressed, the sprite moves up by 20 and says “Up!” for 1 second.
1 When the up arrow key is pressed. 2 Change y by 20. 3 Say “Up!” for 1 second.
2
Turn this algorithm into blocks.
Step
Instruction
1
When the green flag is clicked
2
Go to x: 0, y: 0
3
Switch costume to costume1
when green flag clicked, go to x: (0) y: (0), switch costume to [costume1 v].
3
A friend has just changed the number in script B from 40 to 60. Does script A change? Why?
No. The two scripts are separate and each starts with its own hat block.
4
A turtle starts at x: −100. Each press of the space key changes x by 25. Where is it after 3 presses?