Wikijunior:Raspberry Pi/Scratch Game – Math Quiz

Description
This is a Scratch game that challenges the player to a series of multiplication questions.

They have to provide the correct answer before a dropped Ball reaches the bottom.

The catch is, as they play the Ball drops faster and faster.

Walkthrough
Delete the "Scratch" character sprite, add a new sprite from the library and choose the item that will drop (for example the "baseball" from the Things folder), from now on I will call this the "Ball" sprite.

Click on the Stage, select "Backdrops" and choose a suitable stage.

From the Variables category, create the following 5 variables:


 * – This is the game level and will control how fast the Ball drops.
 * – This is the player's score.
 * – This indicates when the game is running.
 * – this is the first number to multiply.
 * – this is the second number to multiply.

1. Create the Ball Drop Sequence
First, we are going to create the Ball drop sequence:

Select the Ball sprite, and check that the "scripts" tab is selected.

From the Control category, select the "when I receive …" block. Then select the drop-down and enter a message of "startDrop".

Move the Ball so that it is very near the top of the "stage", but not touching it. From the Motion category, drag "go to x: … y: …" and lock it below the event placed above (your x: and y: might be slightly different from mine).

We want to move the Ball down the screen until it reaches the bottom. We will use a "forever if" loop to do this.

From the Control category, drag "repeat until …" and lock it below the "go to …" block.

From the Sensing category, drag "touching …" and place it in the condition area of the "repeat until … " block. Select "edge" from the drop-down list of the "touching …" condition.

From the Motion category, drag the "change y by …" block inside the repeat loop. Change the value to "-10" (this is how far the Ball will move down each time around the loop).

From the Control category, drag the "wait 1 secs" block and place it inside the repeat loop after the "change y by -10" block.

To calculate the speed of the Ball we will use the following formula:

This will make the Ball drop faster as the level increases.

From the Operators category:

Drag "… + …" and use it to replace the (1) of the "wait … secs".

Now drag "… * …" to the second part of the "… + …".

Finally, drag "… - …" to the second part of the "… * …".

Now fill in the values for our formula above. (You might want to try changing these values later to make the game easier or more difficult.) These are all actual numbers except for the " " variable that you need to get from the Variables category.

If this "repeat until …" loop finishes then the Ball has reached the bottom and the game is over. From the Looks category, select the "say … for 2 secs" block and lock it after the repeat loop, changing the " " to a " ".

Then from the Operators category, drag the "join hello world" and place it inside the "Say" block. Replace the "hello" with "Game Over, you scored: ". Replace the "world" with the " " variable from the Variables category.

From the Variables category, drag "set … to …" and lock it under the "say …" block. From the drop-down select " " and leave the value as " ". This will indicate that the game has finished.

2. Test the Ball Drop Sequence
From the Control category, drag "when space key pressed" to the script area.

From the Variables category, select "set … to …" and lock it below "when space key pressed".

From the drop-down select " " and leave the value as 0 (this represents the starting level). Now, from the Control category, drag "broadcast startDrop and wait" and lock it under the "set level to 0" block.

Now pressing the space bar will run your Ball drop sequence. Try it out, this is the slowest speed. Will you be able to answer a maths question before it gets to the bottom?

Now change the value of the "set level to 0" block to " " (this represents the highest level, the fastest Ball drop) – Do you still think that you'll have enough time? Well, you’ll find out later.

3. The Math Question Sequence
Start a new script by dragging the "when the button clicked" event from the Control category, to the script area.

We need to start by resetting the variables. From the Variables category, drag three "set … to …" blocks under the "when clicked" block. From the drop-down, change the first one to " " and the second one to " ", leaving both of these at zero. Set the drop-down of the third one to " " and set it to " " (this indicates that the game is active).

Now drag a "repeat until …" block and place it below the variables. This is the main loop for each question.

Drag a "… = …" block from the Operators category. From the Variables category, drag the " " variable to the left-hand side of the equals operator. Set the right-hand side to " ". Note that we have just set  to " " and will keep our quiz running until   = " ".

We now want to choose two numbers for our quiz question. Drag two "set … to …" blocks and place them inside the "repeat until loop". Select " " in the first drop-down, and " " in the second drop-down. Now drag a "pick random … to …" from the Operators category, to each "to" value in those blocks. Set the values to " " and " " for each "pick random" (" " would be too easy).

Now drag a "broadcast …" from the Control category, and select "startDrop" in the drop-down. This will start the Ball falling! Now drag a "ask … and wait" block from the Sensing category, and place it under the broadcast message. From the Operators category, drag a "join …. …." block and place it in the "ask" block, then place another "join …" on the right-hand side of the first join, and yet another "join …" in the right-hand side of that one. We can now join four things together. Set the first "join" cell to the words "What is " (put an extra space after the word "is"). Drag the variable " " to the next cell of the join. Then put the word " times " in the third cell (with a space before and after it), and finally drag the variable " " to the last cell. This script will now wait for the player to answer the question, but remember that the other script is continuing to make the Ball drop! Time will be running out for the player. We need to keep asking the player for an answer until they get it right, so drag another repeat loop and place it under the "ask" block but inside the existing "repeat until …" loop. There are two reasons why we must stop this loop; either the player is out of time, or the player has got the correct answer. Drag an "… or …" block from the Operators category, and place it in the "repeat until" condition box. Now drag an "… = …" block into each side of the "… or …" block.

For the first reason to stop this repeat loop, we want to check for when the " " variable is " " (zero).

For the second reason to stop this repeat loop, we want to check when the answer is correct. Drag the "answer" element from the Sensing category to the left side and drag a "… * …" element from the Operators category, to the right side. Put the " " and " " variables in the "… * …" element.

When the player gets the answer wrong we want to tell them and ask them the question again. Right-click on the "ask.." section that you’ve already created and select "duplicate". Place it in a clear area. Note that this has also duplicated the "repeat loop", which we don't want, so detach that and discard it. Change the text in the first cell so that it reads "Try again, what is ", now place it inside the repeat loop. Our program will now keep asking until either the time runs out, or gives the correct answer. Now we need to check to see if the player has got the answer right. From the Control category, drag an "if … then" block and place it under the inner repeat loop, but inside the outer repeat loop. From the Operators category, drag a "… = …" block into the "if" condition.

From the Sensing category, drag the answer into the left side of the "… = …". From the Operators category, drag a "… * …" into the right-hand side. Now from the variables place "first" and "second" on either side of the multiple block.

If the answer is correct, then we want to increase the score. From the Data category, drag a "change … by …" inside the "if" block, select " " from the drop-down and set the amount to " ".

We also need to increase the level (which will make the Ball drop slightly faster), but only up to the highest level possible. From the Control category, drag an "if … then" block and place it under the "change score …" block, but still inside the existing "if" block.

The level can only be increased to a maximum value of 20, so from the Operators category, drag a "… < …" to the "if" condition. From the Variables category, drag the " " variable to the left-hand side of the "… < …" and set the right-hand side to " ". From the Variables category, drag a "change … by …" inside the "if" block. select " " from the drop-down and set the amount to " ".

You can now test your game, what score can you get? How can you make the game easier or more difficult?

4. Further Activities
Add sounds when the player gets a question wrong or right and when they lose the game.

Change the game so that it asks you a different maths question (for example to sum three numbers).

ScratchGame-MathQuiz-Pi.pdf
The for this tutorial is available on Wikicommons.