Home » PythonPi Introductory Course » Python Pi: Lesson 8

Python Pi: Lesson 8

So you’ve finally learned the basics of Python using your RaspberryPi and now we are going to put your skills into use by making an actual game!

Using everything you have learned thus far you must create a text based roleplaying game in which you are to escape a haunted house. You will begin with five lives, every time you lose a life you will have to try again. After you’ve lost five lives the game will automatically exit, leaving the player to have to start again.

 

The Script:

note that this script is case sensitive, if you don’t follow the case, you will not receive the marks. Maybe copy and paste for printing.

1: You have 5 lives
2: You awake in a dark room. you cant see anything. what do you do?
3: -IF USER SAYS- “turn on light” – Then – “The Room Lights Up” – ELSE – “Lose a life”
4: There seems to be a bag of sweets in the corner
5: -IF USER SAYS- “open sweets” – Then – “The bag contains:”
6: [“sherbet lemon”, “refresher”, “drumstick”] – ELSE – “Lose a life” – HINT: Store sweets in list
7: Hmmm these look tasty what to do with these sweets?
8: -IF USER SAYS- “put down” – Then – “Avoided poisonous sweets!” – ELSE – ‘Lose a life’
9: There is a door in the corner of the room…
10: -IF USER SAYS- “open door” – Then – “The door opened, revealing a dark staircase, a light switch is to the side” – ELSE – “The walls closed in on you”
11: “I don’t think those stairs look safe…”
12: -IF USER SAYS- “turn on light” – Then – “The staircase lit up, revealing the passage to downstairs” – ELSE -‘The                    darkness caught you!’
13: “Let’s get out of here!”
14: -IF USER SAYS- “go downstairs” – Then – “The door is in sight, a ghost is following you!” – ELSE – ‘The ghosts grab you!’
15: Quick!
16: -IF USER SAYS- ‘run’ – Then – “You made it out of the ghost house, congratulations!” – ELSE – ‘The ghosts grab you!’

The correct output, if you have made the game correctly and win without losing any lives should look as follows:

ghosthousegame

Tips:

  • Write a function that will check whether you have more than 1 life, if you don’t, you should exit using sys.exit() for this to work you will have to write import sys at the top of your code.
  • Make two global variables, one for the number of lives, perhaps lives = 0 will do. Then another to check whether the user can continue, cont = 0 perhaps.
  • For each loop that is in this program there will be a while loop that checks whether continue is the same as the previous setting, for example the first loop will run whilst cont = 0, the second loop whilst cont = 1. If the user gives the correct input, it will add one to cont. If a user gives a wrong answer, they will lose a life and cont will remain the same, thus given them another chance to play.
  • Make sure you have the function that checks whether someone has enough lives to continue is at the top. You will be calling it from there each time and as we learned in the logic lecture, programs run in the order they written.
  • If you are really struggling, there is a sample in the class room section of a similar game!

Leave a comment

Your email address will not be published. Required fields are marked *