-1

I am creating a text based adventure game for one of my college courses. I need to be able to move through the rooms, pick up all the items, and get to the 'Boss' room to finish the game. It is not finished, but so far I was able to make movement throughout the game with ease, however I am unable to pick up items in the spaces.

My Code:

rooms = {  # create dictionary with list of rooms
    'Center of the Village': {'go north': "Village Scribe", 'go south': "Mage Tower", 'go east': 'Village Dining Hall', 'go west': "Village Tailor', 'item': 'None'"},
    'Village Scribe': {'go east': 'Bridge to the Castle', 'go south': 'Center of the Village', 'item': 'Monster Hunting Guidebook'},
    'Mage Tower': {'go north': 'Center of the Village', 'go east': 'Mage Library', 'item': 'Magic Staff'},
    'Village Dining Hall': {'go north': 'Village Elders', 'go west': 'Center of the Village', 'item': 'Hearty Stew'},
    'Village Tailor': {'go east': 'Center of the Village', 'item': 'Magic Cloak'},
    'Village Elders': {'go south': 'Village Dining Hall', 'item': 'Amulet of Protection'},
    'Mage Library': {'go west': 'Mage Tower', 'item': 'Spell Caster Scrolls'},
    'Bridge to the Castle': {'go west': 'Village Scribe', 'boss': 'Bridge Troll'}
}


def main_menu():  # create main menu prompt when game is launched
    print('---------------------------')
    print("Magicians Adventure Text Game\n")
    print("Movement Commands: "
          "\nType: 'go' Followed by: 'north', 'south', 'east', or 'west' to move through the village. "
          "\nType: 'get' to pick up an item. "
          "\nType: 'exit' to end the game.")
    input('\nPress Enter to continue...\n---------------------------\n')
    return "Good Luck!"


def show_status():  # indicate room and inventory contents
    print('\n-------------------------')
    print('You are in the {}'.format(current_room))
    print('In this room you see a {}'.format(rooms[current_room]['item']))
    print('Inventory:', inventory)
    print('-------------------------------')


print(main_menu())  # print main menu for player
print()

current_room = 'Center of the Village'  # starting room for player

commands = ['go north', 'go south', 'go east', 'go west', 'exit', 'get']  # valid movements

inventory = []

item = rooms[current_room].get('item')


def move_rooms(current_room, directions):  # define the movement sequence between rooms
    current_room = rooms[current_room]
    new_room = current_room[directions]
    return new_room  # use return function to apply new location


while True:

    show_status()

    direction_input = input('What direction would you like to travel? \n \n> ')
    print('--------------------------')

    moves = rooms[current_room]

    if direction_input == 'exit':  # use if statement with exit as key input and break
        print('Thanks for playing!')
        print('---------------------------')
        break

    else:  # add in the below if and elifs for valid and not valid moves from the player

        if direction_input in rooms[current_room]:

            current_room = move_rooms(current_room, direction_input)

        elif direction_input not in commands:
            print('Invalid move.\n')
            print('---------------------------\n')

        elif direction_input not in rooms[current_room]:
            print(f'You can not go that way.')
            print('---------------------------\n')

        elif direction_input == 'get' + rooms[current_room]['item']:

            if rooms[current_room]['item'] not in inventory:

                inventory.append(rooms[current_room]['item'])

            else:
                print('You have already picked this item up.\n')
  • 2
    You have a typo in the `'Center of the Village'` dict. The `'go west'` key should have as its value: `"Village Tailor"`. You've moved the closing quote needed for that value to the end of the `dict`. You need to have: `'go west': "Village Tailor", 'item': 'None'` – quamrana Aug 12 '23 at 21:10
  • Thank you! I had issues with the indentation and formatting of my 'rooms' dictionary before and must have deleted it at some point when trying to fix it. However, now that that is fixed, I see that my code doesn't allow the player to pick up items... – harperwill7 Aug 12 '23 at 21:27
  • 1
    That's a different question. This one should be closed as a typo. And you've introduced a different typo. – quamrana Aug 12 '23 at 21:34
  • Oh okay. I'll look into it some more and ask a new question I guess if I cannot figure my new problem out then. Also, not sure how to close this question, just started using stack overflow today. I do appreciate the assistance with the error I was receiving before, thank you. – harperwill7 Aug 12 '23 at 21:46
  • Does this answer your question? [Why dict.get(key) instead of dict\[key\]?](https://stackoverflow.com/questions/11041405/why-dict-getkey-instead-of-dictkey) – Budyn Aug 15 '23 at 07:31

0 Answers0