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:
            print(key)

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


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
    print(places[0])
    print_objects()


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

        # Check if it's a move
        if len(words) == 1:
            if ans == "items":
                items()
            elif ans == "look":
                print(places[location])
                print_objects()

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

            if verb == "take":
                take_object(noun)

            elif verb == "drop":
                drop_object(noun)

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

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

Main()

Exercise

  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