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

Reusable blocks and loops

Week 24 · Unit 10: Be a game developer · 3 lessons
3

Does it match the plan?

Computing 4: Unit 10, pp. 138–151
GoalRun a program and compare what happens with what the algorithm says should happen, then find the parts that do not match.
New words
expected result · what the algorithm says should happenactual result · what really happens when the program runstest · to run a program and compare the actual result with the expected result
Explanation

An algorithm is a plan and a program is the plan written in blocks. After we write a program, we test it. First we write down the expected result from the algorithm. Then we run the program and watch the actual result. If the two are the same, the test is passed. If they are different, there is a bug. A program with a sub-routine has several groups of blocks. We can test each group on its own by clicking it. A test table helps us to keep track: it lists what we do, what should happen and what did happen.

TestExpected resultActual resultPassed?
press the d keythe duck moves 30 steps right??
Worked examples
Part 1whendkey pressedchange x by30splashPart 2definesplashnext costumestart soundpopchange size by10wait0.5secondschange size by-10
The algorithm for a duck says: when the d key is pressed, change x by 30 and run splash. Splash switches to the next costume, plays a pop sound, makes the duck 10 bigger, waits 0.5 seconds and makes it 10 smaller. Here is the program that was written for it. Each line of the algorithm matches one block.
TestExpected resultActual resultPassed?
click define splashnext costume, pop sound, size 110, wait, size 100sameyes
press d oncex is 30, then splashsameyes
press d three timesx is 90, size is back to 100sameyes
A test table for the duck program. The duck starts at x = 0 and size 100. The test groups are run one at a time.
TestExpected resultActual resultPassed?
press d oncex is 30, then splashnothing happens, x is 0no
press s oncenothing happensx is 30, then splashno
click define splashcostume, pop, size 110, wait, size 100sameyes
Now a different program. Its hat block says “when s key pressed”, but the algorithm says the d key. The test table shows a mismatch: pressing d does nothing, and pressing s moves the duck. To fix it, choose d in the hat block and run the tests again.
Class activity

Plan and test (with a friend): one person writes a four-step algorithm for a sprite, for example “move 10 steps, say Go! for 1 second, turn right 90 degrees, move 10 steps”. The other person writes the blocks on paper cards in a row. Check each card against the algorithm. Then the first person secretly changes one number on a card. Can the first person find it by checking card by card? If you have a computer or tablet, build it in Scratch and click each block.

Practice
1
The algorithm says: when the space key is pressed, move 20 steps, say “Go!” for 1 second, then turn right 90 degrees. Find the two blocks that do not match.
whenspacekey pressedmove20stepssayGo!for2secondsturn ↺90degrees
2
Write the algorithm for this script as a table of steps. What is the sprite’s y position at the end?
whenclickedgo to x:0y:-50change y by30wait0.5secondsrepeat3
3
A program has the groups of blocks in the picture. Which group would you click to test the sub-routine on its own? What should you see?
Key fwhenfkey pressedchange x by15flipSub-routinedefineflipnext costumewait0.1secondsnext costume
4
In the duck program the duck starts at x = 0 and the d key changes x by 30. What is x after the d key has been pressed 4 times? What would you write as the expected result in a test table?