所以,我正在解决这个问题,如果有人能指出我正确的方向,我将非常感激。让我为您设置一下。
我有 1 个名为 rooms 的 Python 文件/模块,其中充满了类,如下所示:
class Intro(object):
def __init__(self):
# Setting up some variables here
def description(self):
print "Here is the description of the room you are in"
哪一个既酷又对?然后在另一个名为 Engine 的文件/模块上。我有这个:
import rooms
class Engine(object):
def __init__(self, first_class):
self.lvl = first_class
def play_it(self):
next_lvl = self.lvl
while True:
# Get the class we're in & run its description
next_lvl.description() # it works.
# But how do I get the next class when this class is done?
看看我想要发生的情况是基于用户在每个房间/级别类别中的决定,引擎调用一个新类别及其描述功能/属性。那有意义吗?或者我应该以另一种方式思考这个问题?我有时会倒退。谢谢。
最佳答案
将接下来使用哪个类的选择委托(delegate)给房间。例如
class Intro(object):
def __init__(self):
# Setting up some variables here
def description(self):
print "Here is the description of the room you are in"
def north(self):
return Dungeon()
def west(self):
return Outside()
#etc
因此,当玩家在 Intro 房间中说“向西走”时,引擎会调用 room.west()
并且 Intro
房间返回 Outside
房间。
希望这已经足够了!我希望您会想要制作自己的设计。
关于python - "Moving"在Python中从一个类到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10566193/