加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

学习Python艰难的练习43

发布时间:2020-12-20 12:30:24 所属栏目:Python 来源:网络整理
导读:我不明白脚本如何获得下一个房间,以及“引擎”和“地图”类的工作原理.这是一段摘录: Class Map(object): scenes = { 'central_corridor': CentralCorridor(),'laser_weapon_armory': LaserWeaponArmory(),'the_bridge': TheBridge(),'escape_pod': EscapeP
我不明白脚本如何获得下一个房间,以及“引擎”和“地图”类的工作原理.这是一段摘录:

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类.如果有人能够解释它会很棒.

另外(这可能需要阅读练习),为什么还需要这两个课程呢?难道你不能用类方法代替它(即没有自我作为参数的方法).然后你可以调用,例如,CentralCorridor.enter().事实上,这就是我在阅读答案之前解决它的方法,并且结果很好.

对不起,我的主要问题是引擎和地图类的工作原理.另一件事是次要的.

提前致谢!

解决方法

Map对象是一个映射场景的Class.它有一些场景保存在一个数组中.

scenes = {
    'central_corridor': CentralCorridor(),'death': Death()
}

当制作一个Map对象时,你也会给它一个开头场景,如构造函数中所示

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

这会在Map中创建一个名为start_scene的变量,其中包含您的开场景.

此外,Map有2种方法

# 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)

引擎似乎在控制场景何时播放以及播放什么.

# 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)

why is it required to have these two classes anyway?

除非根据您的任务要求,否则不是

就像你说的那样可以没有,但是有充分的理由这样做.

通过这种方式,您可以使用自己的职责制作2个单独这种方式代码在应用程序变得越来越大时更具可读性.并且可以轻松浏览应用程序.您可以轻松更改应用程序的部分内容等.我的建议是继续练习和阅读有关OOP的内容,您会注意到为什么要执行您所看到的内容.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读