Week 25 · Unit 10: Be a game developer · 3 lessons
2
Levels that get harder
Computing 6: Unit 10, pp. 134–149
GoalUse a variable and a condition to move a game up a level, and explain why ‘more than’ is safer than ‘equals’.
New words
level · a stage of a game that has its own rules, speed or difficultythreshold · the value, such as a score, at which something changescompound condition · a condition made from two conditions joined by and or orbackdrop · the picture behind the sprites on the stage
Explanation
Many games get harder as the player scores. We keep the level in a variable. When the score reaches a threshold, an if block sets the level to 2, makes the game harder and broadcasts a message. Other objects receive the message. For example, the backdrop can change to a new picture. Scratch has no ‘5 or more’ block, so we write score > 4 rather than score = 5. A score can jump past a value, for example from 4 to 6, and then a test for exactly 5 would never be true. We also check that the level is still 1. Otherwise the level-up blocks would run again after every question.
Worked examples
Score
Level
Biggest number
Backdrop
0 to 4
1
9
day
5 or more
2
20
night
The game has two levels. The level changes the biggest number that can be asked. The score is the threshold.
Right answers
Score
score = 5?
score > 4?
1
2
false
false
2
4
false
false
3
6
false
true
4
8
false
true
Here the score goes up by 2 for each right answer after starting at 0. A condition that tests exactly 5 never becomes true, but score > 4 does.
This flowchart shows the level check that runs after every right answer. The second part of the condition stops the level-up from running twice.
Class activity
Race to 20 (no computer): you need one die, or number cards 1 to 6, and paper. Each turn a player rolls and adds the number to a running total. Level 1: total below 10, roll one die. Level 2: total 10 or more, roll the die twice and add both numbers. Play with a friend and write the totals in a table. Did a total ever jump over exactly 10? What would happen if the rule said ‘if total = 10’? If you have a computer or tablet, build the level check in Scratch.
Practice
1
In this game the score goes up by 3 for each right answer, starting at 0. What goes wrong with the level check? Write a better condition.
The scores are 3, 6, 9, 12 and so on, so the score is never exactly 10 and the level never changes. Use score > 9 (10 or more) instead, for example <<(score) > (9)> and <(level) = (1)>>.
2
In level 2 the numbers go up to 20. Write the block that picks the number n for level 2.
set [n v] to (pick random (1) to (20)).
3
The designer wants level 3 to begin at 12 points. Write the condition so that level 3 starts only once.
Level
Starts at
Time
1
0 points
20 seconds
2
5 points
15 seconds
3
?
10 seconds
<<(score) > (11)> and <(level) = (2)>>. This means 12 or more. The level must still be 2, so the blocks do not run again.
4
Why must the level-up blocks run only once?
If they ran after every question, the backdrop would be set again and the message sent again each time. It may also restart other scripts that receive the message.