Eddie
Eddie

Reputation: 1

Python Text Adventure Game

I am creating a text adventure game for IT 140 and I am having a hard time trying to show items in each room. The object is to go from room to room with directional commands and you encounter see an item, pick up the item and keep moving until you have all items to then encounter the villain.

I am currently stuck with a def and having an output stating what is in the room you are currently in and moving then when you move to the next room, you see the item that is in that room and collect them.

this is what I have. Any help would be greatly appreciated! Thank you!

rooms = {
    'Main Cavern': {'South': 'Healing Cavern', 'North': 'Flight Cavern', 'West': 'Telekinesis Cavern',
                    'East': 'Sacred Cavern'},
    'Telekinesis Cavern': {'East': 'Main Cavern', 'Item': 'Telekinesis'},
    'Flight Cavern': {'South': 'Main Cavern', 'East': 'Primordial Cavern', 'Item': 'Flight'},
    'Primordial Cavern': {'West': 'Flight Cavern', 'Item': 'Gem'},
    'Strong Cavern': {'South': 'Sacred Cavern', 'Item': 'Strong'},
    'Sacred Cavern': {'North': 'Strong Cavern', 'West': 'Main Cavern', 'Item': 'Ancient Book'},
    'Healing Cavern': {'North': 'Main Cavern', 'East': 'Cursed Cavern', 'Item': 'Healing'},
    'Cursed Cavern': {'West': 'Healing Cavern', 'Item': 'Villain'}
}

current_room = 'Main Cavern'
inventory = []


def get_new_room(current_room, direction):
    new_room = current_room
    for i in rooms:
        if i == current_room:
            if direction in rooms[i]:
                new_room = rooms[i][direction]
    return new_room


def get_item(rooms):
    return rooms[current_room]['Item']



def show_instructions():
    # print a main menu and the commands
    print("Welcome to the Power Balance Text Adventure Game!")
    print("")
    print("YOu must obtain the Gem and collect 4 abilities to defeat the villain and win the game.")
    print("")
    print("Move commands: go North, go South, go East, go West")
    print("")
    print("Add to Inventory: get, item name/ability name")
    print("")
    print("<< >< >< >< >< >< >< >< >< >< >< >< >< >< >< >< >< >>")


show_instructions()
Inventory = []
items = ['Telekinesis', 'Flight', 'Gem', 'Strong', 'Ancient Book', 'Healing', 'Villain']
while 1:
    print('\nYou are in the', current_room)
    print('Inventory:', Inventory)
    item = get_item(rooms)
    print('You see a ', item)


# will ask user which direction they would like to go
    direction = input('\nEnter which direction you\'d like to go or enter exit to end the game: ')
    direction = direction.capitalize()
# will display thank you for playing when the user exits
    if direction == 'Exit':
        print('\nThank you for playing!')
        exit(0)
# will display thank you for playing when the user exits
    if direction == 'East' or direction == 'West' or direction == 'North' or direction == 
    "South":
        new_room = get_new_room(current_room, direction)
# an invalid direction by text or if there is no connecting room, will display 'invalid direction'
        if new_room == current_room:
            print('\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
            print('\nThere\'s no cavern in that direction, try again!')

        else:
            current_room = new_room
    else:
        print('\nInvalid direction!!')

Upvotes: 0

Views: 784

Answers (1)

eliaseraphim
eliaseraphim

Reputation: 109

After running the code myself it seems like the error is occurring here:

Traceback (most recent call last):
  File "main.py", line 50, in <module>
    item = get_item(rooms)
  File "main.py", line 27, in get_item
    return rooms[current_room]['Item']
KeyError: 'Item'

A few notes before discussing the error:

  1. Some of the names are being shared globally. For example current_room is a global variable, but also is being passed as an argument. Perhaps you should rename that argument in the function.
  2. There's no need to pass rooms to get_item as it's already accessible in global scope. This kind of mirrors the point made in 1.

For the error, if you check current_room before the line: return rooms[current_room]['Item'] you'll find that it's set to 'Main Cavern'. In your dictionary 'Main Cavern' has no key 'Item', and thus it can not be accessed. Perhaps you could add the 'Items' key to 'Main Cavern' but make the value None if you wish for there to be item there. You could also create an if Statement to check if they key exists before accessing.

Upvotes: 0

Related Questions