选择题故事

2024-09-25 10:30:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图做一个选择题的故事,也被称为文本冒险。在

下面是我迄今为止所完成的一点工作的例子。在

  import time
while True: 
    print ("You awaken with a splitting headache and are very disoriented.")
    time.sleep (2)
    print (" ")
    print ("Slowly standing up you begin to relise you are in a dark corridor with no view of the end on either side")
    time.sleep (2)
    print (" ")
    d1a = input ("Do you want to: A) Go Left. B) Go Right. [left/right]? : ")
    if d1a in ['left', 'right']:
        break
time.sleep (2)
print (" ")
if d1a == "left":
    print ("You slowly edge your way left, going into the dark unknown...") 
elif d1a == "right": 
    print ("You slowly edge your way right, going into the dark unknown...")

我需要帮助的是,当一个人回答问题,得到“你慢慢地向右/向左,进入未知的黑暗中……”我需要让它这样做,这取决于他们如何回答更多的文本,并有机会添加更多的问题,这取决于他们选择的答案。在


Tags: thetoin文本rightyoutimewith
3条回答

你走的路是对的。我建议您将输入字符串从"Do you want to: A) Go Left. B) Go Right. [left/right]? : "更改为"Do you want to Go Left or Right. [left/right]? : "

原因是因为用户可能会输入A或B,而您的程序不喜欢这样作为响应。在

您可以定义一个接受字符串的函数,在该函数中有一个if语句,它将打印适当的消息。您还可以在用户每次输入后调用许多函数。在

您可以调用一个函数,而不是跳出循环。在

例子。在

import time

def goLeft():
    print ("You slowly edge your way left, going into the dark unknown...") 
    ##call another function that will continue the game.

def goRight():
    print ("You slowly edge your way right, going into the dark unknown...")
    ##call another function that will continue the game.

while True: 
    print ("You awaken with a splitting headache and are very disoriented.")
    time.sleep (2)
    print (" ")
    print ("Slowly standing up you begin to relise you are in a dark corridor with no view of the end on either side")
    time.sleep (2)
    print (" ")
    d1a = input ("Do you want to go Left or go Right. [left/right]? : ")

    if d1a == 'left':
        goLeft()
    elif d1a == 'right':
        goRight()
    else:
        print("You died!")
        break

time.sleep (2)

然后,这个功能可以引导人进入另一个功能,依此类推。您甚至可以使用一个整数参数,在这个参数中,您可以根据用户的轮数打印随机消息。 我真的很喜欢你这样开始的。继续前进。在

我想你想用一些方法来标记玩家之前的游戏路径?在

我会用一些方式跟你说:

1、使用变量标记前面的答案。在

answer1 = "left"
answer2 = "right"

if answer1 == xxx and answer2 == yyy:
   pass

但如果问题数量很大,问题就变得复杂了。在

2、根据你最后选择的函数。在

^{pr2}$

这样你可以很容易地处理选择和结果的关系,但问题是你只能链接到最后一个问题及其答案(也许你可以尝试在函数中添加一个参数上下文来表示之前的答案)

3.在更复杂的情况下,你可以使用状态机。单击下面的链接以获取更多信息。 https://en.wikipedia.org/wiki/Finite-state_machine

如果保留缩进,可以在相关打印语句下继续书写。如果我能正确理解它取决于用户是向左还是向右,那么就会出现完全不同的问题集?如果类似的问题会出现一段时间,但是故事会根据答案的集合而有所不同,那么我建议你用变量跟踪答案,然后用If语句告诉适当的故事分支。如果你把故事讲得简短一点,这应该会很有效。在

对于更复杂的方法,您可以将故事的不同部分(比如章节或房间)放入负责打印相关文本的函数中,查询用户的响应,然后将这些响应报告回主程序循环,然后主程序循环负责调用故事中的下一个函数。在

相关问题 更多 >