In my college course, we have to create a text-based game where you move in and out of rooms collecting items. I'm still trying to get the hang of python and coding in general so I've struggled with this project. For the most part, my code works except that I can add an item multiple times. How would I add it to the inventory and then delete it from the room to prevent the user from adding it more than once?
import time
#Shows rooms available to move and items in each room
rooms = {
'Foyer': {'North' : 'Dining Hall', 'East' : 'Admin Office' },
'Admin Office': {'West': 'Foyer', 'item': 'Key', },
'Dining Hall': {'North' : 'Exam Room 1', 'South' : 'Foyer', 'East' : 'Bathroom', 'West' : 'Kitchen', 'item' : 'Flashlight', },
'Bathroom': {'West': 'Dining Hall', 'item': 'Proton Pack' },
'Kitchen': {'East': 'Dining Hall', 'item': 'Satchel' },
'Exam Room 1': {'South' : 'Dining Hall', 'East' : 'Isolation Room', 'West': 'Patient Room 1', 'item' : 'Journal' },
'Patient Room 1': {'East': 'Exam Room 1', 'item': 'Ghost Capsule' },
'Isolation Room': {'West': 'Exam Room 1', 'item': 'Dr. Finkelstein' },
}
#Game Instructions
def game_instructions():
print('Collect 6 items to win the game, or be defeated by the ghost of Dr. Finkelstein.')
print('Move Commands: North , South , East , or West.')
print("Add to Inventory: get 'item name'")
print('You’ve been tasked with getting rid of the ghost of Dr. Finkelstein in the old insane asylum up on Academy Hill.')
time.sleep(1)
print('The ghost has taken over the asylum for three centuries and has begun opening a portal for more ghost to pass through.')
time.sleep(1)
print('During your last visit to the asylum, you dropped multiple items during a quick escape.')
time.sleep(1)
print('You need to go back into the asylum, collect all the items, and capture his ghost to close the portal!')
time.sleep(1)
print('The fate of the town is in your hands')
time.sleep(1)
game_instructions()
time.sleep(1)
current_room = 'Foyer' # starts player in the Foyer
inventory = [] # Adds an inventory, starts empty
def get_new_room(current_room, direction):
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):
if 'item' in rooms[current_room]:
return rooms[current_room]['item']
else:
return 'This room has no item!'
while (current_room): # gameplay loop
print('\n-------------------------')
print('You are in', 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('Item:', item) # tells the player what item they have found
print('-------------------------')
if 'item' == 'Dr. Finkelstein':
if len(inventory) == 6:
print('Congratulations!! You have collected all the necessary items and have successfully captured the ghost of Dr. Finkelstein and saved the town!')
else:
print('You did not have all the necessary items to defeat the ghost of Dr. Finkelstein, you are defeated.') # notifies player game has ended.
break # ends game
direction = input('Enter direction you would like to move. >>')
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'):
new_room = get_new_room(current_room, direction)
if new_room == current_room:
print('That is a wall not an exit. Try Again!')
else:
current_room = new_room
elif direction == str('get ' + item ).capitalize():
if item in inventory:
print('You have already collected this item. Move to another room!')
else:
inventory.append(item)
else:
print('Not a valid direction!')