So this is my first time taking a coding class. It's my second project for my IT140 class and I am making a text based game. This is my code so far. I do have a warning on Pycharm for
Shadows name
current_roomfrom outer scope on [lines 23, 76, 84]
but I haven't seen it create any issue so far when running the game. Thanks for any tips or suggestions!
# DKozzy
# Define the room dictionary and other global variables
rooms = {
'Entrance to the Crypt': {'North': 'Research Room'},
'Research Room': {'North': 'Chamber of Shadows', 'East': 'Bed Chambers', 'West': 'Treasury', 'South':
'Entrance to the Crypt', 'Item': 'Tome of Dark Incantations'},
'Treasury': {'East': 'Research Room', 'Item': 'Amulet of Undead Warding'},
'Bed Chambers': {'West': 'Research Room', 'Item': 'Phylactery Orb'},
'Chamber of Shadows': {'East': 'Armory', 'North': 'Throne Room', 'South': 'Research Room',
'Item': 'Orb of Shadow Veil'},
'Armory': {'West': 'Chamber of Shadows', 'Item': 'Soul Reaper Scythe'},
'Throne Room': {'South': 'Chamber of Shadows', 'West': 'Ritual Sanctum', 'Item': 'Cursed Crown of Dominion'},
'Ritual Sanctum': {'East': 'Throne Room'}
}
current_room = 'Entrance to the Crypt'
player_inventory = []
Lich_King_room = 'Ritual Sanctum'
# Define the get_new_state function
def get_new_state(direction_from_user, current_room):
global player_inventory
global rooms
if direction_from_user in rooms[current_room]:
new_room = rooms[current_room][direction_from_user]
print("You moved to", new_room)
# Check if the new room has an item
if 'Item' in rooms[new_room]:
item = rooms[new_room]['Item']
print("You found an item:", item)
return new_room
else:
print("You can't go that way.")
return current_room
# Function to display game instructions
def show_instructions():
instructions = (
"The Lich King's Terror!\n"
"Collect 6 items to win the game.\n"
"Move commands: go North, go South, go East, go West\n"
"Get item command: get [item name]\n"
"Check status command: status"
)
print(instructions)
# Function to display player's status
def show_status():
global current_room
print("Current Room:", current_room)
if 'Item' in rooms[current_room]:
print("Item in Room:", rooms[current_room]['Item'])
else:
print("No item in this room.")
print("Inventory:", player_inventory)
print("Available Directions:")
available_directions = rooms[current_room].keys()
available_directions = [direction for direction in available_directions if direction != 'Item']
print(", ".join(available_directions))
# Main function containing the gameplay loop
def main():
show_instructions()
current_room = 'Entrance to the Crypt' # Initialize current room here
while True:
command = input('Enter a command: ').lower()
action, *params = command.split()
if action == 'go':
direction = params[0].capitalize()
if direction in rooms[current_room]:
current_room = get_new_state(direction, current_room) # Pass current_room as parameter
else:
print('Invalid direction!')
elif action == 'get':
if len(params) == 0:
print('Please specify the item name.')
else:
item_name = ' '.join(params).lower() # Input item name to lowercase
# Check if the item exists in the current room
if 'Item' in rooms[current_room]:
room_item = rooms[current_room]['Item']
if item_name.lower() == room_item.lower():
player_inventory.append(room_item)
del rooms[current_room]['Item']
print(f'You obtained the {room_item} from this room.')
else:
print('Item not found in this room.')
else:
print('No item in this room.')
elif action == 'status':
show_status() # Uses the show_status function here
elif action == 'quit':
break
else:
print('Uh... What kind of command was that?')
# Check win condition
if len(player_inventory) == 6 and current_room == Lich_King_room:
print('Congratulations!\n'
'You have collected all items and defeated the Lich King.\n'
'You have saved the world!\n'
'You win!')
break
# Check lose condition
if current_room == Lich_King_room and len(player_inventory) < 6:
print("You encounter the Lich King, but you don't have all the required items to stop his treachery.\n"
'You lose!')
break
if __name__ == "__main__":
main()