如何使用Python制作一个引擎来更改游戏的当前区域?

如何使用Python制作一个引擎来更改游戏的当前区域?,python,python-2.7,Python,Python 2.7,我目前正在使用《艰苦学习Python》一书学习Python。在练习43中,我被要求创建自己的游戏,我必须遵守以下规则: 我必须使用多个文件 我必须为我的角色可以去的每个区域使用一个类 在我的游戏中,我有不同的区域被表示为类,我需要一个引擎来运行它们。引擎应该设置起始区域,并在我的角色键入正确的命令时更改该区域。我不知道如何创建引擎,我在网上看到过一些,但我永远无法理解它们是如何工作的。我花了几个小时试着为自己做一个,但我就是做不到 这是不同区域的代码: areas.py: # Jungle

我目前正在使用《艰苦学习Python》一书学习Python。在练习43中,我被要求创建自己的游戏,我必须遵守以下规则:

  • 我必须使用多个文件
  • 我必须为我的角色可以去的每个区域使用一个类
在我的游戏中,我有不同的区域被表示为类,我需要一个引擎来运行它们。引擎应该设置起始区域,并在我的角色键入正确的命令时更改该区域。我不知道如何创建引擎,我在网上看到过一些,但我永远无法理解它们是如何工作的。我花了几个小时试着为自己做一个,但我就是做不到

这是不同区域的代码: areas.py:

# Jungle area's are numbered like a keypad on a phone.
from engine import *

class Jungle8(object):
    print "\nYou are south of the jungle.\n"
class Jungle1(object):
    print "\nYou are northwest of the jungle.\n"
class Jungle2(object):
    print "\nYou are north of the jungle."
    print "The Monkey Elder is waiting for you.\n"
class Jungle3(object):
    print "\nYou are northeast of the jungle.\n"
class Jungle4(object):
    print "\nYou are west of the jungle.\n"
class Jungle5(object):
    print "\nYou are in the center of the jungle.\n"
class Jungle6(object):
    print "\nYou are east of the jungle."
    print "You can see a cave with a locked door.\n"
class Jungle7(object):
    print "\nYou are southwest of the jungle.\n"
class Jungle9(object):
    print "\nYou are southeast of the jungle.\n"
这是发动机的代码,基本上为空:

from areas import *

class Engine(object):

    def __init__(self):
        # No idea on what to do at this point.

有没有办法办一个班?如果我在房间里使用函数,我就可以做到这一点,但是类让我很困惑。

是的,你可以。当类具有
\uuu call\uu
方法时,它是可调用的(函数)

class Jungle6():
    def __call__(self):
        print "\nYou are east of the jungle."
        print "You can see a cave with a locked door.\n"
举个例子

然后只需调用实例:

j = Jungle6()
j()

这应该存在于Python2中。我编辑了我的答案,希望更清楚。非常感谢!这应该对我有很大帮助!