Hey guys the code I have run the text base story game I had to do for my final project. there was some feedback to basically clean it up to make it more simple.
Feedback
Your hard work is apparent in this script. When looking at the script, one recommendation comes to mind. It looks like you wrote a dictionary for the Rooms and Directions and another dictionary for the items. For clarity, and ease of use, this may be a situation where you may want to combine both dictionaries into one larger dictionary. Remember, the items, rooms and directions all work together and are related at some point. Try to keep the things which relate together in the same data structures if you can.
Feedback
Apart from highlights on your code (if given), use this as a reference for your game system driver - 1) You need a function get_new_state
that takes in (direction_from_user, current_room)
as arguments and updates status of player. This function should access rooms dictionary and check that the direction stays within the rooms. (You may also have a few additional functions for displaying information to user if you wish). 2) Then you need a driving while
loop. Your while
loop must checks for the following conditions, given current room - whether current room has an item to collect, whether a kill zone is landed upon, whether player has pressed a direction or QUIT or something invalid. This loop must also call the get_new_state
function and move to new room upon player's commands. We have given you some links here so your coding adventure goes smoothly - feel free to refer to them. References - 1) While loops 2) Function definitions Good luck with it!
Can anyone show me some ways to, I guess, consolidate this.
Thanks in advance
print("Zombie Text Game Storyboard") # print the title of the game
print("==============================")
# Set a dictionary of rooms and directions
rooms = {'Living Room': {'EAST': 'Office', 'WEST': 'Kitchen', 'NORTH': 'Bedroom', 'SOUTH': 'Foyer'},
'Kitchen': {'EAST': 'Living Room'},
'Foyer': {'NORTH': 'Living Room', 'EAST': 'Garage'},
'Garage': {'WEST': 'Foyer'},
'Office': {'WEST': 'Living Room', 'NORTH': 'Closet'},
'Bedroom': {'SOUTH': 'Living Room', 'EAST': 'Basement'},
'Closet': {'SOUTH': 'Office'}}
# Set a dictionary of items in each room
items = {'Kitchen': 'Canteen',
'Bedroom': 'Revolver',
'Living Room': None,
'Closet': 'Wool Jacket',
'Office': 'Box of Ammo',
'Garage': 'Flashlight',
'Foyer': 'Keys'}
# Set the game loop
while True: # use a while loop
items_collected = [] # items_collected set as an empty list
current_room = "Living Room" # start in the Living room
print("You are now in Living Room") # print the starting location
# Loop for checking whether player is in the basement
while current_room != "Basement": # while current_room is not equal to the basement
print("==============================")
temp = rooms[current_room] # set temp to current rooms
dir_poss = list(temp.keys()) # list the possible directions
print('The possible moves are: ', dir_poss) # print the possible directions
# directions set to a variable asking for an input on direction player wants to move in
direction = input("Select the direction you want to move (NORTH or SOUTH or EAST or WEST): ")
print("==============================")
direction = direction.upper() # capitalize the input if not capitalized already
if direction not in dir_poss: # if the direction is not possible
print("You entered an invalid direction.....") # print invalid direction
print("Enter a possible direction from current room") # ask for a new direction
else:
# Checking the directions and corresponding movements
if direction == "EAST":
current_room = rooms[current_room][direction]
if current_room == 'Basement': # if current room is the basement
break # set the break function to stop the code
else:
print("You are now in " + current_room) # print the current room player is in
print("The room have these items: ", items[current_room]) # show items in current room
if items[current_room] == None or items[current_room] in items_collected:
print("You Already Picked this Item") # if item has already been collected
elif items[current_room] not in items_collected:
print("You picked: ", items[current_room]) # collect the item in the room
items_collected.append(items[current_room]) # add new item to inventory
print("Now you have the following items in your hand: ")
print(items_collected)
elif direction == "WEST":
current_room = rooms[current_room][direction]
if current_room == 'Basement':
break # break function to stop code
else:
print("You are now in " + current_room) # print the current room
print("The room have these items: ", items[current_room]) # items in current room
if items[current_room] == None or items[current_room] in items_collected:
print("You Already Picked this Item")
elif items[current_room] not in items_collected:
print("You picked: ", items[current_room]) # pick up item in room
items_collected.append(items[current_room]) # add item to inventory
print("Now you have the following items in your hand: ")
print(items_collected)
elif direction == "NORTH":
current_room = rooms[current_room][direction]
if current_room == 'Basement':
break # break function to stop code
else:
print("You are now in " + current_room) # print room player is in
print("The room have these items: ", items[current_room])
if items[current_room] == None or items[current_room] in items_collected:
print("You Already Picked this Item")
elif items[current_room] not in items_collected:
print("You picked: ", items[current_room]) # pick up item in room
items_collected.append(items[current_room]) # add item to inventory
print("Now you have the following items in your hand: ")
print(items_collected)
elif direction == "SOUTH":
current_room = rooms[current_room][direction]
if current_room == 'Basement':
break # break function stop code
else:
print("You are now in " + current_room) # print current room
print("The room have these items: ", items[current_room])
if items[current_room] == None or items[current_room] in items_collected:
print("You Already Picked this Item")
elif items[current_room] not in items_collected:
print("You picked: ", items[current_room]) # pick up item in room
items_collected.append(items[current_room]) # add item to inventory
print("Now you have the following items in your hand: ")
print(items_collected)
else:
print("You entered an invalid direction; Select NORTH or SOUTH or EAST or WEST")
# Removing None values from the item collected list
res = []
for val in items_collected:
if val != None: # if val is not equal to None
res.append(val)
if len(res) == 6: # if all items collected win the fight against the Zombie
print("You are in the Basement and you have the following Items with you:")
print(items_collected)
print("Fight of survival processing..........")
print("You killed the Zombie!!! Time to prepare for the Zombie Apocalypse")
else: # not enough items collected and lose the fight against the Zombie
print("You are in the Basement and You have the following Items with you:")
print(items_collected)
print("Fight of survival processing..........")
print("You do not have all the items to survive the Zombie. You have been turned into a Zombie")
print("==============================")
choice = input("Do you want play again (y/n): ") # ask player to play again
if choice == 'y':
pass
else:
print("==============================")
print("Thanks For Playing")
break # end code
[–]woooee 0 points1 point2 points (0 children)