Having trouble setting up inventory

191 Views Asked by At

I'm working on a text-based game where the player had to find 6 items in different rooms before running into the boss or they die. I have the items set in the dict with the rooms but I don't know how to pull from it as the player moves around. What I have currently have has the player able to add things to the inventory but then it's stuck in a permanent loop. I am very new at this and I am having trouble connecting things together. Here is the whole thing with comments.

rooms = {

    'Entry Way': { 'North': 'Stalagmite Cavern'},
    'Stalagmite Cavern': {'North': 'Grand Cavern', 'South': 'Entry Way', 'item': 'torch'},
    'Grand Cavern': {'North': 'Hallway', 'East': 'Armory', 'West': 'Bedroom', 'South': 'Stalagmite Cavern', 'item': 'cross'},
    'Armory': {'North': 'Treasure Trove', 'West': 'Grand Cavern', 'item': 'Stake'},
    'Treasure Trove': {'South': 'Armory', 'item': 'silver'},
    'Bedroom': {'North': 'Storage', 'East': 'Grand Cavern', 'item': 'elaborate comb'},
    'Storage': {'South': 'Bedroom', 'item': 'mirror'},
    'Hallway': {'North': 'Cliff Top', 'South': 'Grand Cavern'},
    'Cliff Top': {'South': 'Hallway', 'item': 'Orla'}
}

def show_instructions():
   #print a main menu and the commands
   print("Thousand Year Vampire")
   print("Collect 6 items to defeat the vampire or be destroyed by her.")
   print("Move commands: go South, go North, go East, go West")
   print("Add to Inventory: get 'item name'")

def show_status():
    print(current_room)
    print(inventory)
    #print the player's current location
    #print the current inventory
    #print an item if there is one

# setting up inventory
inventory = []

def game():
    inventory = []
    # simulate picking up items
    while True:
        item = input()
        if item in inventory:  # use in operator to check membership
            print("you already have got this")
            print(" ".join(inventory))
        else:
            print("You got ", item)
            print("its been added to inventory")
            inventory.append(item)
            print(" ".join(inventory))

# setting the starting room
starting_room = 'Entry Way'

# set current room to starting room
current_room = starting_room

#show game instructions
show_instructions()

show_status()


while True:
    print("\nYou are currently in the {}".format(current_room))
    move = input("\n>> ").split()[-1].capitalize()
    print('-----------------------------')

    # user to exit
    if move == 'Exit':
        current_room = 'exit'
        break

    # a correct move
    elif move in rooms[current_room]:
        current_room = rooms[current_room][move]
        print('inventory:', inventory)


    # incorrect move
    else:
        print("You can't go that way. There is nothing to the {}".format(move))

#loop forever until meet boss or gets all items and wins
2

There are 2 best solutions below

0
On BEST ANSWER

A nice start from you, Here is some modification as a small push to inspire your thoughts, and you can complete yourself or change according to the scenario you prefer- but discover the changes your self :) I have tested this code:

rooms = {

    'Entry Way': { 'North': 'Stalagmite Cavern'},
    'Stalagmite Cavern': {'North': 'Grand Cavern', 'South': 'Entry Way', 'item': 'torch'},
    'Grand Cavern': {'North': 'Hallway', 'East': 'Armory', 'West': 'Bedroom', 'South': 'Stalagmite Cavern', 'item': 'cross'},
    'Armory': {'North': 'Treasure Trove', 'West': 'Grand Cavern', 'item': 'Stake'},
    'Treasure Trove': {'South': 'Armory', 'item': 'silver'},
    'Bedroom': {'North': 'Storage', 'East': 'Grand Cavern', 'item': 'elaborate comb'},
    'Storage': {'South': 'Bedroom', 'item': 'mirror'},
    'Hallway': {'North': 'Cliff Top', 'South': 'Grand Cavern'},
    'Cliff Top': {'South': 'Hallway', 'item': 'Orla'}
}

def show_instructions():
   #print the main menu and the commands
   print("Thousand-Year Vampire")
   print("Collect 6 items to defeat the vampire or be destroyed by her.")
   print("Move commands: go South, go North, go East, go West")
   print("Add to Inventory: get 'item name'")

def show_status():
    print(current_room)
    print(inventory)
    #print the player's current location
    #print the current inventory
    #print an item if there is one

# setting up inventory
inventory = []

def game(item):
    # simulate picking up items
    if item in inventory:  # use in operator to check membership
        print("you already have got this")
        print(" ".join(inventory))
    else:
        print("You got ", item)
        print("its been added to inventory")
        inventory.append(item)
        print(" ".join(inventory))

# setting the starting room
starting_room = 'Entry Way'

# set current room to starting room
current_room = starting_room

#show game instructions
show_instructions()

show_status()


while True:
    print("\nYou are currently in the {}".format(current_room))
    if 'item' in rooms[current_room]:
        game(rooms[current_room]['item'])

    move = input("\n>> ").split()[-1].capitalize()
    print('-----------------------------')

    # user to exit
    if move == 'Exit':
        current_room = 'exit'
        break

    # a correct move
    elif move in rooms[current_room]:
        current_room = rooms[current_room][move]
        #print('inventory:', inventory)

    # incorrect move
    else:
        print("You can't go that way. There is nothing to the {}".format(move))

#loop forever until meeting boss or gets all items and wins

Good Luck

6
On

If each room only has one item, I think that the following line in the game() function should be removed

while True:

since that will cause an infinite loop with the first three printouts being "You got..." and "... added to inventory ..." and "[contents of inventory]" but the next printouts will be "you have already got this" and "[contents of inventory]" over and over again.