Untitled

 avatar
unknown
plain_text
25 days ago
3.6 kB
4
Indexable
import time
import sys

def print_slow(text):
    """Prints text with a slight delay to create a dramatic story effect."""
    for char in text:
        sys.stdout.write(char)
        sys.stdout.flush()
        time.sleep(0.015)
    print()

def play_game():
    # Define the story nodes. Each node represents a "page" in the adventure.
    # It contains the story text, two choices, and the corresponding destination keys.
    story_nodes = {
        "start": {
            "text": "You wake up in a dim, stone chamber. A heavy oak door stands in front of you, and a dark, damp tunnel slopes downward to your left.",
            "choices": ["Open the heavy oak door.", "Descend into the dark tunnel."],
            "routes": ["oak_door", "dark_tunnel"]
        },
        "oak_door": {
            "text": "The door creaks open. You step into a brightly lit banquet hall filled with food, but a sleeping dragon is curled up in the center.",
            "choices": ["Sneak past the dragon to the exit.", "Grab a golden chalice from the table."],
            "routes": ["exit_hall", "dragon_wakes"]
        },
        "dark_tunnel": {
            "text": "You wade through ankle-deep water. Suddenly, you hear skittering behind you. Two glowing eyes lock onto you from the dark.",
            "choices": ["Stand your ground and fight.", "Run blindly deeper into the tunnel."],
            "routes": ["monster_fight", "hidden_chasm"]
        },
        "exit_hall": {
            "text": "SUCCESS! You move like a shadow. You slip out of the front gates into the warm sunlight. You are free!\n\n*** GAME OVER: VICTORY ENDING ***",
            "choices": [],
            "routes": []
        },
        "dragon_wakes": {
            "text": "The chalice clinks against a plate. The dragon's eyes snap open. A wall of fire engulfs the room.\n\n*** GAME OVER: DEFEAT ENDING ***",
            "choices": [],
            "routes": []
        },
        "monster_fight": {
            "text": "You find a sharp rock and strike! Defeated, the creature flees, dropping a glowing crystal that illuminates an exit pathway.\n\n*** GAME OVER: HEROIC ENDING ***",
            "choices": [],
            "routes": []
        },
        "hidden_chasm": {
            "text": "You run blindly and misstep, tumbling down an unseen underground cliff into total darkness.\n\n*** GAME OVER: DEFEAT ENDING ***",
            "choices": [],
            "routes": []
        }
    }

    current_node = "start"

    print_slow("=== WELCOME TO THE CHOSE-YOUR-OWN PATH ADVENTURE ===")
    print("====================================================")

    # Main game loop
    while True:
        node = story_nodes[current_node]
        
        print()
        print_slow(node["text"])

        # If there are no choices left, the game has reached an ending
        if not node["choices"]:
            break

        # Display options
        print()
        print(f"[1] {node['choices'][0]}")
        print(f"[2] {node['choices'][1]}")

        # Get and validate player input
        while True:
            choice = input("\nMake your decision (1 or 2): ").strip()
            if choice in ["1", "2"]:
                break
            print("Invalid option. Please enter exactly 1 or 2.")

        # Update the game state based on selection
        index = int(choice) - 1
        current_node = node["routes"][index]

    print("\nThank you for playing!")

if __name__ == "__main__":
    play_game()
Editor is loading...
Leave a Comment