pythontext-based

Beating TextBased Game with a full inventory of items


So this is my first question that I've ever posted on here so I'm sorry if I mess up the format of all this. I'm trying to code a text based game moving from room to room and adding items to your inventory. I currently have it set up that if you enter the Villain room and you don't have all items then you lose but if you have all items then you win. Is there a way to code it that once your inventory reaches 6(all items) then you automatically win and you don't have to go to the villain room? It just works better with my narrative, I guess you could say, if I never have to go to villains room in the first place. E.g. trying to escape before being caught by villain, not necessarily trying to fight villian.

Also, this is my first class and I'm still really new to Python, so any pointers on the current code that I have now will be appreciated as well!Thanks in advance!

rooms = {
    'Dining Room': {'South': 'Bedroom', 'North': 'Shop', "East": 'Kitchen', 'West': 'Study', 'item': 'none'},
    'Bedroom': {'North': 'Dining Room', 'East': "Innkeepers Suite", 'item': 'Longbow'},
    'Garden': {'West': 'Shop', 'item': 'Book'},
    'Shop': {'East': 'Garden', 'South': 'Dining Room', 'item': 'Staff'},
    'Study': {'East': 'Dining Room', 'item': 'Gold Coins'},
    'Kitchen': {'West': 'Dining Room', 'North': 'Stables', 'item': 'Chest'},
    'Stables': {'South': 'Kitchen', 'item': 'Horse'},
    'Innkeepers Suite': {'West': 'Bedroom', 'item': 'Seeker'}
}
def main():
def instructions():
    print('Welcome to the Wandering Lotus Inn Text Based Game')
    print("Collect your 6 items hidden around")
    print("the inn before you're caught by the Seeker!")
    print('Move directions: West, East, North, South')
    print("Add item to inventory: 'get item'")

def player_location():
    print("-" * 40)
    print('You are in the {}'.format(currentRoom))
    print('Inventory:', Inventory)
    print('Item:', rooms[currentRoom]['item'])

currentRoom = 'Dining Room'
player_move = ''
Inventory = []

while currentRoom in rooms:
    player_location()
    player_move = input('Enter your move:\n')
    if player_move in rooms[currentRoom]:
        currentRoom = rooms[currentRoom][player_move]
        if currentRoom == 'Innkeepers Suite':
            if len(Inventory) == 6:
                print('You win!')
            if len(Inventory) != 6:
                print('You lose..')
            break
    elif player_move == 'get item':
        if rooms[currentRoom]['item'] != 'none':
            Inventory.append(rooms[currentRoom]['item'])
            print("You aquired : ", rooms[currentRoom]['item'])
            rooms[currentRoom]['item'] = 'none'
        elif rooms[currentRoom]['item'] == 'none':
            print("There isn't anything in here!")
    else:
        print("Not a valid instruction, try again")

main()
print('Game Over! Thanks for playing')

Solution

  • Your code isn't running completely for me so it is hard for me to check if this is right. Regardless, you can grab the length of your Inventory after you append an item. If the length of the Inventory equals 6 then print "You win!" and break out of the while loop.

    So something like:

    Inventory.append(rooms[currentRoom]['item'])
    print("You aquired : ", rooms[currentRoom]['item'])
    
    if len(Inventory) == 6:
        print("You win!")
        break