分支对话-如何继续

时间:2019-07-18 16:07:01

标签: python-3.x

从事游戏,就像制作附加项目一样,以更加熟悉编码。想要分支对话,但我想知道哪种解决方案将是解决此类问题的最佳方法。目前看来,它将是很多if / elif / else语句。

此代码有效,但是我认为它很快就会变得混乱,因此我认为必须有一种更简单的方法。

def choosePath():
    path = ""
    while True:
        path = input("What will you do?\n--> ").lower()
        if path in "abcde":
            return path
        elif path not in "abcde":
            print("Invalid command, try again\n")

choice1 = choosePath()

if choice1 == "a":
    print("text")
    choice2 = choosePath()
    if choice2 == "a":
        print("new text")
elif choice1 == "b":
    print("text")
if choice1 == "c":
    print("text")

1 个答案:

答案 0 :(得分:0)

分支对话和编程语言的问题在于,对话最好与 script 一起使用,例如剧场,剧本,因此您可以从A点跳到B点等等。某些编程语言允许您使用goto语句和标签来执行此操作,但这是一种危险的做法,通常应尽可能避免这样做(即使那样,在python中也是不可能的)。

嵌套脚本是一个不错的开始,它提供了一个相当直观的分支对话系统。问题是,它会很快发展出许多级别的压痕,这是您发现的。

一种可能的解决方法是仅在if语句中保留一个,然后创建一个变量(或变量 s )来指定用户所在的对话分支。例如:

current_path = None

# unconditional path
print("First line of dialogue")
print("Second line of dialogue")
# branch point
choice = choosePath():
current_path = {"a": "path1", "b": "path2", "c": "path3", ...}[choice]

# branch A
if current_path == "path1":
    print("First line of dialogue in branch A")
    print("Second line of dialogue in branch A")
    # branch A branch point
    choice = choosePath():
    current_path = {"a":"path1-sub1", ...}

# branch B
if current_path == "path2":
    print("First line of dialogue in branch B")
    print("Second line of dialogue in branch B")

# branch C
if current_path == "path3":
    print("First line of dialogue in branch C")
    print("Second line of dialogue in branch C")

# sub-branch of A
if current_path == "path1-sub1":
    print("new text")
...

通过使用更复杂的if条件,这也允许更多的创意/扩展分支。当然,它确实需要先见之明,并计划适当地设置分支名称才能使其正常工作。

如果您采用这种方法,我建议您修改choosePath()函数以实际返回可接受的当前路径-例如,将其输入路径名选项字典中,然后返回适当的路径名:< / p>

def choosePath(choices_dict):
    # truncate the list of displayed letter options depending on the number of choices
    letter_options = "abcde"[:len(choices_dict)]

    # zip together dict keys and associated letter options in a tuple
    # [('a', choices_dict.keys()[0]), ('b', choices_dict.keys()[1]), ...]
    options_correspondence = zip(letter_options, choices_dict.keys())

    # make a list of lines to print that show the user what their choices are
    # in the format "letter": "choice"
    displayedOptions = [f"  {letter}: {choice}" for letter, choice in options_correspondence]

    # make a dict such that the letter typed corresponds to the path to take
    # if that letter is typed
    choiceOptions = {letter:choices_dict[choice] for letter, choice in options_correspondence}

    # tell the user what the options are
    print("Here are your options:")
    print("\n".join(displayedOptions))

    # continuously ask the user for their choice until their choice is valid
    # (letter_options.split('') is used to make sure that, for example, 'ab'
    #    is not a valid input - must be asingle letter).
    user_choice = ""
    while user_choice not in letter_options.split(''):
        user_choice = input(f'Which will you choose? (enter any of "{letter_options}"): ')

    # return choice corresponding to user input
    return choiceOptions[user_choice]

...
current_path = choosePath({
    "Go towards the forest":"forestpath", 
    "Go towards the town":"townpath"
})