|
| 1 | +import random |
| 2 | + |
| 3 | +class StoryNode: |
| 4 | + def __init__(self, text, options=None, effects=None): |
| 5 | + self.text = text |
| 6 | + self.options = options or [] |
| 7 | + self.effects = effects or {} |
| 8 | + |
| 9 | +class Character: |
| 10 | + def __init__(self, name): |
| 11 | + self.name = name |
| 12 | + self.inventory = [] |
| 13 | + self.traits = {} |
| 14 | + |
| 15 | +class StoryGame: |
| 16 | + def __init__(self): |
| 17 | + self.story_map = {} |
| 18 | + self.current_node = None |
| 19 | + self.player = None |
| 20 | + |
| 21 | + def add_node(self, node_id, text, options=None, effects=None): |
| 22 | + self.story_map[node_id] = StoryNode(text, options, effects) |
| 23 | + |
| 24 | + def start(self, node_id): |
| 25 | + self.current_node = self.story_map.get(node_id) |
| 26 | + |
| 27 | + def make_choice(self, choice_idx): |
| 28 | + if self.current_node and choice_idx < len(self.current_node.options): |
| 29 | + next_node_id = self.current_node.options[choice_idx] |
| 30 | + self.current_node = self.story_map.get(next_node_id) |
| 31 | + |
| 32 | + def apply_effects(self, effects): |
| 33 | + for key, value in effects.items(): |
| 34 | + if key == "add_item": |
| 35 | + self.player.inventory.append(value) |
| 36 | + elif key == "add_trait": |
| 37 | + self.player.traits[value[0]] = value[1] |
| 38 | + |
| 39 | + def get_current_text(self): |
| 40 | + text = self.current_node.text if self.current_node else "Game Over" |
| 41 | + if self.current_node.effects: |
| 42 | + self.apply_effects(self.current_node.effects) |
| 43 | + return text.replace("{name}", self.player.name) |
| 44 | + |
| 45 | + def is_game_over(self): |
| 46 | + return not self.current_node |
| 47 | + |
| 48 | +# Create the game |
| 49 | +game = StoryGame() |
| 50 | + |
| 51 | +# Create character |
| 52 | +player_name = input("Enter your character's name: ") |
| 53 | +player = Character(player_name) |
| 54 | +game.player = player |
| 55 | + |
| 56 | +nodes_data = [ |
| 57 | + ("start", "You wake up in a mysterious land. Do you explore or rest?", ["explore", "rest"]), |
| 58 | + ("explore", "You venture deep into the woods and find an old cabin. Do you enter or continue exploring?", ["enter_cabin", "continue_exploring"]), |
| 59 | + ("rest", "You find a cozy spot and rest for a while. When you wake up, you see a map nearby. Will you follow the map or ignore it?", ["follow_map", "ignore_map"]), |
| 60 | + ("enter_cabin", "Inside the cabin, you discover a hidden passage. Do you enter the passage or stay in the cabin?", ["enter_passage", "stay_in_cabin"]), |
| 61 | + ("continue_exploring", "You stumble upon a magical waterfall. Do you drink from it or move on?", ["drink_water", "move_on"]), |
| 62 | + ("follow_map", "Following the map, you find a hidden treasure. Do you take it or leave it?", ["take_treasure", "leave_treasure"]), |
| 63 | + ("enter_passage", "The passage leads to an ancient temple. Do you explore further or leave?", ["explore_temple", "leave_temple"]), |
| 64 | + ("stay_in_cabin", "You find a journal that reveals the history of the land. Do you keep reading or close the journal?", ["keep_reading", "close_journal"]), |
| 65 | + ("drink_water", "Drinking from the waterfall grants you enhanced senses. Will you use them to uncover secrets or continue your journey?", ["uncover_secrets", "continue_journey"]), |
| 66 | + ("move_on", "As you move on, you encounter a talking animal. Do you listen to its advice or ignore it?", ["listen_to_animal", "ignore_animal"]), |
| 67 | + ("enter_passage", "The passage leads to an underground city. Do you explore the city or return to the surface?", ["explore_city", "return_to_surface"]), |
| 68 | + ("uncover_secrets", "Using your enhanced senses, you find a hidden cave. Do you enter the cave or keep moving?", ["enter_cave", "keep_moving"]), |
| 69 | + ("listen_to_animal", "The animal warns you of danger ahead. Do you heed its warning or take your chances?", ["heed_warning", "take_chances"]), |
| 70 | + ("node88", "You come across a portal that leads to another realm. Do you step through or stay?", ["step_through", "stay"]), |
| 71 | + ("explore_city", "You discover an ancient artifact that can grant a wish. What do you wish for?", ["wish_for_power", "wish_for_wisdom"]), |
| 72 | + ("return_to_surface", "You emerge from the passage and find yourself in a bustling marketplace. Do you explore or leave?", ["explore_marketplace", "leave_marketplace"]), |
| 73 | + ("enter_cave", "Inside the cave, you find a friendly spirit. Do you converse with it or leave?", ["converse_with_spirit", "leave_cave"]), |
| 74 | + ("keep_moving", "You continue your journey and encounter a group of fellow travelers. Do you join them or go your own way?", ["join_travelers", "go_own_way"]), |
| 75 | + ("heed_warning", "You avoid the danger and find a hidden treasure. What will you do with the treasure?", ["share_treasure", "keep_treasure"]), |
| 76 | + ("take_chances", "Your gamble pays off, and you uncover a secret passage. Will you enter it or continue?", ["enter_secret_passage", "continue_journey"]), |
| 77 | + ("step_through", "You step through the portal and find yourself in a futuristic city. How do you navigate this new world?", ["explore_futuristic_city", "find_a_way_back"]), |
| 78 | + ("stay", "You decide to stay in the current realm, building a new life for yourself.", []), |
| 79 | + ("explore_marketplace", "In the marketplace, you meet a mysterious merchant. Will you buy a rare item or ignore the merchant?", ["buy_item", "ignore_merchant"]), |
| 80 | + ("leave_marketplace", "You leave the marketplace and find yourself in a tranquil garden. Do you explore further or rest?", ["explore_garden", "rest_in_garden"]), |
| 81 | + ("node89", "You face a final challenge. Do you confront it head-on or seek help?", ["confront", "seek_help"]), |
| 82 | + ("confront", "With courage, you conquer the challenge and become a legend.", []), |
| 83 | + ("seek_help", "You gather allies and together you overcome the final obstacle.", []), |
| 84 | +] |
| 85 | + |
| 86 | + |
| 87 | +for node_id, text, options, effects in nodes_data: |
| 88 | + game.add_node(node_id, text, options, effects) |
| 89 | + |
| 90 | +# Start the game |
| 91 | +game.start("start") |
| 92 | + |
| 93 | +# Game loop |
| 94 | +while not game.is_game_over(): |
| 95 | + print(game.get_current_text()) |
| 96 | + if game.current_node.options: |
| 97 | + choice = input("Enter your choice (0 or 1): ") |
| 98 | + game.make_choice(int(choice)) |
| 99 | + else: |
| 100 | + break |
0 commit comments