Absolutely! Here’s a text-based adventure game in Python, using object-oriented programming and meeting all your requirements. The theme is "The Lost Artifact"—you must explore a mysterious mansion, collect items, solve a puzzle, and escape with the artifact!
import sys
class Item:
def __init__(self, name, description, usable=False):
self.name = name
self.description = description
self.usable = usable
def __str__(self):
return self.name
class Room:
def __init__(self, name, description):
self.name = name
self.description = description
self.items = []
self.connections = {}
self.locked = False
self.puzzle = None
def connect(self, direction, room):
self.connections[direction] = room
def get_connection(self, direction):
return self.connections.get(direction, None)
def describe(self):
print(f"\n== {self.name} ==")
print(self.description)
if self.items:
print("You see:", ', '.join(item.name for item in self.items))
print("Exits:", ', '.join(self.connections.keys()))
class Puzzle:
def __init__(self, question, answer, solved=False):
self.question = question
self.answer = answer
self.solved = solved
def attempt(self):
print(self.question)
user_answer = input("> ").strip().lower()
if user_answer == self.answer.lower():
self.solved = True
print("You solved the puzzle!")
return True
else:
print("That's not correct.")
return False
class Player:
def __init__(self, starting_room):
self.current_room = starting_room
self.inventory = []
def move(self, direction):
next_room = self.current_room.get_connection(direction)
if not next_room:
print("You can't go that way.")
return
if next_room.locked:
print("The door is locked.")
return
self.current_room = next_room
self.current_room.describe()
def take(self, item_name):
for item in self.current_room.items:
if item.name.lower() == item_name.lower():
self.inventory.append(item)
self.current_room.items.remove(item)
print(f"You picked up the {item.name}.")
return
print("No such item here.")
def use(self, item_name):
for item in self.inventory:
if item.name.lower() == item_name.lower():
if item.usable:
return item
else:
print(f"You can't use the {item.name} here.")
return None
print("You don't have that item.")
return None
def show_inventory(self):
if not self.inventory:
print("Your inventory is empty.")
else:
print("You have:", ', '.join(item.name for item in self.inventory))
class Game:
def __init__(self):
self.setup()
self.playing = True
def setup(self):
# Rooms
foyer = Room("Foyer", "A dusty entrance hall. A locked door leads north.")
library = Room("Library", "Shelves of old books. A strange box sits on a table.")
vault = Room("Vault", "A small room with a pedestal. The artifact is here!")
# Connections
foyer.connect("north", library)
library.connect("south", foyer)
library.connect("east", vault)
vault.connect("west", library)
# Items
key = Item("Key", "A small brass key.", usable=True)
note = Item("Note", "It reads: 'The password is 'owl''.")
artifact = Item("Artifact", "A glowing ancient artifact.")
# Place items
foyer.items.append(key)
library.items.append(note)
vault.items.append(artifact)
# Puzzle
puzzle = Puzzle(
"A locked box asks for a password. What is it?",
"owl"
)
library.puzzle = puzzle
vault.locked = True
# Store game state
self.rooms = {
"foyer": foyer,
"library": library,
"vault": vault
}
self.vault = vault
self.player = Player(foyer)
self.has_won = False
def process_command(self, command):
words = command.lower().split()
if not words:
return
if words[0] in ["go", "move"]:
if len(words) < 2:
print("Go where?")
else:
self.player.move(words[1])
elif words[0] in ["look"]:
self.player.current_room.describe()
elif words[0] in ["take", "get"]:
if len(words) < 2:
print("Take what?")
else:
self.player.take(' '.join(words[1:]))
elif words[0] == "inventory":
self.player.show_inventory()
elif words[0] == "use":
if len(words) < 2:
print("Use what?")
else:
self.handle_use(' '.join(words[1:]))
elif words[0] == "solve":
self.handle_puzzle()
elif words[0] in ["quit", "exit"]:
print("Thanks for playing!")
self.playing = False
else:
print("I don't understand that command.")
def handle_use(self, item_name):
item = self.player.use(item_name)
if not item:
return
# Use key to unlock vault
if item.name == "Key" and self.player.current_room == self.rooms["library"]:
if self.rooms["vault"].locked:
print("You unlock the door to the east with the key.")
self.rooms["vault"].locked = False
else:
print("The door is already unlocked.")
else:
print(f"You can't use the {item.name} here.")
def handle_puzzle(self):
room = self.player.current_room
if room.puzzle and not room.puzzle.solved:
if room.puzzle.attempt():
# Unlock vault if puzzle solved
self.rooms["vault"].locked = False
else:
print("There's nothing to solve here.")
def check_win(self):
# Win if player has artifact and is in the foyer
for item in self.player.inventory:
if item.name == "Artifact" and self.player.current_room == self.rooms["foyer"]:
print("\nCongratulations! You escaped the mansion with the artifact. You win!")
self.has_won = True
self.playing = False
def start(self):
print("Welcome to The Lost Artifact!")
print("Type commands like 'go north', 'take key', 'use key', 'solve', 'inventory', 'look', or 'quit'.")
self.player.current_room.describe()
while self.playing:
command = input("\n> ")
self.process_command(command)
self.check_win()
if __name__ == "__main__":
Game().start()
How the Game Works
- Rooms: Foyer, Library, Vault (3 rooms)
- Items: Key (to unlock vault), Note (gives puzzle hint), Artifact (win condition)
- Puzzle: In the Library, solve the password puzzle to unlock the Vault (or use the key)
- Inventory: Player can pick up and use items
- Win Condition: Escape to the Foyer with the Artifact
Try it out!
You can expand this with more rooms, puzzles, and items for extra fun. Let me know if you want more features or explanations!