3

スクリプトが次の部屋を取得する方法と、一般的に「エンジン」クラスと「マップ」クラスがどのように機能するかがわかりません。ここに抜粋があります:

Class Map(object):

    scenes = {
        'central_corridor': CentralCorridor(),
        'laser_weapon_armory': LaserWeaponArmory(),
        'the_bridge': TheBridge(),
        'escape_pod': EscapePod(),
        'death': Death()
    }

    def __init__(self, start_scene):
        self.start_scene = start_scene

    def next_scene(self, scene_name):
        return Map.scenes.get(scene_name)

    def opening_scene(self):
        return self.next_scene(self.start_scene)

class Engine(object):

    def __init__(self, scene_map):
        self.scene_map = scene_map

    def play(self):
        current_scene = self.scene_map.opening_scene()

        while True:
            print "\n--------"
            next_scene_name = current_scene.enter()
            current_scene = self.scene_map.next_scene(next_scene_name)

これらの部分がどのように機能するかわかりません。クラス、オブジェクトのインスタンス、属性、その他の OOP がどのように機能するかは知っていますが、何らかの理由でコードのこの部分がわかりません。主に Map クラス。誰かがそれを説明できたら、それは素晴らしいことです。

また (演習を読む必要があるかもしれません)、なぜこれら 2 つのクラスが必要なのですか? 代わりにクラス メソッド (つまり、self をパラメーターとして持たないメソッド) でそれを行うことはできませんか? 次に、たとえば CentralCorridor.enter() を呼び出すだけです。実際、それが答えを読む前に解決した方法であり、うまくいきました。

申し訳ありませんが、私の主な質問は、Engine クラスと Map クラスがどのように機能するかです。もう一つは二次的なものです。

前もって感謝します!

4

2 に答える 2

7

オブジェクトは、Mapシーンをマップするクラスです。配列に保存されたいくつかのシーンがあります。

scenes = {
    'central_corridor': CentralCorridor(),
    'laser_weapon_armory': LaserWeaponArmory(),
    'the_bridge': TheBridge(),
    'escape_pod': EscapePod(),
    'death': Death()
}

Map のオブジェクトが作成されると、コンストラクターに表示されるオープニング シーンも与えられます。

def __init__(self, start_scene):
    self.start_scene = start_scene

これにより、開始シーンを含む変数 in が作成さMapれます。start_scene

さらにMap2つの方法があります

# This one returns a scene based on its name or key in the scenes array
def next_scene(self, scene_name):
    return Map.scenes.get(scene_name)


# And this one  returns the opening scene which is set when you create the map.
def opening_scene(self):
    return self.next_scene(self.start_scene)

Engineいつ、何を演奏するかをコントロールしているようです。

# When creating an Engine object you give the map containing scenes to its constructor
def __init__(self, scene_map):
        self.scene_map = scene_map

# The method which starts playing the scenes
def play(self):

    # the opening scene from the map is selected as the current scene
    current_scene = self.scene_map.opening_scene()

     # You loop all the scenes probably, conditions of this loop are unknown because you haven't posted it entirely.
     while True:
         print "\n--------"
         # It seems the next scene name is known in the current scene
         next_scene_name = current_scene.enter()

         # It replaces current scene with the next scene from the map
         current_scene = self.scene_map.next_scene(next_scene_name)

とにかく、これらの2つのクラスが必要なのはなぜですか?

割り当てに応じて必要な場合を除き、そうではありません

あなたが言ったように、それなしでそれを行うことは可能ですが、そうするのは十分な理由があります.

このようにして、独自の責任を持つ 2 つの別個のクラスを作成します。このようにして、アプリケーションが大きくなってもコードが読みやすくなります。また、アプリケーション内を簡単にナビゲートできます。アプリなどの一部を簡単に変更できます。私のアドバイスは、練習を続け、OOP について読み続けることです。見ていることを行う理由に気付くでしょう。

于 2013-07-01T22:02:53.587 に答える