Question: Hello! I am programming a text game for a Python project and the objective is to have 8 rooms with 6 items and a villain

Hello! I am programming a text game for a Python project and the objective is to have 8 rooms with 6 items and a "villain" spread throughout. In order to complete the game, you must collect all 6 items and move to the boss. But every time I try to pick up an item in a room, it gives me the message that its the wrong item and 'not in the room' when it clearly is. Could someone please help me figure out why I'm unable to pick up the item that corresponds to the correct room? Thank you! Here is my code: (I went a little silly with the theme, sorry!)
# Skeleton Lord's Revengence
# Collect 6 items to win the game, or be trapped with Skeleton Lord for eternity.
# Move commands: go South, go North, go East, go West
# Add to Inventory: get 'item name'
import random
# Define the game map as a dictionary linking rooms to other rooms and items to their corresponding rooms.
game_map ={
'Main Cell': {'South': 'Eternal Fire Pit', 'North': 'Room of Everlasting Despair', 'East': 'Huge Skull Door'},
'Eternal Fire Pit': {'North': 'Main Cell', 'East': 'Forbidden Armory', 'item': 'Flame Sword'},
'Forbidden Armory': {'West': 'Eternal Fire Pit', 'item': 'Anti-Magic Armor'},
'Room of Everlasting Despair': {'South': 'Main Cell', 'East': 'Torture Chamber', 'item': 'Cursed Ring'},
'Torture Chamber': {'West': 'Room of Everlasting Despair', 'item': 'Skull Lantern'},
'Huge Skull Door': {'West': 'Main Cell', 'North': 'Skeleton Lords Lair', 'South': 'Bone Storage', 'item': 'Bag o Gold'}, # Correct linkage to the Bone Storage
'Skeleton Lords Lair': {'North': 'Huge Skull Door', 'item': 'Skeleton Lord'}, # villain
'Bone Storage': {'North': 'Huge Skull Door', 'item': 'Skull Door Key'} # New room with the 'Skull Door Key' item
}
# Define the player's starting position and inventory.
current_room = 'Main Cell'
inventory =[]
# Define the items that the player needs to collect.
items_to_collect =['Flame Sword', 'Anti-Magic Armor', 'Cursed Ring', 'Skull Lantern', 'Bag of Gold', 'Skull Door Key']
def show_instructions():
print("Skeleton Lord's Revengence")
print("Collect 6 items to win the game, or be trapped with Skeleton Lord for eternity.")
print("Move commands: go South, go North, go East, go West")
print("Add to Inventory: get 'item name'")
print("------------------------------------------")
def show_status():
print(f"You are in the {current_room}")
print(f"Inventory: {inventory}")
if 'item' in game_map[current_room]:
print(f"You see a {game_map[current_room]['item']}")
print("------------------------------------------")
def get_item(item_name):
if 'item' in game_map[current_room] and item_name == game_map[current_room]['item']:
inventory.append(item_name)
print(f"You got the {item_name}!")
del game_map[current_room]['item']
else:
print("Item not found in this room!")
def main():
global current_room # Declare current_room as global
show_instructions()
# Gameplay loop
while True:
show_status()
command = input("Enter your move (type 'exit' to quit): ").strip().lower()
# Handle exit command
if command == 'exit':
break
# Handle move commands
if command.startswith('go '):
direction = command.split('')[1].capitalize() # Convert the direction to title case
if direction in game_map[current_room]:
current_room = game_map[current_room][direction]
else:
print("You can't go that way!")
# Handle get item commands
elif command.startswith('get '):
item_name = command.split('',1)[1].capitalize() # Extract the item name and convert to title case
get_item(item_name)
# Handle invalid commands
else:
print("Invalid command. Please try again!")
# Check if the player has won or lost the game
if len(inventory)== len(items_to_collect):
print("Congratulations! You have collected all items and defeated the Skeleton Lord!")
break
if current_room == 'Skeleton Lords Lair':
print("YOU FOOL! I SENTENCE YOU TO OBLITERATION!", "*You are reduced to ashes instantly with a snap of bony fingers*")
break
print("Better luck next time, Adventurer!")
if __name__=="__main__":
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!