Inventory condition so I can only add item once and win condition

Here’s the code with all the bug fixes:

def game_instructions():
    # print a main menu and the commands
    print("Killer Wolf Text Game")
    print("---" * 20)
    print("Collect 6 items to win the game, or be killed by the Wolf.")
    print("---" * 20)
    print('Move Commands: North , South , East , or West.')
    print("---" * 20)
    print("Add to Inventory: get 'item name'")
    print("---" * 20)


rooms = {
    "Main fair ground": {'South': 'Food stand', 'North': 'Arcade', 'East': 'Corn field', 'West': 'Candy shop'},
    "Food stand": {'North': 'Main fair ground', 'East': 'Security', 'item': 'meat'},
    "Security": {'West': 'Food stand', 'item': 'armor'},
    "Arcade": {'South': 'Main fair ground', 'East': 'Gift shop', 'item': 'sword'},
    "Gift shop": {'West': 'Arcade', 'item': 'gloves'},
    "Candy shop": {'item': 'candy', 'East': 'Main fair ground'},
    "Corn field": {'West': 'Main fair ground', 'North': 'Petting area', 'item': 'repellent'},
    "Petting area": {'South': 'Corn field', 'item': 'Killer Wolf'}

current_room = 'Main fair ground'  # starts player in the Main fair ground
inventory = []  # Adds an inventory

def get_new_room(current_room, direction):
    new_room = None
    for i in rooms:  # starts loop
        if i == current_room:  # if statement
            if direction in rooms[i]:  # if statement
                new_room = rooms[i][direction]  # Assigns new room.
                new_room = current_room #this assigns new room to current room if the mentioned direction is not valid
    return new_room  # returns new room

def get_item(current_room):
    if 'item' in rooms[current_room]:  # if statement
        return rooms[current_room]['item']  # return statement
        return 'This room has no item!'  # return statement

while (current_room):  # gameplay loop
    print('You are in the {}'.format(current_room))  # tells player what room they are in.
    print('Inventory:', inventory)  # shows player their inventory
    item = get_item(current_room)  # defines item as get item
    print('You found the:', item)  # tells the player what item they have found
    if item == 'Killer Wolf':
        if len(inventory)<6:
            print('NOM NOM, You did not collected all the necessary items to survive! The game has ended, I hope you '
              'enjoyed!')  # notifies player game has ended.
        elif len(inventory)==6:
            print("You defeated the Killer Wolf so I hope you enjoyed playing")
    direction = input('Enter direction you would like to move. >>')  # gets direction from player.
    direction = direction.capitalize()  # Capitalizes the players input to match what is in the dictionary.

    if (direction == 'North' or direction == 'South' or direction == 'East' or direction == 'West'):  # if statement
        new_room = get_new_room(current_room, direction)  # Calling function
        if new_room == current_room:  # if statement
            print('That is a wall not an exit. Try Again!')  # Print statement
            current_room = new_room  # declares current room as new room
    elif direction == str('get ' + item).capitalize():  # input statement to add item
        if item in inventory:  # if statement
            print('You have already collected this item. Move to another room!')  # print statement
            inventory.append(item)  # adds item to inventory
        print('Not a valid direction!')  # Print statement
    if len(inventory) == 6:  # if statement
        print("Congratulations!! You have collected all the necessary items to defeat the killer wolf")

NOTE: Please copy the changes over properly, I made sure it all works

Browse More Popular Posts

Leave a Comment