pythontext-based

Python text game launches but wont allow me to move through rooms


Working on a project for a Python scripting class. The objective of the project is to make a text-based game where you collect items and then confront the BBEG for a win. Self-explanatory and common question. I already have my game mostly coded but whenever I test it I can never get to move into another room or even pick up items. The code is below, an answer with an explanation would be preferred because I'm trying to make sure I learn whatever mistake I made.

# game start


def game_insturctions(): # introducing players to game and controls
    print("You've been hired to investigate this light house")
    print("Scavenge for 6 items you'll need to complete your investigation.")
    print("Be warned of the dangers lurking beneath the light house")
    print("without all 6 items you will not make it out alive.")
    print('Movement Controls: north, south, east, west')
    print("Pick up items: Get 'item name' ")
    print("Quit Game: quit")
    print("--------------------\n")

game_insturctions() # adding list of rooms and items
rooms = {
            'Dock': {'south': 'LH exterior', 'item': 'Flashlight'},
            'LH exterior': {'north': 'Dock', 'east': 'Living Quarters'},
            'Living Quarters': {'north': 'Basement', 'east': 'Kitchen', 'south': 'Service Room', 'west': 'LH exterior', 'item' : 'Fire Axe'},
            'Basement': {'south': 'Living Quarters', 'item': 'Eldritch Horror'},
            'Kitchen': {'west': 'Kitchen', 'item': 'First Aid Kit'},
            'Service Room': {'north': 'Living Quarters', 'east': 'Watch Room', 'west': 'Lantern Room'},
            'Watch Room': {'west': 'Service Room', 'item': "Keeper's Journal"},
            'Lantern Room': {'west': 'Service Room', 'item': 'Strange Necklace'},
}

current_room = 'Dock' # starts player on dock at game start
inventory = [] # player inventory

def get_new_room(current_room, direction): # allow players to enter new rooms
    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(current_room): # allow player to get items
    if 'item' in rooms[current_room]:
        return rooms[current_room]['item']
    if 'item' not in rooms[current_room]:
        return 'There is nothing to find here.' # no item message

while (current_room):
    print("You're at the", current_room) # tells current room
    print('Inventory:', inventory) # shows inventory

    item = get_item(current_room)

    if item !='Eldritch Horror':
        if current_room == 'Basement':
            print("Nothing in here.")
        if current_room != 'Basement':
            if item in inventory:
                print('You already have this item. Better check out the rest of the grounds.')
            else:
                print('You found a', item , "you should bring it along.")
                pickup = input()
                pickup = pickup.title()
                if pickup == str('Get' + item): # input for item pickup
                    inventory.append(item)
                    print('You picked up the' + item + '.')
                if pickup == 'quit':
                    print("The horror's persist.")
                    break

    if item == 'Eldritch Horror':
        if len(inventory) == 6:
            print("You have successfully defeated the horror of the lighthouse and solved the case.")
            break

        if len(inventory) != 6:
            print("You ventured into the horrors lair unprepared and thus have lost your mind to the void. Game Over...")
            break

    direction = input('Where would you like to go? ') # prompt input from the player
    direction = direction.capitalize()

    if direction == 'quit':
        print("The horror's persist.")
        break

    if (direction == 'north' or direction == 'south' or direction == 'east' or direction == 'west'):
        new_room = get_new_room(current_room, direction)
        if new_room == current_room:
            print('No where to go that way.')
        else:
            current_room = new_room

    else:
        print('No where to go that way.')

# end of code

Solution

  • You can convert to lower/uppper case all user inputs and all rooms data and then you can compare lower to lower case strings. Also there is a bit mess in if-else logic. Here is more strict version:

    ROOMS = {'Dock': {'south': 'LH exterior', 'item': 'Flashlight'},
             'LH exterior': {'north': 'Dock', 'east': 'Living Quarters'},
             'Living Quarters': {'north': 'Basement', 'east': 'Kitchen', 'south': 'Service Room', 'west': 'LH exterior',
                                 'item': 'Fire Axe'},
             'Basement': {'south': 'Living Quarters', 'item': 'Eldritch Horror'},
             'Kitchen': {'west': 'Kitchen', 'item': 'First Aid Kit'},
             'Service Room': {'north': 'Living Quarters', 'east': 'Watch Room', 'west': 'Lantern Room'},
             'Watch Room': {'west': 'Service Room', 'item': "Keeper's Journal"},
             'Lantern Room': {'west': 'Service Room', 'item': 'Strange Necklace'}}
    
    print('''You've been hired to investigate this light house
    Scavenge for 6 items you'll need to complete your investigation.
    Be warned of the dangers lurking beneath the light house
    without all 6 items you will not make it out alive.
    Movement Controls: north, south, east, west
    Pick up items: Get 'item name'
    Quit Game: quit
    ''')
    
    current_room = 'Dock'  # starts player on dock at game start
    inventory = []  # player inventory
    while True:
        print('--------------------')
        while True:
            print('You\'re at the', current_room)  # tells current room
            print('Inventory:', inventory)  # shows inventory
            item = ROOMS[current_room].get('item')
            if item:
                print('You found a', item, 'you should bring it along.')
            action = input('Your action: ').lower()
    
            if action == f'get {item}'.lower():
                print(f'You picked up the {item}')
                inventory.append(item)
                del ROOMS[current_room]['item']
    
                if item == 'Eldritch Horror':
                    if len(inventory) == 6:
                        print('You have successfully defeated the horror of the lighthouse and solved the case.')
                    else:
                        print('You ventured into the horrors lair unprepared and thus have lost your mind to the void. Game Over...')
                    exit()
    
            else:
                new_room = ROOMS[current_room].get(action)
                if not new_room:
                    print('No where to go that way.')
                else:
                    current_room = new_room
                    break
    

    Output:

    You've been hired to investigate this light house
    Scavenge for 6 items you'll need to complete your investigation.
    Be warned of the dangers lurking beneath the light house
    without all 6 items you will not make it out alive.
    Movement Controls: north, south, east, west
    Pick up items: Get 'item name'
    Quit Game: quit
    
    --------------------
    You're at the Dock
    Inventory: []
    You found a Flashlight you should bring it along.
    Your action: get FlashLight
    You picked up the Flashlight
    You're at the Dock
    Inventory: ['Flashlight']
    Your action: south
    --------------------
    You're at the LH exterior
    Inventory: ['Flashlight']
    Your action: