Guess the Number The computer picks a number
Guess the Number The computer picks a number that we have to guess
Create the Character Select a character to use in our Guess the Number game. This is done by creating a new sprite and selecting a costume from the Scratch library of images.
Creating the Variables allow us to record information through our computer program. For this game we need two variables: One to record the computer’s choice and another to record the number of guess attempts the player has made. This is done from within the DATA section in Scratch.
Making a Guess Let’s start building the logic for our game. When the green flag is clicked we need to reset our Guess count variable and prompt the computer to secretly pick a number between 1 and 100. Use the SAY block to prompt the player to enter their guess as shown.
The Repeat Loop This REPEAT UNTIL loop will allow us to keep guessing the computer’s number until either the number is guessed correctly or the player has used up all their attempts - in this case the player is allowed 5 attempts. Don’t forget that the ASK block shown below saves our guess as a separate variable called ANSWER.
Making Decisions Deciding whether the player’s guess is too high, too low or correct is achieved using CONDITIONS. Use the IF THEN block to do this. Don’t forget to increase the Guess count variable by 1 after every attempt.
The End of the Game When the REPEAT UNTIL loop is no longer running, then whatever we place underneath it will run AFTER we’ve lost the game or won. Let’s place a SAY block to confirm to the player the number they were trying to guess.
Let’s Guess the Number!
- Slides: 8