Skip to main content
 首页 » 编程设计

python之英文命令【文字RPG】-python

2025年05月04日13Terrylee

我正在创建一个文本角色扮演游戏,灵感来自玩家输入英语命令的旧文本冒险;比如‘捡剑’之类的。 我建立了一个简单的;输入“A”来执行此操作并输入“B”来执行此操作,但我想扩展我的系统以获得更多自由。 我需要创建一个系统;当玩家输入命令时,程序会挑选出关键词。 我认为这可以通过“in”命令实现。 这是我的代码:

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!" 

当我运行代码时,无论我输入什么,它总是带有“ sleep ”选项。 我可能只是犯了一些简单的错误! 你能帮我解决我做错了什么以及我该如何解决吗?

请您参考如下方法:

要回答关于为什么 sleep 循环一直重复的问题:

你通过

控制循环
while bed_time == False: 

但您从未在循环中将 bed_time 设置为 True(仅在 else 子句中,但该子句仅在循环时执行正常退出,当它通过 break 退出时不会,就像您现在所做的那样 - 因此 bed_time 永远不会改变)。

此外,与 bool 值的直接比较通常是不受欢迎的。惯用的方式(在大多数语言中,不仅仅是 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)) 将引发 TypeError(str 只需要一个参数)。

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

等等等等......