3
Build, reset and test
Computing 3: Unit 1, pp. 8–27
GoalBuild a short program that resets itself, then run it twice to test that it works.
New words
test · to run a program and check that it does what we wantedrun · to start a program so it follows its blocksoriginal · the way something was at the very start
Explanation
Here is a plan for a good program. First decide the start place and the start size. Put “go to” and “set size to” straight after the hat block. Then add the action blocks. Next, join any blocks that repeat. Last, test the program: run it twice. If the sprite ends in the same place, with the same size, both times, the reset works. A short program that resets itself is a good program.
Worked examples
| Click | No reset: y | No reset: size | With reset: y | With reset: size |
|---|---|---|---|---|
| 1st | 0 | 80 | 0 | 80 |
| 2nd | 100 | 60 | 0 | 80 |
The reset blocks must come first. In this program the set size block is last. It puts the size back to 100 at the end, so the shrinking is cancelled and the sprite is always size 100. The red block is in the wrong place.
run once→write down the end place→run again→compare
To test a reset, run the program once and write down where the sprite stops. Run it again. If the place and size are the same, the reset works. If they are different, something is missing.Class activity
Test it on paper (with a friend): the program is “go to x: −3, change x by 2, change x by 2” on a paper number line from −4 to 4, with a coin as the sprite. Run it twice and check that the coin ends in the same place both times. Then remove the go to card and run it twice. What happens the second time? Write down what you see.
Practice
1
A sprite follows this program. Where is it, and what size is it, after the first run? And after the second run?
At x: 0, y: 0 and size 130 after both runs. The reset blocks put it back at the start each time: y goes from −80 to 0 and size from 100 to 130.
2
Write a more concise version of the program in question 1. How many blocks does it have?
Hat, go to x: (0) y: (−80), set size to (100) %, change y by (80), change size by (30). It has 5 blocks (the original had 6).
3
Find the mistake. The sprite should always start at x: −100.
The go to block is at the end. It must go right after the hat block so the sprite starts in the right place. At the end it only sends the sprite back after the move.
4
A kite starts at y: −80 and climbs 100 each run. There is no reset. After the first run it is at y: 20. Where is it after the second run?
At y: 120. 20 + 100 = 120.