Week 25 · Unit 10: Be a game developer · 3 lessons
1
Score and clock together
Computing 6: Unit 10, pp. 134–149
GoalCombine variables, loops and selection in a game, and trace how two sprites share the same variables.
New words
variable · a named place that stores a value, such as score or time leftselection · a choice in a program, made with if blocksiteration · repeating blocks with a loopoperator · a symbol or block that calculates or compares, such as × or =
Explanation
A real game uses many constructs at once. In the ‘Double it!’ game the Quizzer sprite asks the player to double a random number. It uses variables for the score and the time left, a loop that repeats the questions, and an if–else block that checks the answer. A second sprite, the Clock, runs its own loop. It waits one second and lowers time left by one, twenty times. The two sprites share the variable time left, so the quiz loop stops when the Clock has counted down to zero. The message start clock tells the Clock sprite when to begin. Because the sprites work at the same time, we test each script alone first.
Worked examples
Round
n
n × 2
Player answers
Right?
Score after
1
6
12
12
yes
1
2
4
8
9
no
1
3
9
18
18
yes
2
Trace three rounds of the quiz. The condition is answer = n × 2. Only right answers add to the score.
After
time left
Quiz loop
0 seconds
20
keeps going
1 second
19
keeps going
2 seconds
18
keeps going
19 seconds
1
keeps going
20 seconds
0
stops, because 0 < 1
Trace the Clock sprite. It starts when the message start clock is received. Before each new question the quiz loop checks time left, and it stops once time left is less than 1.
The designer adds a bonus: double points while more than 10 seconds are left. The condition is a strict greater-than, so a time left of exactly 10 gives only 1 point.
Class activity
Quiz-game role play (no computer): three players are the Quizzer, the Clock and the Player. The Clock holds a sand timer or watches a clock and calls ‘one second’ every second. The Quizzer thinks of a number from 1 to 9 and says ‘Double’ followed by the number. The Player answers. The Quizzer keeps the score on paper and says ‘Correct!’ or ‘Try again’. The Clock calls ‘time!’ after 20 seconds and the Quizzer reads out the score. Swap roles. Which job was the hardest to do at the same time as the others? If you have a computer or tablet, build the Quizzer in Scratch.
Practice
1
The Quizzer asks ‘Double 7’ and the player types 14. Then it asks ‘Double 5’ and the player types 12. The score was 3 at the start. What is the score after the two rounds?
Round
Quizzer asks
Player types
1
Double 7
14
2
Double 5
12
7 × 2 = 14, so the first answer is right and the score becomes 4. 5 × 2 = 10, so 12 is wrong and the score stays 4.
2
The bonus rule is ‘if time left > 10, add 2, else add 1’. Find the points for time left = 14, 10 and 3.
14 > 10 is true, so 2 points. 10 > 10 is false, so 1 point. 3 > 10 is false, so 1 point.
3
The Clock script is changed as shown. How long does the clock take to reach time left = 0 if it starts at 20?
The loop runs 20 times and waits 2 seconds each time, so it takes 20 × 2 = 40 seconds.
4
Name two sprites in the ‘Double it!’ game, the variable that they share and the message that connects them.
The Quizzer and the Clock share the variable time left. The message start clock connects them.