PLIRO / Project
Make your first game: guess the number
A game needs a rule, a choice and a response. In this little guessing game, your player chooses a number from 1 to 10. Your code decides whether the answer is right.
Start the working example01
What you will practise
- Store a number in a variable.
- Compare a player’s input with the secret number.
- Test both outcomes of an if/else decision.
02
What you need
- A computer, Chromebook, tablet or phone with internet and an up-to-date browser.
- A keyboard helps with writing more code; the small input game also works with touch.
- Someone who would like to test your game.
Preparation
- Open the program below and try 7, then a different number.
- This first version has a fixed secret number; it has no randomness or score yet.
- Press Run again after each attempt to play another round.
Project
Step by step
Play the starting version
Open the project and press Run. Enter 7 when asked and send your answer. Read the response in Output.
Try a wrong answer too
Press Run again and choose 3. The program now runs the line under else. Both outcomes are part of the game.
Change the secret number
Choose Code, then Text. Change secret = 7 to secret = 4. Keep it between 1 and 10. Run again and check that 4 is now correct.
Give the game your voice
Change the text inside the quotation marks around You guessed it! Keep the quotation marks and indentation. Try ‘You opened the treasure chest!’
Invite a tester
Ask someone to try a correct and an incorrect answer. Are the question and responses clear? Save your source with the download button inside the project.
Try it yourself
This starting code works. Open the project, choose Code, then Text, to change something, then press Run.
# language: en
let secret = 7
let guess = askNumber("Guess the number from 1 to 10", 1, 10)
if guess == secret:
say "You guessed it!"
else:
say "Not yet. Try again!"
The example result
In the starting version, 7 produces ‘You guessed it!’. Another valid number produces ‘Not yet. Try again!’. You have made input, a game rule and two outcomes.
Discuss what you discovered
Which line would you change for a different game rule? Decide what you want first, then test both outcomes again.
A next step
