Adventure Game 6: Using Objects

We’ve set up the basics of the game, but it’s not much fun. All you can do is wander round picking up and dropping objects. We need to allow the user to use the objects. We’re going to add two ways of using objects.

  • If the player uses the spade in the clearing in the forest they will dig up gold.
  • If the player uses the spade in the cave they will open a path to an underground lake.

Note that I’ve added new locations to the map, the same ones we added in Exercise 1

places = ["A clearing in a forest", "An old wooden cabin", "A dark cave", "The top of a Hill", "Deep in the Forest", "An Underground Lake", "Caught in the Brambles"]
moves = [{"n": 1, "s": 2, "e":4},     {"s": 0, "e":3},       {"n": 0} ,     {"w": 1, "s":4},    {"n":3,"w":1, "e":6}, {"w": 2},             {"w": 4}]
objects = {"spanner":0,  "lockpick":0, "spade":2}
location = 0

def print_objects():
    for key, val in objects.items():
        if val == location:

def items():
    print("You are carrying: ")
    for key, val in objects.items():
        if val == 99:

def take_object(noun):
    for key, val in objects.items():
        if key == noun and val == location:
            print("Got it!")
            objects[noun] = 99

def drop_object(noun):
    for key, val in objects.items():
        if key ==noun and val == 99:
            print("Dropped ", noun)
            objects[noun] = location

def use_object(noun):
    if noun == "spade" and location == 0:
        objects["gold"] = 0 # create the gold
        print("You dug up some gold!")
    if noun == "spade" and location == 2:
        moves[2] = {"n": 0, "e":5}
        print("You've opened up a tunnel, leading east...")

def Main():
    ans = ""
    global location

    while ans != "bye":
        ans = input("What now?")
        words = ans.split()

        # Check if it's a move
        if len(words) == 1:
            if ans == "items":
            elif ans == "look":

            elif ans in moves[location]:
                location = moves[location].get(ans)
                print("I can't move that way")
            verb = words[0] # e.g. Take or Drop
            noun = words[1] # e.g. hammer or spanner

            if verb == "take":

            elif verb == "drop":

            elif verb == "use":
                if (objects[noun]==99): # check holding object

                print("I don't understand what you mean")



  1. Copy the code into your IDE. Move around the map to see the new locations
  2. Find the spade and use it to dig up gold in the forest
  3. Now dig in the cave and check to see the path to the lake opens up.
  4. Look at the function use_object(noun). What’s the purpose of the statement objects["gold"] = 0
  5. Look at the moves list (line 2). What has been added to the first dictionary in the list?
  6. Look at moves[2] (remember, this is the third item in the list). At the moment this is {“n”: 0}. What does that mean?
  7. Look at the function use_object(noun). What is the purpose of the line moves[2] = {"n": 0, "e":5} (hint, this is only called if the noun is a spade)
  8. Add the following objects to the map: telescope in the wooden cabin, cutters deep in the forest.
  9. Add code so if the user uses the telescope on top of the hill they see a message written on a sign saying “There is treasure in the forest”
  10. Add code so if the user uses the cutters when caught in the brambles, treasure appears.

Leave a Comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s