Untitled
unknown
plain_text
3 years ago
2.4 kB
9
Indexable
import random
# Define the room class
class Room:
def __init__(self, name, description):
self.name = name
self.description = description
self.connections = {}
def connect(self, direction, room):
self.connections[direction] = room
def get_connection(self, direction):
if direction in self.connections:
return self.connections[direction]
else:
return None
# Define the game class
class Game:
def __init__(self):
self.current_room = None
self.rooms = {}
def add_room(self, room):
self.rooms[room.name] = room
def start(self):
self.current_room = random.choice(list(self.rooms.values()))
print(self.current_room.description)
def play(self):
while True:
user_input = input("> ").lower()
if user_input == "quit":
print("Thanks for playing!")
break
direction = None
for d in self.current_room.connections:
if user_input == d.lower():
direction = d
break
if direction is not None:
next_room = self.current_room.get_connection(direction)
if next_room is None:
print("You can't go that way!")
else:
self.current_room = next_room
print(self.current_room.description)
else:
print("I don't understand that command.")
# Define the rooms
entrance = Room("Entrance", "You stand before the entrance to a dark dungeon. You can hear eerie sounds emanating from within.")
hallway = Room("Hallway", "You are in a long, dimly lit hallway. The walls are made of rough stone.")
treasure_room = Room("Treasure Room", "You have found the treasure room! It is filled with gold, jewels, and other valuable items.")
# Connect the rooms
entrance.connect("north", hallway)
hallway.connect("south", entrance)
hallway.connect("east", treasure_room)
treasure_room.connect("west", hallway)
# Create the game and add the rooms
game = Game()
game.add_room(entrance)
game.add_room(hallway)
game.add_room(treasure_room)
# Start the game
game.start()
# Play the game
game.play()Editor is loading...