You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python文本冒险游戏:如何实现多分支剧情路径跳转

文本冒险游戏多剧情分支实现方案

问题场景

正在通过Udemy的100天Python课程学习编程,基于课程内容制作文本冒险游戏。游戏运行正常,但在角色选择门的环节遇到问题:当用户输入d1选择木门、d2选择铁门后,希望跳转至对应的剧情分支继续游戏,知道可以用break退出循环,但不清楚如何实现这种多路径的剧情延续。最初考虑用新文件实现,但感觉不合适,未找到合适方案。相关代码如下:

print("What to do you do? \n")
print("To open the Wooden door, enter: d1 \n ")
print("To open the Metal door, enter: d2 \n ")
print("To read whats on the wall, enter: r1 \n ")
while True:
    c1 = input("\n d1 \n d2 \n r1 \n> ")

    if c1 == "d1":
        time.sleep(0.5)
        print("you enter the wooden door . \n ")
        # continue advanture in file1
    if c1 == "d2":
        time.sleep(0.5)
        print("you enter the metal door. \n ")
        # continue advanture in file2
        
    elif c1 == "r1":
        print("You walk closer to the wall and see that it has a bunch of names carved in to it")
        print("most of the names are crossed over with a red X..")
        print("your eyes quickly goes to the end of the list and see something that sends a chill down youw spine")
        print("the name " + nm + " carved in to the stone wall")
        print("without a red X... \n")
        print("*You go back to the center of the room*")
        
        
    else:    
        print(" Please Enter Either d1, d2 or r1 ")

解决方案

不需要拆分文件,用函数封装剧情分支是最简洁易维护的方案,也可以用状态变量管理游戏流程,以下是具体实现:

方法1:函数封装各剧情分支

把木门、铁门对应的后续剧情封装成独立函数,用户选择后调用对应函数并退出当前循环即可。同时修正原代码中的条件判断逻辑错误(原代码中d2用if会导致输入d2后触发else分支)。

import time

# 定义木门后续剧情函数
def wooden_door_scenario():
    print("Inside the wooden door, you find a cozy cottage with a warm fireplace.")
    print("A old woman looks up at you and says: 'Welcome, traveler...'")
    # 这里可以继续编写木门分支的游戏逻辑

# 定义铁门后续剧情函数
def metal_door_scenario():
    print("The metal door creaks open to reveal a damp dungeon.")
    print("You hear distant dripping water and faint whispers in the dark.")
    # 这里可以继续编写铁门分支的游戏逻辑

# 初始房间场景
def main_room(nm):
    print("What do you do? \n")
    print("To open the Wooden door, enter: d1 \n ")
    print("To open the Metal door, enter: d2 \n ")
    print("To read what's on the wall, enter: r1 \n ")
    while True:
        c1 = input("\n d1 \n d2 \n r1 \n> ")

        if c1 == "d1":
            time.sleep(0.5)
            print("you enter the wooden door . \n ")
            break  # 退出当前房间循环
        elif c1 == "d2":
            time.sleep(0.5)
            print("you enter the metal door. \n ")
            break  # 退出当前房间循环
        elif c1 == "r1":
            print("You walk closer to the wall and see that it has a bunch of names carved into it")
            print("most of the names are crossed over with a red X..")
            print("your eyes quickly go to the end of the list and see something that sends a chill down your spine")
            print(f"the name {nm} carved into the stone wall")
            print("without a red X... \n")
            print("*You go back to the center of the room*")
        else:    
            print("Please Enter Either d1, d2 or r1 ")
    
    # 根据用户选择进入对应剧情
    if c1 == "d1":
        wooden_door_scenario()
    elif c1 == "d2":
        metal_door_scenario()

# 假设nm是之前获取的用户名
nm = input("Enter your name: ")
main_room(nm)

方法2:状态变量管理游戏流程

适合场景较多的复杂游戏,用一个变量记录当前所在场景,主循环根据场景执行对应逻辑:

import time

def main():
    nm = input("Enter your name: ")
    current_scene = "main_room"  # 初始场景

    while True:
        if current_scene == "main_room":
            print("What do you do? \n")
            print("To open the Wooden door, enter: d1 \n ")
            print("To open the Metal door, enter: d2 \n ")
            print("To read what's on the wall, enter: r1 \n ")
            c1 = input("\n d1 \n d2 \n r1 \n> ")

            if c1 == "d1":
                time.sleep(0.5)
                print("you enter the wooden door . \n ")
                current_scene = "wooden_door"  # 切换场景
            elif c1 == "d2":
                time.sleep(0.5)
                print("you enter the metal door. \n ")
                current_scene = "metal_door"  # 切换场景
            elif c1 == "r1":
                print("You walk closer to the wall and see that it has a bunch of names carved into it")
                print("most of the names are crossed over with a red X..")
                print("your eyes quickly go to the end of the list and see something that sends a chill down your spine")
                print(f"the name {nm} carved into the stone wall")
                print("without a red X... \n")
                print("*You go back to the center of the room*")
            else:    
                print("Please Enter Either d1, d2 or r1 ")
        
        elif current_scene == "wooden_door":
            # 木门场景逻辑
            print("Inside the wooden door, you find a cozy cottage with a warm fireplace.")
            choice = input("Do you talk to the old woman? (y/n) > ")
            if choice == "y":
                print("She hands you a key and says: 'This will help you escape...'")
            # 可以继续添加更多分支或切换场景
        
        elif current_scene == "metal_door":
            # 铁门场景逻辑
            print("The metal door creaks open to reveal a damp dungeon.")
            choice = input("Do you explore deeper? (y/n) > ")
            if choice == "y":
                print("You find a rusted sword lying on the ground.")
            # 可以继续添加更多分支或切换场景

if __name__ == "__main__":
    main()

内容的提问来源于stack exchange,提问作者NikkoV

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 21:01:05