我目前正处于学习python的开始阶段。我制作了一个游戏,使用类。但现在我需要将这些类放在另一个文件中,并从主文件中导入它们。现在我有:
a_map = Map("scene_1")
game = Engine(a_map)
game.play()
我似乎无法使用模块制作这样的实例。我试过了:
a_map = __import__('map')
game = Engine(a_map)
game.play()
但这给了我错误
AttributeError: 'module' object has no attribute 'first_scene'
这里出了什么问题?这些是引擎/地图类:
class Engine(object):
def __init__(self, map):
self.map = map
def play(self):
current_scene = self.map.first_scene()
while True:
next = current_scene.enter() #call return value of the current scene to 'next'
current_scene = self.map.next_scene(next) #defines the subsequent scene
和
class Map(object):
scenes = {"scene_1" : Scene1(),
"scene_2" : Scene2(),
"scene_3" : Scene3()
}
def __init__(self, start_scene):
self.start_scene = start_scene
#defines the first scene, using the 'scenes' array.
def first_scene(self):
return Map.scenes.get(self.start_scene)
#defines the second scene, using the 'scenes' array.
def next_scene(self, next_scene):
return Map.scenes.get(next_scene)
我是编程/这个网站的新手。如果我提供的脚本信息太少/太多,请告诉我。提前致谢!