Today we are building a number guessing game in scratch. The aim of the game is to guess the number that the computer picks.
We will need a sprite and a nice backdrop to make our scene look great. I’ve chosen a nice-looking fish, sitting in front of a brick wall, a regular fish out of water story.
The first part of our game involves us telling the user what the game is about. We need to have our sprite say a few words when the ‘Green Flag’ is pressed.
We first drag over the ‘Green Flag Hat Block’.
Let’s connect some ‘Say’ blocks from the ‘Looks’ tab.
My fish is going to say ‘Hello’ and then ‘Let’s play a game’.
Let’s test to make sure it’s work the way we think it should.
Before we ask the user for their guess, we need the computer to pick a number and remember it. To get the computer to remember our number, we need to use a variable. A variable is just a spot where the computer can store something.
We add a variable from the data tab:
Give this variable a name like ‘NumberToGuess’, this will be the number that the computer has picked for the player to guess.
We are going to set the variable to a random number, you can pick how hard your game is going to be by changing the range of the random numbers. I’ve picked 1 – 20, meaning that the computer could pick a number between 1 and 20.
After we have stored our number in a variable, it’s time to ask the user for their first guess.
We use the blue ‘Ask’ block for this (under the sensing tab). The block will ask the user a question and wait for the user to respond by typing something into the text box.
Now that the user has given us their guess, we need to compare it with our stored number to see if it’s higher or lower.
We can use the blue ‘Answer’ block to provide us with the number given by the user.
Since we want to test the number, we are going to use the If block:
Anytime we want to test something, we should be thinking about using the If Then block.
To see if the number is greater than our variable, we can use the green operators tab and select the greater than sign (>).
We can place the blue answer block in the left hand side of the green > block and the orange variable in the right hand side.
Any code that is inside the “if” block will be run if the user’s answer is greater than the picked out number.
Test this out, use the checkbox to show the number on the screen. This will show what number the computer picked, this will help you see if your code runs properly. Make sure you type a number larger than what the computer picks.
Now that we have the greater than part handled, now is the time to add the case when the user picks a number that is less than the number picked by the computer.
Use the less than green block:
Test it out again. Make sure it works how it should.
Now the only other possible case is if the player picks the same number that the computer picked. In this case, we are going to use the equals sign.
Test this out. What is missing from our game at this point?
We only give the player a single chance to guess the number. We really need to give the player more chances.
We want the player to always be guessing a number until they pick the correct number. If we always want to be doing something, then we should be using the forever block.
Test this out.
Now our game doesn’t end, even if the user selects the correct number. Let’s add a little bit more to make the game interesting for our player.
Let’s keep count of how many guesses they take before they reach the correct number. Add a new variable called ‘Guesses’, so we now have two variables:
We need to make sure that our game resets the number of Guesses to zero when it first runs. Under the hat block, add the Set block:
We made it 1, because it will take at least one guess to pick the correct number.
Now we want to stop the game if the player picks the correct number, so add the ‘Stop All’ block inside the equals if block section.
Next, we need to tell the player that they need to pick another number, so ask them to try again. We also need to make sure that the first ‘Ask’ block is removed from the ‘forever’ block, we only want to ask that question once, after the first attempt we will say ‘try again’.
Finally, we need to give our players some reward for picking the correct number. This could be a cheering sound or maybe it could be some really cool visual trick.
I’ve done something cool to my fish:
We need to make sure this is just above the ‘Stop All’ block. My final code is shown below, but try to make it interesting.