私は現在、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)
私はプログラミング/このウェブサイトが初めてです。提供したスクリプト情報が少なすぎる/多すぎる場合は、お知らせください。前もって感謝します!