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

Find and fix bugs

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.

Ship: startwhenclickedgo to x:-180y:-100Ship: keywhenright arrowkey pressedchange x by30Lighthousewhen this sprite clickednext costumestart soundWhoosh
Worked examples
What we seeWhich sprite to check
the ship does not movethe ship
the lighthouse does not changethe lighthouse
both do the wrong thingcheck 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.
Ship (bug)whenleft arrowkey pressedchange x by30
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.
PressesShip 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?
dog: runs✓ball: does nothing✗
2
The algorithm says: when the up arrow key is pressed, the balloon changes y by 20. Which sprite and which block is wrong?
Balloonwhendown arrowkey pressedchange y by20
3
A ship starts at x: −180. Each press of the right arrow adds 30. Where is it after 3 presses?
4
Why do we test the whole program after we fix one sprite?