Working on a project for an intro to python scripting course. The project is to build a game starting with some basic code they provide to create an adventure that takes you through several rooms, collecting items along the way. I've been able to get most of the requirements working but after adding the inventory system to the game, its become pretty slow to process and is also creating an issue with how the game progresses. I have no doubt that code is inefficient and could be either reorganized or just use different looping.
Before getting the inventory system operating as required, it would take a user input for direction, tell you if it was the correct room and if there was an item in the room, you would type a move, then move to the next room if it was correct. Now, when the game starts, it shows you in correct room with no item (As expected), you type a move and it is now very slow to process the next move. Then it shows the item and prompts before showing you in the room with your inventory, where it should be showing you in the room, that there is an item, and what your inventory is.
If anyone could help point out what I'm doing wrong, I would greatly appreciate it.
import time
# A dictionary for the Server room text game
# The dictionary links a room to other rooms.
rooms = {
'IT Office': {'South': 'Corporate Office'}, # First Room
'Corporate Office': {'North': 'IT Office', 'South': 'Cafeteria', 'East': 'Supply Closet', 'item': 'fix it guide'},
'Supply Closet': {'West': 'Corporate Office', 'item': 'plumbers tape'},
'Cafeteria': {'North': 'Corporate Office', 'West': 'Break Room', 'South': 'Maintenance Facility', 'item': 'poncho'},
'Break Room': {'East': 'Cafeteria', 'item': 'rain boots'},
'Maintenance Facility': {'North': 'Cafeteria', 'East': 'Equipment Shed', 'item': 'pipe wrench'},
'Equipment Shed': {'West': 'Maintenance Facility', 'North': 'Server Room', 'item': 'section of pipe'},
'Server Room': {'South': 'Equipment Shed'} #Last Room
}
#defining the game instructions
def introduction():
print('')
print('********** Server Room Text Adventure Game **********\n')
time.sleep(1.0)
print('*****************************************************')
print('\nThere are 8 rooms to move between and 6 items to pick up\n')
print('that you will require to complete your adventure.\n')
print('Directions: You can go North, South, East, or West\n')
print('to navigate between rooms. If the room has an item\n')
print('it will be picked up and added to your inventory.\n')
print('You can also type exit to leave the game. Good Luck!\n')
print('*****************************************************\n\n')
time.sleep(2.0)
# Defining player status, prints
def player_status():
print('=========================================')
print('\nYou are currently in the {}'.format(currentRoom))
print('\nYour current inventory: {}'.format(inventory))
#print('You see a {}'.format(currentItem))
print('\n=========================================')
print('\n')
# Inventory list to hold inventory as it's added in each room
inventory = []
user_item = ''
# defining Inventory carried to add to inventory list
def game(item):
#user_item = input('')
if item in inventory: # use in operator to check membership
print("you already have got this")
print(" ".join(inventory))
elif item not in inventory:
print('You see a', item)
print('Would you like to pick it up? \n')
user_item = input('')
if user_item == 'Y':
print('item has been added\n')
inventory.append(item)
else:
print('You leave the item behind.')
#else:
#print("You see a", item)
#print("and you pick it up, its been added to inventory")
#inventory.append(item)
#print(" ".join(inventory))
# Current Room, starts off in IT Office
currentRoom = 'IT Office'
# Players Move
player_move = ''
# Calling the Introduction function
introduction()
# While Loop for moving between rooms
while True:
player_status()
player_move = input('Enter a move: \n')
if player_move == 'Exit':
print('Thanks for playing.')
# break statement for exiting
break
if player_move in rooms[currentRoom]:
currentRoom = rooms[currentRoom][player_move]
addItem = input(' \n')
if 'item' in rooms[currentRoom]:
game(rooms[currentRoom]['item'])
#if/else statements with break statement for final room/finishing the game
if currentRoom == 'Server Room':
if len(inventory) == 6:
print('\n*********************** Congratulations! ***********************')
time.sleep(1.0)
print('\n************************************************************************')
print('\nYou have made it to the Server Room with all of the items to ')
print('fix the burst pipe. You used the items you collected to repair')
print('the pipe, and Saved the Data! You are a Hero to your company!\n')
print('*************************************************************************\n')
time.sleep(2.0)
print('^^^^^^^^^^^^^^^^^^^^^^^^^^ Thanks for playing! ^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n')
break
else:
print('\n################## You Failed! #####################')
time.sleep(1.0)
print('\n####################################################')
print('\nYou are missing items needed to fix the pipe')
print('& the Server Room has flooded. All of your')
print('companies data is gone. Time to polish your resume.')
print('\n####################################################\n')
print('@@@@@@@@@@@@@@@@@@@ Please play again @@@@@@@@@@@@@@@@')
break
# else statement for input validation/invalid move
else:
print('\nYou walk in to a wall, try again.\n')