简体   繁体   中英

python-text based game- adding it to the inventory with no duplicates

I'm having trouble creating a code that will ask the user if they would like to add the item to the inventory. When I run my code, it doesn't show that an item was found or that there is no item in that room. My goal is to have all items in the inventory, and the player wouldn't be able to pick up the same item if it's in the inventory unless it's the healing potion which will have a limit of 3(i have an idea of what to do for the limit and want to try that on my own). Could someone help me with this? Below is what I have so far:

def instructions():

print("-------------Instructions-------------")
print("Collect the Fire Sword, Shield, and Magic key to defeat the Demon King Nox and save Princess Calista. ")
print("Find the Magic Ring and Healing potions to assist you but be careful of the traps King Nox has set up!")
print("To move type: north, south, east, or west.")
print("To stop playing type 'quit'.")
print('-' * 38)


def game_sum():
    print("\n        Text Adventure Game")
    print('-' * 38)
    print("The princess of the kingdom Chrysanthemum was kidnapped by the Demon King, Nox.")
    print("King Nox wants to steal Princess Calista's magic and life force to take over her kingdom.")
    print("Her loyal knight must save the princess from the Demon King before it is too late.")
    print(' ' * 38)

game_sum()

instructions()

# data setup
rooms = {'Great Hall': {'name': 'Great Hall', 'item': ['none'], 'south': 'Vendetta Room', 'east': 'Kitchen',
                        'north': 'Potion Lab', 'west': 'Armory',
                        'text': 'You are in the Great Hall.'},

         'Armory': {'name': 'the Armory', 'item': ['fire Sword'], 'east': 'Great Hall', 'north': 'Treasure Room',
                    'text': 'You are in the Armory.'},

         'Treasure Room': {'name': 'the Treasure Room', 'item': ['Magic Ring'], 'south': 'Armory',
                           'text': 'You are in the Treasure Room.'},

         'Potion Lab': {'name': 'the Potion Lab', 'item': ['Healing Potion'], 'east': 'Bedroom', 'south': 'Great Hall',
                        'text': 'You are in the Potion Lab.'},

         'Bedroom': {'name': 'the Bedroom', 'item': ['Magic Key'], 'west': 'Potion Lab',
                     'text': 'You are in the Bedroom.'},

         'Kitchen': {'name': 'the Kitchen', 'item': ['Sandwich'], 'south': 'Storage', 'west': 'Great Hall',
                     'text': 'You are in the Kitchen.'},

         'Storage': {'name': 'Storage', 'item': ['Shield'], 'east': 'Mystery Room',
                     'text': 'You are in Storage.'},

         'Mystery Room': {'name': 'the Mystery Room', 'item': ['none'], 'west': 'Storage', 'north': 'Kitchen',
                          'text': 'You are in the Mystery Room.'},

         # villain
         'Vendetta Room': {'name': 'the Vendetta Room', 'item': ['none'], 'west': 'Dungeon', 'north': 'Great Hall',
                           'text': 'You are in the Vendetta Room.'},

         # Princess
         'Dungeon': {'name': 'the Dungeon', 'item': ['none'], 'east': 'Vendetta Room',
                     'text': 'You are in the Dungeon.'}
         }

directions = ['north', 'south', 'east', 'west']
current_room = rooms['Great Hall']
inventory = []

# game loop
while True:

    if current_room['name'] == 'the Dungeon':
        print('Congratulations! You have reached the Dungeon and saved the Princess!')
        break

        # display current location and inventory
    print('You are in {}.'.format(current_room['name']))
    print('Your current inventory: {}\n'.format(inventory))

        # get user input
    command = input('Enter Move:')

    # movement
    if command in directions:
        if command in current_room:
            current_room = rooms[current_room[command]]
        elif command == 'get item':
            if current_room['item'] != 'none':
                inventory.append(current_room['item'])
                print("You acquired : ", current_room['item'])
                print(inventory)
            elif current_room['item'] == 'none':
                print("No items to collect in this room")

        elif command != rooms[current_room[command]]:
            # bad movement
            print('You cannot go that way.')
            # quit game
    elif command == 'quit':
        print('Thanks for playing!')
        break
        # bad command
    else:
        print('Invalid input')

You can have a look at what datastructure exist for your use case (storing elements only once). In python this can be easyly implement by either a set or a map.

In your case, because you store the number of item (for potions) and the items you can use a map.

To do that, instead of using an list for your inventory, you can use a map:

Because you are not using any class, you can do it like that, map the key (item name) to the quantity inside the inventory. If the key is not present, then its 0:

# Define your inventory as a "global" variable

inventory = {}

# Create a function to update an inventory
def add_to_inventory(inventory, item):
   if item in inventory:
      # Your logic here if item is inventory (do nothing if potion), you should check the quantity associated for this item.
   else:
      inventory[item] = 1 # Add the item into inventory, with quantity one.

After the program gives the item to the player's inventory you should set the room to have none:

inventory.append(current_room['item'])
current_room['item'] = 'none'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM