I have a text-based game using python. My issue is that I can't move between rooms due to a variable not getting changed like it needs to.
I've used global
and that results in another error. The code is as follows:
#The dictionary links a room to other rooms and items.
rooms = {
'Holding Cell': {'South': 'Training room','North': 'Cupboard', 'West': 'Study' },
'Cupboard': {'South': 'Holding Cell', 'Item': 'Health Potion'},
'Study': {'West': 'Armory', 'East': 'Holding Cell', 'Item': 'Mind key'},
'Armory': {'East': 'Study', 'Item': 'Sword and shield'},
'Training room': {'North': 'Holding Cell', 'East': 'Storage room', 'Item': 'Body key'},
'Storage room': {'West': 'Training room', 'Item': 'Armor set'},
'Prayer room': {'North': 'Dungeon Exit', 'Item': 'Soul key'},
'Dungeon Exit': {}
}
starting_room = 'Holding Cell'
current_room = starting_room
inventory = []
inventory1 = ['Health Potion', 'Sword and shield', 'Mind key', 'Soul key', 'Armor set', 'Body key']
inventory1.sort()
health = 100
def status():
inventory.sort()
print('-----------------------------')
print("Inventory: ", inventory)
print("Health: ", str(health))
print("Current room: ", current_room)
def main():
rooms = {
'Holding Cell': {'South': 'Training room', 'North': 'Cupboard', 'West': 'Study'},
'Cupboard': {'South': 'Holding Cell', 'Item': 'Health Potion'},
'Study': {'West': 'Armory', 'East': 'Holding Cell', 'Item': 'Mind key'},
'Armory': {'East': 'Study', 'Item': 'Sword and shield'},
'Training room': {'North': 'Holding Cell', 'East': 'Storage room', 'Item': 'Body key'},
'Storage room': {'West': 'Training room', 'Item': 'Armor set'},
'Prayer room': {'North': 'Dungeon Exit', 'Item': 'Soul key'},
'Dungeon Exit': {}
}
status()
current_room = starting_room
direction = input("Enter 'North/South/East/West' to move or 'Exit': ")
# user to exit
if direction == 'Exit':
print("Thanks for playing!")
exit(0)
# a valid move
elif direction in rooms[current_room]:
current_room = rooms[current_room][direction]
# invalid move
else:
print("Invalid Move. There's no room to the {}".format(direction))
def show_instructions():
print("Type 'North', 'South', 'East', 'North' to go in a direction. Type 'Exit' to leave the game!")
print("To get an item type 'Get (item).")
#provides instructions to player
show_instructions()
while 1:
main()