Week 24 · Unit 10: Be a game developer · 3 lessons
2
A quiz that keeps score
Computing 5: Unit 10, pp. 129–144
GoalFollow an algorithm for a quiz that adds points for a right answer and takes points away for a wrong one.
New words
score · a variable that keeps count of the points in a gameanswer · the Scratch reporter that holds what the player typedask and wait · a Scratch block that shows a question and waits for a typed replystart value · the number that a variable is given when the program begins
Explanation
A quiz game needs a variable for the score. When the green flag is clicked, the program first sets the score to its start value. Then it asks a question and waits. What the player types is held in the reporter answer. An if … then … else block compares the answer with the right one: when they match, the score goes up, and when they do not match, it goes down. In Scratch you can write set score to (score + 3), or use the block change score by 3. Both give the same result, and Scratch ignores capital letters in this test.
Rule
Value
start value
5 points
right answer
+3 points
wrong answer
−2 points
Worked examples
Step
Instruction
1
Start when the green flag is clicked
2
Set score to 5
3
Ask: ‘How many legs has a spider?’ and wait
4
If answer = 8, then set score to score + 3 and say ‘Right!’
5
Else set score to score − 2 and say ‘Not quite’
6
End
This is the algorithm for a Parrot that asks how many legs a spider has. The right answer is 8. If the player types 8, the score changes from 5 to 8. If the player types 6, the score changes from 5 to 3.
Here is the same algorithm in Scratch. The condition is the equal-to block. Each path changes the score in a different way.
After
Result
Calculation
Score
start
5
question 1
right
5 + 3
8
question 2
wrong
8 − 2
6
With two questions, the score carries on from one question to the next. The player is right on question 1 and wrong on question 2. Each row starts from the score of the row above.
Class activity
Paper quiz master (3 players, paper and a pencil). The quiz master reads out 6 easy questions from a list. One player answers. The score keeper writes the start value 5 on a card and then changes it after each answer: add 3 for a right answer and take away 2 for a wrong one. Write a new value on a new line each time. What is the final score? Now change the rule to +4 and −1. Does the same set of answers give a higher score? (If you have a computer or tablet, build the Parrot program in Scratch.)
Practice
1
The Tortoise starts with 10 points. The player types 8. What is the score at the end? What would it be if the player typed 9?
With 8 the answer is wrong, so the score is 10 − 3 = 7. With 9 the answer is right, so the score is 10 + 4 = 14.
2
A quiz starts at 5 points, adds 3 for a right answer and takes 2 away for a wrong answer. The player is right, wrong, wrong. What is the final score?
After
Result
Score
start
5
question 1
right
?
question 2
wrong
?
question 3
wrong
?
5 + 3 = 8, then 8 − 2 = 6, then 6 − 2 = 4. The final score is 4.
3
Why does the program set the score to a start value when the green flag is clicked?
Without it, the score from the last game would still be in the variable. Every game must begin from the same number.
4
A player gives a wrong answer but the score goes up. Which block must be changed, and to what?
The block in the else part: ‘change points by 2’ should be ‘change points by −2’, so that a wrong answer takes points away.