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

A complete game

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.

Quizzerwhenclickedsetscoreto0settime leftto20broadcaststart clocksetntopick random1to9askjoinDouble nand waitchangescoreby1sayCorrect!for1secondssayTry againfor1secondsifanswer=n*2thenelserepeat untiltime left<1sayjoinScore: scorefor3secondsClockwhen I receivestart clockwait1secondschangetime leftby-1repeat20
Worked examples
Roundnn × 2Player answersRight?Score after
161212yes1
2489no1
391818yes2
Trace three rounds of the quiz. The condition is answer = n × 2. Only right answers add to the score.
Aftertime leftQuiz loop
0 seconds20keeps going
1 second19keeps going
2 seconds18keeps going
19 seconds1keeps going
20 seconds0stops, 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.
changescoreby2changescoreby1iftime left>10thenelse
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?
RoundQuizzer asksPlayer types
1Double 714
2Double 512
2
The bonus rule is ‘if time left > 10, add 2, else add 1’. Find the points for time left = 14, 10 and 3.
3
The Clock script is changed as shown. How long does the clock take to reach time left = 0 if it starts at 20?
when I receivestart clockwait2secondschangetime leftby-1repeat20
4
Name two sprites in the ‘Double it!’ game, the variable that they share and the message that connects them.