dimanche 17 octobre 2021

Movement and pick up Item based game (possible if-else statement problem)

I'm a little new to python and doing and assignment in my class to make a text based game that moves from room to room with North South East and West. In all but the first room there are Items that the player must pick up. The commands for movement is 'move "direction"', the commands to get the item is 'get "item"'. I have it set up that the rooms are in a dictionary and a nested dictionary for each room that correlates to neighboring rooms and items in each room. Here is my current working code.

def show_instructions():
print('Collect 8 items to win the game and defeat the beast')
print('Move Commands: move North, move South, move West, move East, or Exit to leave the game')
print('Pickup items: get item, ex. get sword\n')

#Dictionary for rooms and Items
def main():
rooms = {
    'Shed' : {'West': 'Well Room', 'item' : 'well room West of you'},
    'Well Room' : {'South' : 'Sewers', 'East' : 'Shed', 'item' : 'Healing-Potion'},
    'Sewers' : {'East' : 'Crawl Space','North' :'Well Room', 'item' : 'Armor'},
    'Crawl Space' : {'East' : 'Cavern', 'West' : 'Sewers','item' : 'Sword'},
    'Cavern' : {'South' : 'Dungeon Hall', 'West' : 'Crawl Space', 'item' : 'Shield'},
    'Dungeon Hall' : {'East' : 'Hidden Room', 'West' : 'War Room', 'North' : 'Cavern', 'item' : 'Sword-Enchantment'},
    'Hidden Room' : {'South' : 'Treasure Room', 'East' : 'Dungeon Hall', 'item' : 'Flame-Resistance-Potion'},
    'War Room' : {'West' : 'Laboratory', 'East' : 'Dungeon Hall', 'item' : 'Armor-Enchantment'},
    'Laboratory' : {'South' : 'Treasure Room', 'East' : 'War Room', 'Item' : 'Enchantment-Table'},
    'Treasure Room' : {'item' : 'Dragon'}
}
#Helps with Decision branching
directions = ['North','South','East','West']
#Sets the player in the first room
currentroom = 'Shed'
inventory = []
print(show_instructions())
while currentroom != 'Exit':
    def showStatus():
        print('You are in the', currentroom)
        print('Inventory:', inventory)
        print('You see a', rooms[currentroom]['item'])
    showStatus()
    #Win Condition checkpoint
    if currentroom == 'Treasure Room':
        if len(inventory) < 8:
            print('You get eaten by the Dragon')
            break
        else:
            print('You slay the dragon and return to your boss victorious')
    #input from player
    c = input('What would you like to do?:\n')
    #exit condition
    if c == 'Exit':
        currentroom = 'Exit'
    #split to have tokens dignify if move or get item
    tokens = c.split()
    if tokens[0] == 'move' or 'Move':
        if tokens[1] in directions:
            try:
                currentroom = rooms[currentroom][tokens[1]]
            except KeyError:
                print('You see a Wall')
        else:
            print('Not a Valid Direction')
    elif tokens[0] == 'get' or 'Get':
        if tokens[1] == rooms[currentroom]['item']:
            inventory.append(rooms[currentroom]['item'])
        else:
            print('Not a valid Item')
    else:
        print('Not a Valid Entry')
print(main())

The problem I am running into is whether or not I input get or move it initiates the "if tokens[0] == 'move' or 'Move'" line. So typing Get just prints 'Not a Valid Direction'. Can anyone help me to see how I messed up my statement? At the end of the day I want my movement to work from room to room, print the item in the room, be able to add the item to the inventory, and move to the next room. Once 8 items are collected I want to win the game once getting to the 'treasure room'.

Aucun commentaire:

Enregistrer un commentaire