'text based game get item and finishing game
Alright so, I am currently trying to create a text based game for a class and im having a few small issues. the first of which being the carrying value I've created doesn't want to pick up items and I also cannot figure out how to make the loop break in the final room. Im pretty new to this so im not 100% sure on how to ask but here's my code
import time
rooms = {
'Great Hall': {'name': 'Great Hall', 'South': 'Dining Room', 'North': 'Armory',
'East': 'Hallway', 'West': 'Bedroom', 'text': 'The Great Hall, begin your quest'},
'Bedroom': {'name': 'Bedroom', 'East': 'Great Hall', 'contents': 'Magic wand',
'text': 'A Bedroom. There appears to be a Magic wand in the corner'},
'Hallway': {'name': 'Hallway', 'West': 'Great Hall', 'North': 'Cellar', 'contents': 'Sword',
'text': 'A Hallway. There appears to be a Sword on the wall'},
'Cellar': {'name': 'Cellar', 'South': 'Hallway', 'contents': 'Book',
'text': 'A Cellar. There is a Book on a shelf'},
'Dining Room': {'name': 'Dining Room', 'North': 'Great Hall', 'East': 'Kitchen', 'contents': 'Potion',
'text': 'The Dining Room. It looks like somebody left their Potion behind...'},
'Kitchen': {'name': 'Kitchen', 'West': 'Dining Room', 'contents': 'Shield',
'text': 'A Kitchen. What a strange place to find a Shield'},
'Armory': {'name': 'Armory', 'South': 'Great Hall', 'East': 'Proving Grounds', 'contents': 'Armor',
'text': 'The Armory. That Armor looks like it would fit you...'},
'Proving Grounds': {'name': 'Proving Grounds', 'contents': 'Minotaur',
'text': 'The Proving Grounds. Are you ready for your showdown?'}
}
directions = ['North', 'South', 'East', 'West']
currentRoom = rooms['Great Hall']
carrying = []
def show_instructions():
# print a main menu and the commands
print('-------------------------------------------------------')
print("Text Adventure Game")
print("Collect 6 items to win the game, or be eaten by the minotaur.")
print("Move commands: South, North, East, West")
print("Add to Inventory: get 'item name'")
print('-------------------------------------------------------')
print('''--------------------------------------------------------------------------------------------------------
A great beast has taken over your lands. You are a young Viking, tasked with defeating the minotaur
and becoming the village hero. The grand hall holds everything you need. To defeat the beast, you must find the
six treasures located within the grand hall and defeat the beast that plagues your land!
--------------------------------------------------------------------------------------------------------''')
time.sleep(4)
show_instructions()
time.sleep(4)
while True:
# display current location
print()
print('You are in {}.'.format(currentRoom['text']))
# get user input
command = input('\nWhat do you do? ').strip()
# movement
if command in directions:
if command in currentRoom:
currentRoom = rooms[currentRoom[command]]
else:
# bad movement
print("You can't go that way.")
# quit game
elif command.lower() in ('q', 'quit'):
break
# bad command
elif command.lower().split()[0] == 'get':
item = command.lower().split()[1]
if item in currentRoom['contents']:
carrying.append(item)
print('You grabbed {}.'.format(currentRoom['contents']))
else:
print("I don't see that here.")
while currentRoom in ['Proving Grounds']:
if len(carrying) in 6:
print('You collected all of the items, and defeated the minotaur!')
else:
print('It looks like you have not found everything, you have been defeated!')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
