制作文本类游戏:多结局概率触发实现及乱码异常求助
Hey there! Let’s work through your text game’s multi-ending problems step by step—probability triggers, garbled text, and overlapping endings are all common hurdles, but they’re totally fixable with some targeted adjustments.
The most reliable way to handle weighted probabilities is to use a cumulative probability range system. Here’s a concrete example using Python (super common for text games):
import random def pick_ending(): # Define your endings with their percentage probabilities (sum to 100) ending_options = [ {"label": "Heroic Ending", "chance": 45}, {"label": "Bittersweet Ending", "chance": 30}, {"label": "Tragic Ending", "chance": 20}, {"label": "Hidden True Ending", "chance": 5} ] # Roll a random number between 1 and 100 random_roll = random.randint(1, 100) cumulative_chance = 0 # Check which range the roll falls into for ending in ending_options: cumulative_chance += ending["chance"] if random_roll <= cumulative_chance: return ending["label"] # Use the function to get the player's ending player_ending = pick_ending() print(f"Your journey ends with: {player_ending}!")
This method ensures each ending gets exactly the weight you want, and it’s easy to adjust probabilities later by tweaking the chance values.
Garbled text almost always comes from mismatched character encodings. Try these fixes:
- Save all text files as UTF-8: When writing story dialogue or ending text, make sure your text editor (like VS Code, Notepad++) saves files with UTF-8 encoding (no BOM is preferred).
- Explicitly set encoding in code: When reading or writing files, specify UTF-8 to avoid system-default encoding conflicts:
# Read story content without garbling with open("game_story.txt", "r", encoding="utf-8") as story_file: story_content = story_file.read() # Write ending text correctly with open(f"{player_ending}_dialogue.txt", "w", encoding="utf-8") as ending_file: ending_file.write(f"Congratulations on unlocking the {player_ending}!") - Fix terminal output: If your game prints garbled text to the console, switch the terminal to UTF-8. On Windows CMD, run
chcp 65001before launching your game; on Linux/macOS, UTF-8 is usually default, but double-check your terminal settings.
This happens when your logic doesn’t enforce mutually exclusive branches. Here are two simple fixes:
- Use
if-elif-elseinstead of multipleifstatements: This ensures only the first matching condition runs:# Bad: Multiple independent ifs can trigger overlapping endings if random_roll <= 45: print("Heroic Ending") if random_roll <= 75: # This will also trigger if roll is <=45! print("Bittersweet Ending") # Good: Mutually exclusive branches if random_roll <= 45: print("Heroic Ending") elif random_roll <= 75: # Only runs if first condition failed print("Bittersweet Ending") elif random_roll <= 95: print("Tragic Ending") else: print("Hidden True Ending") - Add a "triggered" flag: If your logic is more complex (like checking multiple in-game variables), set a flag once an ending is triggered to skip subsequent checks:
ending_triggered = False if player_morality >= 80 and not ending_triggered: print("Heroic Ending") ending_triggered = True if player_morality >= 40 and not ending_triggered: print("Bittersweet Ending") ending_triggered = True # ... rest of your ending checks
内容的提问来源于stack exchange,提问作者Tough Spartan

