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

Debug and build games

Week 25 · Unit 10: Be a game developer · 3 lessons
1

Hunt the bug, part by part

Computing 4: Unit 10, pp. 138–151
GoalTest a program one group of blocks at a time, record the results in a table and use the table to find and fix a bug.
New words
bug · a mistake in a programdebug · to find a bug and fix itsystematically · one part after another, in order, so that nothing is missedtest table · a table that lists each test, the expected result and the actual result
Explanation

When a program does not do what we want, there is a bug. To debug it, do not guess. Test it systematically. A program is made of several groups of blocks, and we can click on one group at a time to run it alone. Write the result of each group in a test table. Groups that pass are probably fine. Groups that fail point to the bug. If several groups fail and they all call the same sub-routine, look in the sub-routine first. Fix one thing, then test all the groups again, because a fix can sometimes cause a new bug.

Worked examples
Space keywhenspacekey pressedchange y by25rumbleClickwhen this sprite clickedsayReady!for1secondsrumbleSub-routinedefinerumblestart soundpopchange size by10wait0.2secondschange size by-1
A rocket game has five groups of blocks. The algorithm says that every time the rumble sub-routine runs, the rocket gets 10 bigger and then 10 smaller, so it ends at the same size. Three of the groups are shown here. They all call rumble.
GroupExpected resultActual resultPassed?
green flagrocket at y = −140, size 100sameyes
space keyy + 25 and size back to 100y + 25 but size 109no
l keyy − 25 and size back to 100y − 25 but size 109no
clicksays Ready!, size back to 100says Ready!, size 109no
define rumblesize 110, then size 100size 110, then size 109no
The test table for the rocket. Each group was clicked on its own and the results were written down. The group that sets the start position works. The three groups that call rumble all show the same problem, so the bug must be in the part they share.
definerumblestart soundpopchange size by10wait0.2secondschange size by-10
The bug is in the last block of rumble. It makes the rocket 1 smaller, but it should be 10 smaller. After the fix, every test passes. A rocket that rumbles three times now stays at size 100. Before the fix, it would have grown to 127.
Class activity

Bug detectives (with a friend): one person writes a short algorithm for a sprite, for example “move 10 steps, say Hi for 1 second, turn right 90 degrees, move 10 steps”, and then writes the blocks on paper cards. Before showing the cards, they secretly change one number. The other person acts out the cards and writes down what happens. Where does the actual result differ from the expected result? Find the bug and fix it.

Practice
1
The test table is for a boat game. Which group has the bug? What do you think is wrong with it?
GroupExpected resultActual resultPassed?
green flagboat at x = −150boat at x = −150yes
left arrowboat moves left 20boat moves left 20yes
right arrowboat turns rightboat turns leftno
define splashpop sound, waitpop sound, waityes
2
Three scripts call a sub-routine, and all three show the same problem. The scripts that do not call it all pass. Where do you look for the bug first, and why?
GroupCalls the sub-routine?Passed?
key ayesno
key byesno
clickyesno
green flagnoyes
define pulse(is the sub-routine)no
3
Put these steps for debugging in the right order: fix one thing; read the algorithm; find the groups marked no; test all the groups again; click each group and write the result.
4
Why should you fix only one thing before you test again?