交互式荒野逃脱冒险游戏分支叙事异常:首次选择后游戏终止问题求助
Hey Sharon, let's break down why your game ends right after the first choice—it all comes down to how you're structuring your story tree!
The Core Issue
Right now, you’re adding all four of your ending nodes directly to the root node in story_data.py, but your root only has two initial choices ("Roar back!" and "Run to the left..."). Worse, those ending nodes don’t have any child choices of their own, so when your traverse() method picks one, the loop immediately exits because there are no more choices to present.
Your story should be a layered tree:
- Root node → Intermediate node (for choice 1: Roar back) → Its two ending nodes
- Root node → Intermediate node (for choice 2: Run left) → Its two ending nodes
Fixed Code for story_data.py
Here’s how to restructure your story tree properly:
from tree_node import TreeNode # Root node (initial story prompt) story_data = TreeNode(""" You are in a forest clearing. There is a path to the left. A bear emerges from the trees and roars! Do you: 1 ) Roar back! 2 ) Run to the left... """) # --- Intermediate nodes for initial choices --- # Node for "Roar back!" choice roar_back_node = TreeNode(""" You roar back at the bear! It pauses, looking confused. Do you: 1 ) Apologize for yelling 2 ) Tell the bear you're having a rough week too """) # Node for "Run to the left..." choice run_left_node = TreeNode(""" You sprint down the left path and stumble upon a patch of wild flowers. The bear catches up, but seems calmer now. Do you: 1 ) Offer the bear a flower 2 ) Explain you were scared """) # --- Ending nodes --- # Endings for roar back path roar_apologize_end = TreeNode(""" The bear returns and tells you that bullying is not okay before leaving you alone in the wilderness. YOU REMAIN LOST. """) roar_empathize_end = TreeNode(""" The bear returns and tells you it's been a rough week. After making peace with a talking bear, he shows you the way out of the forest. YOU HAVE ESCAPED THE WILDERNESS. """) # Endings for run left path flower_offer_end = TreeNode(""" The bear is unamused. After smelling the flowers, it turns around and leaves you alone. YOU REMAIN LOST. """) scared_explain_end = TreeNode(""" The bear understands and apologizes for startling you. Your new friend shows you a path leading out of the forest. YOU HAVE ESCAPED THE WILDERNESS. """) # --- Build the tree hierarchy --- # Add intermediate nodes to root story_data.add_child(roar_back_node) story_data.add_child(run_left_node) # Add endings to intermediate nodes roar_back_node.add_child(roar_apologize_end) roar_back_node.add_child(roar_empathize_end) run_left_node.add_child(flower_offer_end) run_left_node.add_child(scared_explain_end)
Small Tweak to tree_node.py (Optional but Nice)
To make the ending feel more complete, add a message when the game reaches a final node (no more choices):
class TreeNode: def __init__(self, story_piece, choices=None): self.story_piece = story_piece self.choices = choices or [] def add_child(self, node): self.choices.append(node) def traverse(self): story_node = self while story_node.choices: choice = input("Enter 1 or 2 to continue the story: ") if choice not in ["1", "2"]: print("Invalid choice. Try again.") continue # Skip to next loop iteration instead of breaking chosen_index = int(choice) - 1 chosen_child = story_node.choices[chosen_index] print(chosen_child.story_piece) story_node = chosen_child # Add this line to signal game end print("\nThe game has ended! Thanks for playing.")
Why This Works
Now when the user makes their first choice, they’re taken to an intermediate node that presents a new set of choices. The traverse() loop will keep running until they reach an ending node (which has no choices), at which point the loop exits and the game wraps up nicely.
Let me know if you need help tweaking the story or adding more branches!
备注:内容来源于stack exchange,提问作者Sharon Kaufman

