Search code examples
pythonprojectadventure

Can't start the game and don't know how to add inventory


I have to make a text based game for a final project. The goal is to pick up 6 items and to move from room to room. I'm still very new at this and would like some help! I can't seem to call the functions and I don't know how to add an inventory. Here is my current code:

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 showStatus(current_room, inventory, rooms):
    #print the player's current location
    #print the current inventory
    #print an item if there is one


def main():
    #define inventory and dictionary linking other rooms
    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'}
    }
# setting up inventory
inventory = []

# setting the starting room
starting_room = 'Great Hall'

# set current room to starting room
current_room = starting_room

while True:
    print("\nYou are currently in {}".format(current_room))
    move = input("Enter 'go North/South/East/West' to move or 'Exit': ").split()[-1].capitalize()

    # 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]

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



#loop forever

#show game instructions
show_instructions()

Solution

  • This is not the final answer but I wanted to show you the changes you can make to the code to get the program to work.

    This is just restructuring your code. It is not the solution. Once we understand what the problem is, I can help add to this to solve for it.

    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 showStatus(current_room, inventory, rooms):
        #print the player's current location
        #print the current inventory
        #print an item if there is one
    
    #define inventory and dictionary linking other rooms
    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'}
    }
    # setting up inventory
    inventory = []
    
    # setting the starting room
    starting_room = 'Hallway'
    
    # set current room to starting room
    current_room = starting_room
    
    #show game instructions
    show_instructions()
    
    while True:
        print("\nYou are currently in {}".format(current_room))
        move = input("Enter 'go North/South/East/West' to move or 'Exit': ").split()[-1].capitalize()
    
        # 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]
    
        # incorrect move
        else:
            print("You can't go that way. There is nothing to the {}".format(move))
    
    #loop forever