GoalRecall how scripts, variables, messages and test plans fit together in a game.
New words
broadcast · a block that sends a named message to all spritesvariable · a named place where a program keeps a value that can changetest plan · a table of tests with expected and actual resultscustom block · a block that you define once and use many times
Explanation
A game is built from several scripts that work together. One sprite can broadcast a message when something happens, for example when it touches a prize. Another sprite has a script that starts with when I receive the same message, and it changes the score. A variable such as score is set to 0 at the start, and then it changes as the game goes on. A custom block holds steps that we use in many places, so we write them once and fix them once. When the game is built, we run a test plan. Each test has an input, an expected result and an actual result. If they are different, there is a bug to find and fix.
Worked examples
Touch
Message
Score
start
—
0
1
caught
2
2
caught
4
3
caught
6
Trace the game. The score starts at 0. The bird touches the worm three times. Each time the message caught is sent and the scorer adds 2. The score goes 2, 4, 6, so it ends at 6.
This custom block is defined once. Every time the game needs it, one call runs all its steps. If we change the words in the definition, every call changes too.
Score
Expected
Actual
Result
2
Keep going
Keep going
pass
6
You win!
You win!
pass
7
You win!
Keep going
FAIL
The game says You win! when the score is 6 or more. The test plan checks the edge of the rule. The test with 6 is the boundary. The test with 7 fails, so the condition probably says score = 6 and not score > 5.
Class activity
Human game (no computer): make the game with people. Two children are sprites. The bird touches a card with a worm drawn on it and calls out caught. The scorer hears the message, adds 2 to the number on the board and calls out the new score. Play until the score is 10. Then change a rule, for example adds 3 instead of 2, and decide which script you have to change. Why do we only change one script? If you have a computer or tablet, build the two scripts in Scratch.
Practice
1
A frog catches 3 flies. Each time the message caught is received, the score goes up by 5. The score starts at 0. What is the score at the end?
5 + 5 + 5 = 15.
2
Why is it better to send a broadcast message than to copy the scoring blocks into every sprite?
With a message only one script changes the score. If the rule changes we fix one place, and there are fewer mistakes.
3
The game should say You win! for a score of 6 or more. Look at the tests. Which test fails and what is the likely bug?
Score
Expected
Actual
4
Keep going
Keep going
6
You win!
Keep going
9
You win!
You win!
The test with 6 fails. The condition probably says score > 6, so exactly 6 does not win. It should be score > 5 (6 or more).
4
The same dance is used in four places in a game. How can a custom block help?
Define the dance once as a custom block and call it in all four places. If the dance changes, we change only the definition.