Guessing Game

Background

User Input

Sometimes, a game may want the player to provide some text input or choice of dialog. Perhaps you need to name your character or need to choose the right way to respond to an NPC (Non-Player Character) in a game like Fallout 4.

Fallout Dialogue

I feel your pain…

We can obtain player input in a variety of ways. In the Fallout 4 example above, the player has to press a button to select a dialogue option. We can do things like this in Scratch too!

But what if we want to make input more open-ended? Let’s play around with creating guessing game where the player has to guess a number that we randomize.

Useful Scratch Blocks

To make a random integer we can use the pick random block:

Pick Random Block

If we create a variable called number, we can store this random number for the player to keep guessing against!

Set Number to Random

We’ll also need to be ask the player for a guess! We can achieve this using an ask and wait block:

Wait and Ask Block

What ever the player types in response to the question posed (in this case “What’s your name?”) will be stored in a special variable called answer. We can easily change the question by typing in the white text field.

Ending The Game Loop

It’s pretty normal for a game to end if the player achieves a certain condtion. For example, in the Legend of Zelda series, Link can run out of hearts. What happens then?

Game Over

You get slapped with one of these bad babies…

It doesn’t always have to be bad news, though! After all, we also want to make win conditions for players.

To make any of these things happen, we’ll have to end the game loop. There are two ways to do this in Scratch that depend on how you set up your game loop:

Two Game Loops

In both of these Scratch examples, this sprite will keep asking the player to guess the meaning of life until the user enters 42, at which time the background will change to the You Win backdrop I created to signal a victory!

Your Mission

You’re going to create a guessing game where you randomize a number for the player to guess. We’ll want to store this value in a variable and continueously ask the player for a number (what kind of loop(s) will come in handy here?), only stopping once the player has guessed correctly. Of course, when the player has guessed correctly we’ll let them know and end the game!