I'm building a text-based adventure game in Python for my intro to scripting class. I have most of the code running such as moving between rooms, prompting the player to choose a direction, and closing the game when the player inputs quit. I'm having issues with my code to grab and remove items from a room to work and to even prompt them when there is an item in the room. I've been working on it for a few hours and can't seem to get the correct format. This is what I have so far:
def main_menu():
print('Welcome to the Avengers Text Based Adventure Game!')
print("Collect the Avengers stolen weapons to win the game or be defeated by Ultron")
print("Move Commands: go North, go South, go East, go West")
print("Add to Inventory: get 'item name'")
main_menu()
def get_items(current_room, command, rooms, inventory): #adds items to the inventory and then recommands it from the current room
inventory.append(rooms[current_room]['item'])
del rooms[current_room]['item']
def main():
pass
rooms = {
'Living Area': {'North': 'Gym', 'South': 'Workshop', 'East': 'Rehab Room', 'West': 'Conference Room'},
'Gym': {'South': 'Living Area', 'East': 'Shooting Range', 'item': 'Thors Hammer, Mjolnir'},
'Shooting Range': {'West': 'Gym', 'item': 'Captain Americas Shield'},
'Conference Room': {'East': 'Living Area', 'item': 'Black Widow Bite'},
'Workshop': {'North': 'Living Area', 'East': 'Hangar', 'item': 'Iron Man Suit'},
'Hangar': {'West': 'Workshop', 'item': 'Hawkeye Bow & Arrow'},
'Rehab Room': {'West': 'Living Area', 'North': 'Foyer', 'item': 'Key to Hulks prison'},
'Foyer': ''
}
current_room = 'Living Area'
inventory = []
directions = ['north', 'south', 'east', 'west']
s = ' '
while True:
print('\nYou are currently in the {}.'.format(current_room)) # displays current location
command = input('\n What would you like to do?').strip()
if command in rooms[current_room]: # user performs valid movement
current_room = rooms[current_room][command]
elif command.lower() == 'quit': # user quits game
print('Thanks for playing!')
#break
print(inventory)
if current_room != 'Foyer':
if 'item' in rooms[current_room].items():
print('You have found the {}'.format(rooms[current_room]['item'])) # tells user they have found an item
print('-----------------------------')
command = input('Enter your move: ').title().split() # prompts for a move
if len(command) >= 2 and command[1] in rooms[current_room].items(): # if player wantes to enter a new room
current_room = rooms[current_room][command]
break
elif len(command) == 4 and command[0] == 'Get' and s.join(command[1:3]) in rooms[current_room]['item']: # if player wants to grab an item
print('You have picked up the {}'.format(rooms[current_room]['item']))
print('-----------------------------')
get_items(current_room, command, rooms, inventory)
continue
else:
print('Sorry, you cant go that way. Try again.')
main()
I also have this code block to add/remove an item but am not sure how to implent it:
inventory = []
#
# def start_room():
# global inventory
# print("You are currently in the Living Area...")
# pickup = input()
# if pickup == 'yes':
# addToInventory('Iron Man Suit')
# else:
# print('Ignore Iron Man Suit')
# print(inventory)
#
# def addToInventory(item):
# inventory.append(item)
# start_room()
And previously, I had written this block of code to prompt the user to pick up an item, add to inventory and remove the item from the room but kept receiving an error around: 'if item not in current_room[item]:' this line stating "expected int|slice", given str instead" and I had no clue how to fix this one:
# elif command.lower().split()[0] == 'get':
# item = command.lower().get('item')
# if item not in current_room[item]:
# current_room[item].remove(item)
# inventory.append(item)
# print(inventory)
# else:
# print("Is there anything in here?")
# elif command.lower().split()[0] == 'drop':
# item = command.lower().get('item')
# print(inventory)
# if item in inventory:
# current_room[item].append(item)
# inventory.remove(item)
# else:
# print("Did you forget something?")