Untitled
unknown
plain_text
a year ago
5.0 kB
10
Indexable
import random class Player: def __init__(self, name): self.name = name class Location: def __init__(self, name, description): self.name = name self.description = description self.visited = False self.exits = {} def visit(self): self.visited = True def add_exit(self, direction, location): self.exits[direction] = location class ForestGame: def __init__(self): self.player = None self.locations = {} self.create_world() def create_world(self): # Define locations self.locations["Enter the Forest"] = Location("Enter the Forest", "You enter the forest, surrounded by towering trees.") self.locations["Discover an Overgrown Trail"] = Location("Discover an Overgrown Trail", "You discover an overgrown trail leading deeper into the forest.") self.locations["Enter a Grove Filled with Whispers"] = Location("Enter a Grove Filled with Whispers", "You enter a grove filled with mysterious whispers.") self.locations["Stumble Upon a Still, Dark Pond"] = Location("Stumble Upon a Still, Dark Pond", "You stumble upon a still, dark pond, its surface reflecting the surrounding trees.") self.locations["Cross a Crumbling Bridge Spanning a Chasm"] = Location("Cross a Crumbling Bridge Spanning a Chasm", "You cross a crumbling bridge spanning a deep chasm.") # Define connections between locations self.locations["Enter the Forest"].add_exit("north", self.locations["Discover an Overgrown Trail"]) self.locations["Discover an Overgrown Trail"].add_exit("south", self.locations["Enter the Forest"]) self.locations["Discover an Overgrown Trail"].add_exit("north", self.locations["Enter a Grove Filled with Whispers"]) self.locations["Discover an Overgrown Trail"].add_exit("east", self.locations["Stumble Upon a Still, Dark Pond"]) self.locations["Stumble Upon a Still, Dark Pond"].add_exit("west", self.locations["Discover an Overgrown Trail"]) self.locations["Stumble Upon a Still, Dark Pond"].add_exit("east", self.locations["Cross a Crumbling Bridge Spanning a Chasm"]) self.locations["Cross a Crumbling Bridge Spanning a Chasm"].add_exit("west", self.locations["Stumble Upon a Still, Dark Pond"]) self.locations["Cross a Crumbling Bridge Spanning a Chasm"].add_exit("east", self.locations["Enchanted Forest"]) def start_game(self): print("Welcome to the Enchanted Forest Adventure!\n") player_name = input("Enter your name: ") self.player = Player(player_name) print(f"\nWelcome, {self.player.name}! Your journey begins now...\n") self.explore_location(self.locations["Enter the Forest"]) def explore_location(self, location): while True: if not location.visited: print(location.description) location.visit() self.print_options(location) choice = input("\nWhat do you want to do? ").lower() if choice == "quit": print("\nThanks for playing!") return elif choice in location.exits: next_location = location.exits[choice] if next_location.name == "Enter a Grove Filled with Whispers": print(f"\nYou venture {choice} and find yourself at {next_location.name}.") print("You are entranced by the mysterious whispers...") print(f"Suddenly, {self.player.name} feels a sense of peace wash over them as they drift into eternity.") print(f"{self.player.name} finds themselves in heaven, surrounded by angels and eternal bliss.") return else: print(f"\nYou venture {choice} and find yourself at {next_location.name}.\n") location = next_location elif location.name == "Cross a Crumbling Bridge Spanning a Chasm" and choice == "find": print("\nYou search around and find a clue about the enchanted forest treasures. You also find an old map, but it's so faded that you can hardly see the signs of direction.") print("You decide to keep the map as it may be useful later.") print("You also notice a new path leading deeper into the forest.") self.locations["Cross a Crumbling Bridge Spanning a Chasm"].add_exit("north", self.locations["Enchanted Forest"]) else: print("Invalid choice. Try again.\n") def print_options(self, location): exits = list(location.exits.keys()) print("Exits:", ", ".join(exits)) if location.name == "Cross a Crumbling Bridge Spanning a Chasm": print("Type 'find' to search for clues and a map.") print("Type 'quit' to exit the game.") if __name__ == "__main__": game = ForestGame() game.start_game()
Editor is loading...
Leave a Comment