More complex maths games 1
Quiz time with Scratch - Lesson 4
Objectives
- Devise, test and refine effective control sequences incorporating conditional statements, broadcasts, inputs and outputs
- Use arithmetic operators, if statements, and loops, within programs
- Design algorithms that use repetition and two-way selection i.e. if, then and else
Lesson Resources
Lesson 1 - Variables and operators
Lesson 2 - Using lists
Lesson 3 - Memory games with lists
Lesson 4 - More complex maths games 1
Lesson 5 - More complex maths games 2
Lesson 6 - Word games
Introduction
This week we return to maths games, but with a bit more complexity than the times tables game we made in lesson 1.
This game combines addition and subtraction questions (randomly) and introduces how to send random broadcasts. It also makes use of variables, lists, operators, inputs, and if/else blocks.
Introduce our task for this lesson to the students; Can you create a game that asks ten random addition or subtraction questions, marks them and returns the player’s score to them at the end?
They will get some help through the first part of the project (if needed) but must try to complete the rest of the project on their own (or with a partner).
Setting the variables
To begin we need to create three variables. Two that will represent the numbers in our questions, we’ll call these variables A and B. We also need a variable to represent the player’s score.
Using broadcasts and lists
Later the students will create two separate stacks of code for the quiz, one that asks addition questions and another that asks subtraction questions. Both of these will need to be triggered by broadcasts. However, it is not possible to send random broadcasts with the broadcast block on its own as the only options you have from the drop down box are the names of individual broadcasts.
So to make this possible we will use a list, which allows a number of variables to be added to, and chosen from it. We’ll call our list ‘broadcasts’ and add two items to the list; ‘add’ and ‘subtract’.
Starting the game
Now we’re ready to build the first main stack of code to start our game:
It begins by resetting any previous score to 0, and then introduces the game with a few ‘Say’ blocks.
It then sends ten broadcasts (one at a time, each one must reach a completed stack of code before the next one is sent), and each one should later trigger a question to be asked. The broadcast block picks number 1 (add) or number 2 (subtract) randomly from the list we made and sends that word from the list as the broadcast.
After the ten questions have been asked, we have another ‘say’ block to report the score variable back to the player to tell them how they did.
So we now need two ‘when I receive’ blocks. (note - it’s vital that any ‘when I receive’ blocks used, match the words used in the list precisely for the broadcast to work).
From here they need to create the code for the addition and subtraction stacks of code. If needed, remind them of the multiplication quiz they created in lesson 1 as it can form the basis of what they need today:
Here is one solution:
Questions to consider:
- What should the number range be in the ‘pick random’ part of ‘set a to’ and ‘set b to’ blocks?
- Why do the ranges need to differ in the subtraction code?
When either of these scripts ends it will trigger the next broadcast to be sent until ten questions have been asked.
The final steps are to permanently hide the variables A and B and the ‘broadcasts’ list as there is no reason for any of this to be showing on the screen for the player to see, and it looks nicer without it! Rather than using blocks in the code to hide these, you can just untick the boxes next to the variable/list names in the block palette.
Plenary
At the end of the lesson review how they tackled the problem and have a look at some of their games. Ask the class:
- What did you alter in the code from the multiplication quiz to make it work for addition and subtraction?
- How did you ensure you didn’t get a subtraction question that required a negative number answer?
- Did you add anything else to your quiz to improve it?
- Did you find any bugs in your code as you were working?
- How did you fix them?