Haunted House

haunted-houseYou are going to write a Python game where the user walks within the different rooms of a haunted house.

In each room, something will happen. You will decide what happens when the user enters a room. You can use some of the following ideas:

  • Maybe a ghost will appear (ASCII Art using the print command) when the user enters the bedroom,
  • You could have three magic potions on the kitchen table and your program could ask the user to choose one of them. It would then display a different message based on the potion chosen by the user,
  • There could be a secret passage in the library to lead to the kitchen,
  • There could be a random math question to solve in the living room, and if the user does not give the correct answer they lose.

Designing the game


Open and print the following floor plan. Write in each room what you would like your program to do when the user enters the room.

hauntedhouse

Let’s get coding


We have started the game for you with some (but not all) the options for the user to walk from one room to the other.

Complete this game, one room at a time:

Code for the library() function

Here is an example of code for the library() function. Looking at the floor plan of the mansion, you will notice that there is only one door to go back to the hall. However we are going to add a secret passage to lead to the kitchen.

def library():
  print("--- You are entering the library ---")
  print("")
  time.sleep(1)
  print("While looking at the books on the bookshelves, you notice that one of the book is not perfectly aligned with the other books.")
  print("""
                       ┌─┐
   ┌─┐  ┌─┐   ┌──┐     │ │
┌──┤ │  │ ├───┤  ├─────┤ │
│  │ ├──┤ │   ├──┤     │ ├───┐
│  ├─┤xx│ │   │┼┼│ ─── │ │   │
│  ├─┤  │ ├───┼──┤     │ │ ┌─┤
│  │ │  │ ├───┤  │ ─── │ │ └─┤
│  │ │  │ │   │  │     │ │   │
│  │ │  │ │   │  │     │ │   │
└──┴─┴──┴─┴───┤  ├─────┴─┴───┘
              └──┘
  """)
  time.sleep(1)
  option = input("Do you: \n a) pick up this book \n b) Leave this book where it is and go back to the hall").lower()
  if option == "a":
      print("When trying to pick up the book, the book acts as a lever and the bookshelf slides to the left revealing a secret passage.")
      time.sleep(1)
      print("Feeling adventurous, you decide to go through this secret passage and realise that it leads you to the kitchen.")
      time.sleep(1)
      kitchen()
  elif option == "b":
      print("Hum... I do wonder why this book was not perfectly aligned? Anyway, let's go back to the hall...")
      time.sleep(2)
      hall()

Note that for this code to work, you will also need to define a new function for the kitchen:

def kitchen():
  print("--- You are entering the kitchen ---")
  print("")
  time.sleep(1)
  #complete the code here

Using an inventory

We will now add an inventory to our code to let the player collect objects as they progress through the game. To do so we will first initialise a new empty list called inventory using the following line of code that we will place at the very beginning of our code (e.g. on line 3)

inventory = []

Then we will add some code to our kitchen() function for the player to pick up a golden key.
In Python, we can check if a value is in a list using the keyword in.
For instance we will only display the key on the floor of the kitchen, if the user has not already picked it up using the following line of code:

if "Golden Key" not in inventory:

To add a value to a list we will use the append() function as follows:

inventory.append("Golden Key")

Let’s use this code to let the user pick up the key from the kitchen floor.

def kitchen():
  print("--- You are entering the kitchen ---")
  print("")
  time.sleep(1)
  
  if "Golden Key" not in inventory:
    print("""
         ┌──────┐
         │      │
         │      ├────────┬─┬──┬─┐
         │      │        │ ├──┤ │
         └──────┘        └─┘  └─┘
    """)
    print("As you enter into the kitchen, you notice a golden key on the floor and decide to pick it up as it could become useful later on.")
    inventory.append("Golden Key")
    print("Inventory:")
    print(inventory)
    #Add code here to decide where to go next

Then, in other rooms, you will be able to check if the user has the key to for instance let them open a treasure chest.

def bedroom():
  print("--- You are entering bedroom ---")
  print("")
  time.sleep(1)
  print("Hidden under the bed, you find a treasure chest.")
  if "Golden Key" in inventory:
     print("You use your golden key to open it and found a silver crown inside it.")
     inventory.append("Silver Crown")
     print("Inventory:")
     print(inventory)
  else:
     print("The treasure chest is locked and you do not seem to have the key to unlock it!")
  #Add code here to decide where to go next
 

Did you like this challenge?

Click on a star to rate it!

Average rating 3.2 / 5. Vote count: 33

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: , , ,