Week 25 · Unit 10: Be a game developer · 3 lessons
2
Debug two sprites
Computing 3: Unit 10, pp. 147–160
GoalFind out which sprite has the bug in a program with two sprites, and fix it.
New words
sprite · a character or object in a Scratch projectsymptom · what we see that tells us something is wrongbackdrop · the picture behind the sprites
Explanation
A game often has more than one sprite, and each sprite has its own scripts and its own algorithms. When something goes wrong, first ask which sprite is not doing its job. The symptom tells us: if the lighthouse never changes, look at the lighthouse. Then compare that sprite’s scripts with its algorithm, line by line. Fix only the part that is wrong. At the end, test the whole program again, because the sprites have to work together.
Worked examples
What we see
Which sprite to check
the ship does not move
the ship
the lighthouse does not change
the lighthouse
both do the wrong thing
check both
We run the ship and lighthouse game. The ship moves when we press the right arrow. The lighthouse does nothing when we click it. The symptom points to the lighthouse, so we look at its scripts first.
Now the ship does not move when we press the right arrow. The ship’s algorithm says “when the right arrow key is pressed”. The program says “left arrow”. The hat block is wrong. We change it to “right arrow” and test again.
Presses
Ship x
0
−180
1
−150
2
−120
3
−90
4
−60
After the fix we test the whole game. The ship starts at x: −180. Each press of the right arrow adds 30, so after 4 presses it is at −180 + 120 = −60. Then we click the lighthouse and it changes costume and plays a sound.
Class activity
Two-sprite game on paper (in a group of three): one of you is the ship and one is the lighthouse. The third is the tester and holds both algorithms. The tester says “green flag”, “right arrow”, or “click”. The ship moves 30 cm each time “right arrow” is called. The lighthouse turns round when “click” is called. Secretly, tell the lighthouse to do something different from the algorithm. Can the tester find which player has the bug?
Practice
1
In a dog and ball game, the dog runs when we press the right arrow, but the ball never bounces when we click it. Which sprite should we check first?
The ball, because the symptom is in the ball. The dog works.
2
The algorithm says: when the up arrow key is pressed, the balloon changes y by 20. Which sprite and which block is wrong?
The balloon. The hat block says “down arrow” and should say “up arrow”.
3
A ship starts at x: −180. Each press of the right arrow adds 30. Where is it after 3 presses?
−180 + 30 + 30 + 30 = −90.
4
Why do we test the whole program after we fix one sprite?
Because the sprites work together, and a fix in one sprite can change what the game looks like as a whole.