Python文本游戏的问题

Python文本游戏的问题,python,Python,所以,这可能会,也可能不会变得复杂。希望不会。 无论如何,我一直在业余时间编写一个雄心勃勃的Python文本游戏,只是想看看我是否能完成它。我意识到有很多交互式小说引擎、解析器等等,但我是从头开始做的。这是我学习的方式——我想是艰难的方式 下面是它的分解方式: 在engine.py模块中有main()函数,它几乎可以抓取房间对象并显示房间描述。然后它等待用户输入并将其发送给解析器 py模块中的解析器运行用户输入并构造一个句子对象(由动词和对象组成,可以是名词或方向)。语句对象还有一个输出函数,

所以,这可能会,也可能不会变得复杂。希望不会。 无论如何,我一直在业余时间编写一个雄心勃勃的Python文本游戏,只是想看看我是否能完成它。我意识到有很多交互式小说引擎、解析器等等,但我是从头开始做的。这是我学习的方式——我想是艰难的方式

下面是它的分解方式:

  • 在engine.py模块中有main()函数,它几乎可以抓取房间对象并显示房间描述。然后它等待用户输入并将其发送给解析器
  • py模块中的解析器运行用户输入并构造一个句子对象(由动词和对象组成,可以是名词或方向)。语句对象还有一个输出函数,它从command.py模块内部调用xxxCommand类
  • 示例:输入“gonorth”&解析器将其作为合适的句子接受。因此,解析器输出函数将搜索GoCommand类
现在我有麻烦了。在继续之前,为了清晰起见,我将粘贴我的引擎和句子类:

class Engine(object):
    def __init__(self, start_room):
        self.L = lexi.Lexicon() #Imported earlier
        self.P = parser.Parser() #Imported earlier
        self.room = start_room

    def main(self):
        while True:
            self.room.describe() #Describes the current room.

            # Ask for user prompt
            input = raw_input(self.room.prompt)
            cmd = input.lower()

            # Scans user input & creates tokens from Lexicon table
            # For example: [('verb', 'go'), ('direction', 'north')]
            tokenizer = self.L.scan(cmd)

            # Runs through tokens and creates a sentence object
            # With noun & object attributes
            parsed_sentence = self.P.parse_sentence(tokenizer)

            # See below
            parsed_sentence.output(self.room)


class Sentence(object):

    def __init__(self, verb, noun):
        self.verb = verb[1]
        self.obj = noun[1]

    # There's a command module with different xxxCommand classes with an execute method
    # as seen on last line. So pretend the command module has already been imported.
    def output(self, current_room):
        verb = self.verb.capitalize()
        obj = self.obj
        command = getattr(commands, '%sCommand' % verb)(obj)
        command.execute(current_room)   
好的,在那冗长的设置之后,我的GoCommand类如下:

# Subclassed from Command parent class. Has one method called execute. Does nothing else.
class GoCommand(Command):
    def __init__(self, direction):
        self.direction = direction

    def execute(self, current_room):
        # 'E' is the instantiation of the Engine class, declared within engine.py
        from engine import E

        # self.direction is 'north' in our example
        # current_room.map is a dict within any Room class named 'map'.
        # For example: Intro.map = {'north': char_intro }
        if self.direction in current_room.map:
            print "You head %s\n" % self.direction # Pretty explanatory

            # HERE'S WHERE I HAVE TROUBLE
            E.room = current_room.map[self.direction]
        else:
            print "You can't go that way."
所以我希望实现的是,当循环结束时,E.room将等于一个名为char_intro的房间类,当循环再次运行时,它将显示char_intro的描述,基本上重新开始

事实并非如此。它就在第一个房间里。虽然正在运行GoCommand.execute(),但E.room没有改变。有人知道为什么吗


哦,天哪,我意识到这很长,但我希望有人知道我在说什么&能帮我解决问题。我应该如何解决这个问题,以便当用户说go north&有一个为north设置的路径时,它会更改room类???

为了回答我自己的问题,Engine.main()中的循环按其预期的方式工作,但我将self.room.description()从while语句中删除:

def main(self):
    self.room.describe() #Describes the current room.
    while True:
        # Ask for user prompt
        input = raw_input(self.room.prompt)
        cmd = input.lower()
        ...etc, etc, etc...
然后我将GO命令更改为:

class GoCommand(Command):
def __init__(self, direction):
    self.direction = direction

def execute(self):
    # 'E' is the instantiation of the Engine class, declared within engine.py
    from engine import E

    # self.direction is 'north' in our example
    if self.direction in E.room.map.keys():
        print "You head %s\n" % self.direction # Pretty explanatory

        E.room = E.room.map[self.direction]
        print E.room.describe()
    else:
        print "You can't go that way."

而且一切都按预期进行。

因此,游戏状态(当前房间、库存)似乎保持在引擎上。为了便于比较,请看一下我作为命令解析练习编写的文本冒险游戏。在这段代码中,我在一个玩家实例上保留了当前的房间和库存。然后,主游戏循环看起来像:

parser = Parser()
p = Player("Joe")
p.moveTo( startRoom )
while not p.gameOver:
    cmdstr = raw_input(">> ")
    cmd = parser.parseCmd(cmdstr)
    if cmd is not None:
        cmd.command( p )

parseCmd
解析输入字符串,如果有效,返回实现
Command(p)
的Command对象,其中p是播放器。通过玩家,该命令可以访问当前房间、当前库存以及任何特殊玩家状态或技能(例如,具有较高视力的玩家在进入特定房间时可能更有可能检测到陷阱)。这也使得使用模拟播放器测试命令更容易,而不必模拟引擎本身中的任何全局变量或属性。

我没有立即看到任何不起作用的东西,但有一种猜测可能是,可能您有多个
引擎
,但没有意识到?您的意思是在我的python路径中?没有,我的意思是,也许你有一个
E
作为
引擎
模块的一部分,另一个
E
作为局部变量,它们是不同的,也许?self.direction不在当前房间中。map?作为E变量。。。我仔细检查了一下。这是唯一的一个。self.direction应该是从句子宾语(可以是名词或direction)传递的参数。我想知道它是否与循环本身有关,如何…?你在github上没有这个,是吗?不,它只是一个文件。