Reputation: 7
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!')
Upvotes: 0
Views: 2619
Reputation: 23139
You have a number of problems. As @Blackaurd says, your code to check against the contents of the current room is wrong because the value of contents
in each room is a string, and so you're comparing item
against each letter in the string. I expect what you want is a list of items in each room, because besides your code expecting that, I assume you want to be able to have multiple items in a room.
Another problem is that when you are comparing the typed in item name against the items in the room, you make sure the entered item name is lowercase but the item in the room's contents
starts with uppercase. You should in some what make sure that upper/lowercase doesn't matter.
Here's how to fix both of these. First, change your description of each room so that the contents
value is a list of items, like this:
'Armory': {'name': 'Armory', 'South': 'Great Hall', 'East': 'Proving Grounds',
'contents': ['Armor'], 'text': 'The Armory. That Armor looks like it would fit you...'},
Then, you can modify your check for a match against the room's contents to look like this:
item = command.lower().split()[1]
if item in [x.lower() for x in currentRoom['contents']]:
carrying.append(item)
You could also permanently convert room contents to lowercase, but I'm thinking that you might want to retain the uppercase version for display purposes. This does that. It doesn't convert anything. Rather, it just does the test in a way that everything is done in lowercase.
I'm not sure what you want in terms of the "final room", but it seems like maybe you want to be done when you get to the "Proving Grounds". I see some code at the end of your program that I'm guessing is what you want to run when your user gets to the "Proving Grounds", and then you want to exit. So you can just do that. After you set the current location to a new room, check if you're in the final location, and if so, perform the ending logic and exit the loop:
if command in currentRoom:
currentRoom = rooms[currentRoom[command]]
if currentRoom in ['Proving Grounds']:
if len(carrying) == 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!')
break
Note that I changed the test for how many items are in carrying
from using in
to using ==
. in
is used to test against a list. If you're testing against one specific value, like in this case, use ==
.
You need to remove the code at the bottom of your code, starting with the while
line. Not only do you not need the code there as you've moved it elsewhere in your code, the while
, as written, is an infinite loop if you're in the "Proving Grounds" when you get there. Think about it...you're going to keep looping until the current location is not the "Proving Grounds", but nothing in that loop will ever change the current location. So you'll keep printing the same thing over and over forever.
Upvotes: 1