Week 28 · Unit 12: Be a problem solver · 3 lessons
3
Start and reset algorithms
Computing 3: Unit 12, pp. 178–193
GoalWrite linear algorithms for a sprite, with an input and an output, including one that resets the sprite.
New words
linear algorithm · an algorithm whose steps run one after the other in a straight linereset · to put something back to how it was at the startstart position · the place where a sprite begins
Explanation
A linear algorithm is a list of steps that run one after the other, from top to bottom. For a sprite, the first line says what starts the algorithm. That is the input: clicking the green flag, clicking the sprite or pressing a key. The lines after it are the outputs: what the sprite does. A reset algorithm puts the sprite back in its start position and costume. It starts with the green flag, so that every new game begins in the same way. A second algorithm makes the sprite do its job. Each job gets its own algorithm.
input: the green flag is clicked→outputs: first costume, start position
Worked examples
Line
Turtle reset algorithm
input
When the green flag is clicked
output 1
switch costume to turtle-a
output 2
go to x: −150, y: −60
This is the reset algorithm for a turtle. The input is clicking the green flag. There are two outputs: the turtle changes to its first costume and goes to its start position.
Line
Turtle move algorithm
input
When the space key is pressed
output 1
glide 2 seconds to x: 100, y: −60
output 2
say “Made it!” for 2 seconds
This is the move algorithm for the turtle. The input is pressing the space key. The outputs are that the turtle glides to the right and says something.
What we do
Where the turtle ends
green flag, then space
x: 100
space again, with no reset
x: 100 (it did not move)
green flag, then space
x: 100 (it moved again)
Why do we need the reset algorithm? Press the space key once and the turtle glides from x: −150 to x: 100. Press it again and the turtle is already at x: 100, so it does not move. After the green flag, the turtle is back at the start, and the glide works again.
Class activity
Reset and go (in pairs): mark a start spot on the floor with a piece of paper. One of you is the sprite. The other says “green flag” and the sprite goes back to the start spot and faces the wall. When the other says “space”, the sprite walks 4 steps forward and says “Made it!”. Say “space” twice without a green flag. What happens to the second walk? Then say “green flag” and “space” again. Talk about why a reset is useful.
Practice
1
Dina writes a reset algorithm for a duck, but it starts when the duck is clicked. Why is that a problem?
Line
Duck reset algorithm
input
When this sprite is clicked
output 1
switch costume to duck-a
output 2
go to x: 80, y: 20
The duck only goes back to its start when someone clicks it. A reset should start with the green flag, so that the duck is back in place at the start of every game.
2
Write the algorithm for the duck’s second job. The input is pressing the up arrow key. The outputs are that the duck glides 1 second to x: 80, y: 120 and says “Quack!” for 1 second.
When the up arrow key is pressed: glide 1 second to x: 80, y: 120; say “Quack!” for 1 second.
3
Name the input and the outputs of this algorithm.
Step
Instruction
1
When this sprite is clicked
2
Change size by 20
3
Start sound Pop
The input is clicking the sprite. The outputs are that the sprite gets bigger and a pop sound plays.
4
Why does a program often have a reset algorithm that starts with the green flag?
So that the sprite begins in the same place and costume every time, and the program works the same way each time.