2013-07-08 90 views
1

我正在创造一个文本RPG的灵感来自旧的文字冒险,玩家输入英文命令;如“拿起剑”等。 我已经建立了一个简单的;输入'A'来做到这一点,并输入'B'来做到这一点,但我想扩大我的系统以获得更多的自由。 我需要创建一个系统;当玩家键入一个命令时,程序挑选出关键词。 我认为这可以通过'in'命令实现。 这里是我的代码:英文命令[文字RPG] - 蟒蛇

print "What would you like to do??" 
input_loop_sleep = str('') 
choice_sleep = raw_input(str('>>>')) 
loop_sleep = False 
table_time = False 
bed_time = False 
error_time = False 



while loop_sleep == False: 


    if str('sleep') in choice_sleep or str('bed') in choice_sleep or str('goodnight') in choice_sleep or str('Sleep') in choice_sleep or str('tired') in choice_sleep: 
     while bed_time == False: 
      print "you decide to go back to sleep" 
      time.sleep(1) 
      print "..." 
      time.sleep(1) 
      print "" 
      time.sleep(1) 
      print "darkness" 
      time.sleep(1) 
      print "" 
      print "you wake up..." 
      time.sleep(1) 
      print "it is now 9:15am" 
      time == int(9.15) 
      time.sleep(1) 
      print "You are standing in your room, slightly more refreshed." 
      time.sleep(1) 
      print "there is a table with some things on it, stairs, and a wardrobe... with the doors wide open..." 
      time.sleep(1) 
      print "that's strange... you swear that they were shut when you went to sleep..." 
      break 
     else: 
      bed_time == True 
     break 
     bed_loop_choice = raw_input('>>>') 



    elif str('bedside') in choice_sleep or str('table') in str(choice_sleep): 
     while table_time == False: 
      print "You rub your eyes and pick up some belongings from a" 
      print "bedside table." 
      time.sleep(1) 
      print "Map added!" 
      time.sleep(1) 
      print "100 gold added!" 
      time.sleep(1) 
      print "Leather Bag added!" 
      cash == int(100) 
      time.sleep(1) 
      Map == str('map of', str(province)) 
      Inventory == [str(Map)] 
      container == str('leather bag') 
      print "your", str(container), str("contains a"), str(Map), str('and'), str(cash) 
      break 
     else: 
      table_time == True 
     break 

    else: 
     print "invalid command!" 
当我运行的代码

,无论我在里面输入的内容总是与“睡眠”选项进入。 我可能只是犯了一些简单的错误! 你能帮我解决我做错了什么,以及我如何解决它。

+3

下载不公开。请尽量让您的问题自成一体。 –

+0

请详细描述您的问题,并在此发布您的相关代码。 –

+0

修复了问题 – Dannyboy8899

回答

5

要回答你关于为什么sleep循环重复所有的时间问题:

你控制回路通过

while bed_time == False: 

,但你永远不会在你的循环设置bed_timeTrue(仅在else子句,但该子句仅在环路正常退出时执行,而不是,因为您现在正在执行此操作,因此bed_time将永远不会更改)。

此外,直接比较布尔值通常是皱眉。惯用的方式(大多数语言,不仅仅是Python)应该是while not bedtime:

在开始这样一个大项目之前,您应该阅读一些初学者的编程书籍和/或Python tutorial。代码中有几个问题表达了您需要掌握一些基本编程原则和Python习惯用法的印象。

例如,

int(9.15) 

不是存储时间的好方法 - 其结果将是9

然后,您使用time == int(9.15),这意味着“将模块time与整数9进行比较”。我想你的意思是time = int(9.15)由于上述原因,这已经不好了,但是还会有另外一个问题:你将覆盖模块名称time,这将导致后续的time.sleep(1)命令失败并返回AttributeError

在您的代码中不需要调用大多数str()调用,因为您已在字符串的对象上使用它。你不在的地方,这是不正确的:str('map of', str(province))将会增加TypeErrorstr只带一个参数)。

您正在使用不是类实例的对象的大写变量名称。

等等...

+2

我还会注意到==不是Python中的赋值运算符,只是比较运算符。 – llb

+0

@llb:哇,我完全错过了。 –

2

我想,这应该足以理清问题

In [1]: str('bed') in "bedside" 
Out[1]: True 

所以,当你写bedside它得到了sleep选项里面,如果状态,因此你得到错误的答案。

你应该写:

if str('bed') == choice_sleep or *other conditions* : 
    then got inside the sleep option 

P.S:我假设你已导入time模块。 P.P.S:我检查了输入table的代码,它工作正常。