2017-01-03 48 views
-1

我在python中创建了一个游戏,我试图找到一种方法来创建一个故事,用户选择要继续的路径,现在听起来很简单,但是一旦添加如果在if内部的话太多了,那么事情会变得混乱。 我试图找到最清晰的,有效的和无限的方式(可以分支不断增加的故事线W /出计算机崩溃或东西。Python:高效和无限方式的选项分支

我使用repl.it和python 2.7.13

,认真,有多少次你要读这个弄清楚它的路径分支的游戏,你选择一个路径分支到其他路径,而不是一个房间NESW游戏!

+0

为了避免过多的嵌套,如果你可以使每个语句调用一个函数。 –

+1

你需要使用repl.it吗?你不能在你的电脑上安装Python吗?顺便说一句,你应该使用它,所以我不需要在家里工作 –

+0

没有Im和没有我喜欢Python 2和PY3也是完全不同的,它甚至不是同一种语言。 – DuckyQuack

回答

1

编辑:

我想出如何回答我的问题,这是我的代码:

区1 = 'SRTR'

def dict_area1(): 
    global area1 
    if area1 == "SrtR": 
     print ("you wake up in a forest") 
     print ('''what do you do? 
Choices: 
    1 = find shelter 
    2 = build shelter 
    3 = go back 2 sleep''') 
     while True: 
      AC1 = input('>') 
      if AC1 == '1': 
       area1 = "area_1_SmlShed" 
      elif AC1 == '2': 
       area1 = "area_2_PileLog" 
      elif AC1 == '3': 
       print ('you wake up an hour later, in the same place, it is now afternoon') 
       continue 
      else: 
       print ('that is not a choice') 
       continue 
      dict_area1() 
    if area1 == 'area_1_SmlShed': 
     print ('after searching for about an hour, you come across a small shed') 
     print ('the shed seems to be abandoned') 
     print (''' 
Choices: 
    1 = go into shed 
    2 = scout shed 
    3 = keep looking''') 
     while True: 
      AC1 = input('>') 
      if AC1 == '1': 
       area1 = "area_1_InShed" 
      elif AC1 == '2': 
       area1 = "area_BackShed" 
      elif AC1 == '3': 
       print ('After looking for another hour, you can\'t seem to find anything else') 
      else: 
       print ('that is not a choice') 
       continue 
dict_area1() 
1

一种选择是使用字典中的所有选项Here is an example - self.orient_dict然后在220行调用。

+0

我不知道这是如何工作的,因为我正在制作基于故事情节的游戏 – DuckyQuack

2

你的最好的方法可能取决于你的Python的知识水平。我认为戴维惠特洛克使用字典的建议是一个好的开始。如果你想要一个更先进的方法,你会定义自己的Python对象(类),然后建立自己的“世界”了出来(例如房间,对象,球员,能力等)。这将是无限可扩展的。

我做了一些四处寻找,发现这个现成的模块做到这一点:Create your own Classic Text Adventure with Python。看看它是如何工作的。

+0

有一个更新版本的代码在这里:https://github.com/gever/bwx-adventure – Bill

0

你需要找到你的冒险正确的数据结构。对于带有“房间”的文本冒险,图形将非常合适。基本上你会创建一个room类,它们是图中的节点。每间客房都提供您可以从那里前往的客房。一个非常简单的room类将有north,south,eastwest成员能够在每个方向上旅行。更加复杂的游戏可以有房,其中键描述方向行驶去隔壁房间的dict