We want to end up with a quiz game that asks questions and keeps track of the score
Start a new project in Scratch and remove the scratch cat.
We would like something to ask the questions so choose a new sprite from the Scratch library. I chose Avery but you can choose anything you want.
Choose a backdrop to give your quiz a nice background:
Now let’s do some normal setup tasks to make sure the questioner is in the correct position for your background. I added three blocks:
- When Green Flag clicked
- go to x: -158 y: -71
Now lets get the sprite to ask a question. From the ‘Sensing’ blocks use the block for ‘ask …. and wait’. Drag the block onto your sprite and amend the text of the question. I have asked a question about how many hours there are in a day.
Next add an ‘if…else’ block under the question to determine if the user answered the question correctly:
Under the green ‘operators’ blocks use an ‘equals’ block inside the if statement and drag the blue ‘answer’ block to the left and type ’24’ into the space on the right. An example of this is shown below:
Next we want to add a ‘variable’ to track the score for the person playing the quiz. A ‘variable’ is what coders use to store something during the running of your code. It is usually something that varies as the program runs.
Click the ‘Make a variable’ button under the orange ‘Variables’ section
Give your variable the name ‘score’ and click OK, as shown below:
Next we need to increase the score when the user guesses the correct answer so in the orange ‘Variables’ section drag a block for ‘change my variable by 1’ and add it to the ‘if’ block as shown below:
(don’t forget to change ‘my variable’ to ‘score’)
Now test your code by clicking the green flag. Try entering the correct answer and then run the code again to try entering the wrong password.
Have you noticed any ‘bugs’ ?
You might have noticed that your score variable does not reset back to zero when you start the game. From the orange ‘Variables’ section drag a block for ‘set my variable to 0’ to the top of your code to ensure the score is reset to zero at the start of every game:
Now you might want to test your code again. Is it working as you would expect?
We could now add another question so right-click on the ‘ask’ block and choose duplicate.
Now amend the question and answer for the second question. An example is shown below:
Finish the game on your own
You now have the basis of a quiz game but you could make it much better. Can you improve it to add:
- add 8 more questions to make a quiz with 10 questions
- add a ‘results’ section at the end to tell the user their score out of 10
- add pictures for each question. Question 2 could have a picture of Paris to help the user. You could change the backdrop or add a new sprite.