我将如何将用户发送到新的“阻止”,而不是继续相同的路径?

问题描述 投票:0回答:2
response = ""
while response not in directions_window:
    print("While crouched below the window you begin weighing you options")
    print("You decide your only options are to listen in, knock on window, or wait")
    response = input("Which are you doing?\nlisten/knock/wait")
    if response == "listen":
        print("You raise your head slightly to hear better\n")
        print("You can ony make out a few words before the figure inside sees you\n")
        print("Latin words can be heard screaming as it charges towards the window\n")
        print("You try to run away, but slip and fall on your stomach.\n")
        print("The figure catches up to you only to knock you unconsious.\n")
        print("You wake up with scratches and marks around your wrist\n")
        print("Taking in your surroundings, you notice you're at the entrance to the forest.\n")
        print("You cut your losses, and leave the forest. Forever wondering who, or what, that was.\n")
        quit()

我正在制作基于文本的冒险游戏。如果选择此选项而不是quit(),我想将用户发送到游戏的新“部分”。我如何跳到一个新的街区?另外,我知道我可以不用打印每一行就可以编写它,但是随着时间的流逝,随着想法的出现,我写了这本书。稍后将修复

python text skip
2个回答
1
投票
[您可能要使用的功能是将冒险的“路径”分为不同的“部分”,每个“部分”将用户定向到不同的区域(如果需要,甚至可以使用户返回到以前的区域) ,就像这样:

def adventure_listen(): print("you picked listen") # ... more text response = input("Which do you pick?\nA/B") if response == "A": do_something() elif response == "B": do_something_else() def adventure_knock(): # code similar to above def adventure_wait(): # code similar to above print("message1") response = input("Which are you doing?\nlisten/knock/wait") if response == "listen": adventure_listen() elif response == "knock": adventure_knock() elif response == "wait": adventure_wait()


0
投票
“跳转块”,它在汇编代码中使用。在高级语言(例如python)中,使用流控制结构(否则,用于循环,while等)。

对于您的特定情况,如果您决定使用流控制结构,并且您的

story很长,那么您很快就会迷失在自己的代码中。 Cristopher Rybicki提出的功能将是更好的选择。为此,您应该(a)首先事先了解您的故事,和/或(b)在您的故事中找到一个可以使您“打开”和“关闭”章节的通用结构或模式。

(a),因为它将帮助您更好地构建代码(b)因为如果您一开始无法画出故事,将有助于您保持结构并遵循某些模式。

代码的执行是顺序执行的,因此(

cannot

不应从一个函数“跳转”到另一个函数,从而避免(或希望)在某些“分支”行不执行之后执行代码。 >更高级的方法(也是我推荐的方法)是与类一起学习并通过继承学习自己的方式。因此您的程序可以具有ActionsMoves或任何您选择的程序。因为您现在可能想要adventure_knock,后来又可能是adventure_throw_the_door_down。他们两个(i)来自故事的另一个步骤,(ii)将获得反馈给用户,并且(iii)必须回到故事中的要点。

在您的主要执行中,您可以具有一个循环来启动您的storieschapters ?,依此类推。

一个

伪代码

示例将是:
Action: abstract function start_action: ... abstract function end_action: ... abstract function give_feedback: ... function ask_for_input: input('What are you waiting for?') Knock extends Action: function start_action: #specific implementation of knock... ...
© www.soinside.com 2019 - 2024. All rights reserved.