In this workshop, participants will learn how to build their own guessing game using the Scratch programming software. It enables them to discover the different elements and stages of programming
General Objective
Preparation time for facilitator
Competence area
Time needed to complete activity (for learner)
Support material needed for training
Resource originally created in
Introduction
We will program a game in which you have to guess a randomly chosen number. If the player gets it wrong, they get a hint telling them that the number they tried is greater or less than the target number.
Choosing a backdrop
We will first create a backdrop. Choose the backdrop in the bottom right of the creation screen.
Retaining information
To start, the game needs to remember the mysterious number and record the number of attempts the player has made before landing the correct answer. For this we will use variables. Go to the variables tab and click ‘Make a Variable’ and name it ‘Attempts’. Create a second variable called ‘Number’. As you are creating the variables, tick the box ‘For all sprites’ such that the variable is applied to all future sprites and not only the one selected currently. Deselect the variables in the list so they won’t show up on the stage.
Starting the game
The two variables show have their value set at the beginning of the game. The attempts will starts at 0 and the number to guess should be chosen at random by the game. We must also give the player an introduction and an explanation of the rules. To do this, copy the following code in the sprite’s script:
Creating the game
The player needs to be able to enter a number and we need to then be able to record it and say whether it is correct. Then we will need to continue asking the question until they get the right answer. When the player succeeds, the sprite will say how many attempts the player needed! Add the following blocks to the sprite’s script:
Code explanation: the game will ask the player to guess the number until the answer is equal to the correct one. It will increase the attempt counter after each incorrect try. Once the number guessed is equal to the target number, the sprite speaks confirms the correct answer. Then it will say how many attempts were needed. Now integrate the code as follows:
Giving a hint
All that’s left to do is add two tests to determine what hint to give; what should be said when the number is higher than the number to be guessed and what should be said when it is lower. Add the following:
If the player’s response is higher than the target, the sprite will say ‘it’s lower’ and vice versa. Integrate the blocks with previous as follows:
Victory sound effect
The game is now complete. We can add a winning sound effect to improve the presentation. To include sounds, the first step is to import them. For this, go to the sounds tablet and click on the speaker icon in the bottom left.
Choose a sound from the library, for example ‘Triumph’ in ‘Loops’. Add the sound to the script so it plays when the correct answer is found. Try to do it without looking at the solution below!
Going further
Ways to improve the game:
- Play sounds in cases of higher or lower
- Ask the player in what range they would like to search for a number (i.e. 1-10, 1-100, 1-500, etc.)
- More difficult: role inversion. Have the player choose the number and the computer guess!
- Try to find the quickest strategy to get the right answer.