Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I am working on a text based game and I cat get the inventory to display items already collected in order to defeat the villain.
I am working on a text based game and I cat get the inventory to display items already collected in order to defeat the villain. Please help.
Here is my Python code:
def show_instructions(): #main menu and commands print("A witch has taken over your Kingdom!"), print("Collect all 6 items and defeat the witch!"), print("Move commands go South, go North, go East, go West"), print("Add to Inventory: get 'item name'"), print () rooms = { 'Great Hall':{'North':'Library', 'West':'Gate', 'South':'Armory', 'East':'Kitchen'}, 'Library':{'South':'Great Hall','East':'Bedroom',}, 'Bedroom':{'West':'Library'}, 'Gate':{'East':'Great Hall'}, 'Armory':{'North':'Great Hall','East':'Secret Room'}, 'Secret Room':{'West':'Armory'}, 'Kitchen':{'West':'Great Hall','North':'Dining Room'}, 'Dining Room':{'South':'Kitchen'} } items = { 'Great Hall':'No items', 'Library':'Book of Spells', 'Bedroom':'Secret Key', 'Gate':'Magic Sword', 'Armory':'Shield', 'Secret Room':'Witch', 'Kitchen':'Food', 'Dining Room': 'Magic Potion' } state= 'Great Hall' inventory = [] def get_new_state(state, direction): new_state= state for i in rooms: if i == state: if direction in rooms[i]: new_state = rooms[i][direction] return new_state show_instructions() while 1: #loop print('You are in the ',state) #current state if state == 'Secret Room': print('Battling with the witch',end ='') for i in range (50): for j in range (1000000): pass print(".",end='',flush = True) print() if len(inventory)>= 6: print('You won - Hooray') else: print('Sorry, you were defeated, get more items') break print('Available you in this room is',items[state]) print('You currently have', inventory) direction = input('Enter item you want OR direction to go OR exit to give up:')#asking user if direction.lower()==items[state].lower(): if items[state] not in inventory: inventory.append(items[state]) else: direction=direction.capitalize()#making first capital remain lower if direction=='Exit': #if exit(0)#exit function if direction =='East' or direction =='West' or direction == 'North' or direction == 'South': new_state = get_new_state(state, direction)#calling function if new_state == state: #if print('There is a loud noise coming from in front of you!')#print else: state = new_state #changing value else: print('Invalid direction!') #print
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started