习题43 基本的面向对象分析和设计

看题目就知道要搞什么面向对象编程(OOP)之类的。


作者给了我们一个经验流程:

        1.把要解决的问题写下来,或者画出流程图。

        2.将第一条中的关键概念摘录出来并加以研究。

        3.创建一个类和对象的层次结构图。

        4.用代码实现各个类,并写一个测试来运行它们。

        5.重复上述步骤并细化代码。


Zed称之为一个“自顶而下”(top down)的流程。现在我们将用这个流程实现一个游戏引擎和一个游戏。


===========================================================================================

《来自Percal 25号行星的哥顿人》


1.把问题写下来或者画下来

游戏描述就不多说了,自己看书看网页。先写场景:

1.Death                                       死亡

2.Central Corridor                     中央走廊

3.Laser Weapon Armory          激光武器库

4.The Bridge                              飞船主控舱

5.Escape Pod                            救生舱


2.摘录和研究关键概念

整理名词列表:

  • Alien                                          外星人
  • Player                                        玩家
  • Ship                                           飞船
  • Maze                                         迷宫
  • Room                                        房间
  • Gothon                                      哥顿人
  • Escape Pod                             救生舱
  • Planet                                        行星
  • Map                                           地图
  • Engine                                      引擎
  • Death                                        死亡
  • Central Corridor                      中央走廊
  • Laser Weapon Armory           激光武器库
  • The Bridge                               主控舱

3.为各种概念创建类层次结构图和对象关系图

Zed通过问问题的方式,把上面的工作转成一个类的层次结构图:

        “和其他东西有哪些类似?”

        “那个只不过是某个东西的另一种叫法?”


        很快可以发现,“房间”和“场景”基本是一个东西,所以游戏里面就叫做“场景”。

        “中央走廊”是一个场景,“死亡”也是一个场景。“死亡场景”可以接受,“死亡房间”就有些奇怪,所以之前选用“场景”这个词。

        “迷宫”和“地图”差不多一个意思,所以用“地图”。

        其实“行星”也可以是一个“场景”。

理清楚思路以后可以画一个类的层次结构图:

  • Map
  • Engine
  • Scene
    • Death
    • Central Corridor
    • Laser Weapon Armory
    • The Bridge
    • Escape Pod
然后查看描述里面的动词部分,知道每个东西要什么样的动作。大概是这么回事:

  • Map

- next_scene

- opening_scene

  • Engine

- play

  • Scene

- enter 

    • Death
    • Central Corridor
    • Laser Weapon Armory
    • The Bridge
    • Escape Pod

注意只有在 Scene 的下面添加了 enter 这个方法,因为具体的场景会继承并覆盖这个方法。

4.编写和运行各个类

准备好了类和函数的树状结构,现在需要在编辑器里面打开一个源文件,写代码。

通常只用把这个树状结构复制到源文件中,把它扩写成各个类就可以了。

#-*-coding:utf-8-*-  
class Scene(object):

    def enter(self):
        pass


class Engine(object):

    def __init__(self, scene_map):
        pass

    def play(self):
        pass

class Death(Scene):

    def enter(self):
        pass

class CentralCorridor(Scene):

    def enter(self):
        pass

class LaserWeaponArmory(Scene):

    def enter(self):
        pass

class TheBridge(Scene):

    def enter(self):
        pass

class EscapePod(Scene):

    def enter(self):
        pass


class  Map(object):
    
    def __init__(self, start_scene):
        pass

    def next_scene(self, scene_name):
        pass

    def opening_scene(self):
        pass


a_map = Map('central_corridor') # 将 a_map 设为类 Map 的一个实例。这里 'central_corridor' 是预设的测试参数?
a_game = Engine(a_map)          # 将 a_game 设为类 Engine 的一个实例。这里 a_map 是预设的测试参数?
a_game.play()                   # 从 a_game 中找到 play 函数,并调用。

这段代码主要是重复了架构图的内容,然后在最后写了三行测试代码看看能不能运行。


5.自顶向下和自底向上

之前的都是“自顶向下”的方法,就是从最抽象的顶层概念入手,一直向下做到具体的代码实现。

还有一种方法叫做“自底向上”,就是从代码下手,一直做到抽象概念。


一般步骤如下:

        1.取出要解决的问题中的一小块,写些代码让它差不多能工作。

        2.细化代码让它更为正式,比如加上类和自动测试。

        3.把关键概念抽取出来然后研究他们。

        4.把真正需要实现的东西描述出来。

        5.回去细化代码,有可能需要全部丢弃从头做起。

        6.在问题的另外一小块里重复上述流程。


Zed认为这个流程对于基础牢固的程序员更好使。当你知道一个大问题的小部分,但是对于整个总体的概念没有足够了解的时候,这种流程是非常好用的,不过刚刚开始的时候也许会走弯路。


6.《来自Percal 25号行星的哥顿人》的代码

现在我们准备些代码,这里最好是利用前面的代码骨架,为它添加功能直到它能实现为止。

由于我水平渣渣,这里就跟着 Zed 跑一次吧。


Zed把代码拆分成很多小块,我会写出完整的程序,并且我会试着添加注释。

===========================================================================================================

#-*-coding:utf-8-*- 
# 基本导入,没什么特别的  
from sys import exit
from random import randint

# 这里建立一个 Scene 基类,它包含了所有场景的通用信息。
class Scene(object):

    def enter(self): # 关于 self 还不熟练,部分可以参见 ex41
        print "This scene is not yet configured. Subclass it and implement enter()."
        exit(1) # 结束的时候返回 1

# 这里创建好 Engine 类,使用了 Map.opening_scene 和 Map.next_scene 这些方法。
# 这些方法其实还没有写好,后面才写。但是我们假设它们都写好了,这里只是拿来用。Map 类也是后面再写。
class Engine(object):

    def __init__(self, scene_map): # 都是这个格式
        self.scene_map = scene_map # 实例 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)

class Death(Scene):

    quips = [
        "You died. You kinda suck at this.",
        "Your mom would be proud...if she were smarter.",
        "Such a luser.", # 原文就是 luser
        "I have a small puppy that's better at this."
    ]

    def enter(self):
        print Death.quips[randint(0, len(self.quips)-1)] # 随机数是从 0 到 self.quip 的长度 -1 
        # 应该是随机输出四句话之一
        exit(1) # 退出,返回 1 

#===========================================================
# 没啥说的,就是在类 CentralCorridor 里面做一些事情,这是游戏的初始场景
class CentralCorridor(Scene):
# 定义 enter
    def enter(self):
        print "The Gothons of Planet Percal #25 have invaded your ship and destroyed"
        print "you entire crew. You are the last surviving member and your last"
        print "mission is to get the neutron destruct bomb from the Weapons Armory,"
        print "put it in the bridge, and blow the ship up after getting into an"
        print "escape pod."
        print "\n"
        print "You're running down the central corridor to the Weapons Armory when"
        print "a Gothon jumps out, red scaly skin, dark grimy teeth, and evil clown costume"
        print "flowing around his hate filled bosy. He's blocking the door to the"
        print "Armory and about to pull a weapon to blast you."

# action 等待玩家输入
        action = raw_input(">")
# 这里要缩进,书里面排版错误
        if action == "shoot!":
            print "Quick on the draw you yank out your blaster and fire it at the Gothon."
            print "His clown costume is flowing and moving around his body, which throws"
            print "off your aim. Your laser hits his costume but misses him entirely. This"
            print "completely ruins his brand new costume his mother bought him, which"
            print "makes himfly into a rage and blast you repeatedly in the face until"
            print "you are dead. Then he eats you."
            return 'death'

        elif action == "dodge!":
            print "Like a world class boxer you dodge, weave, slip and slide right"
            print "as the Gothon's blaster cranks a laser past your head."
            print "In the middle of your artful dodge your foot slips and you"
            print "bang your head on the metal wall and pass out."
            print "You wake up shortly after only to die as the Gothon stomps on"
            print "your head and eats you."
            return 'death'

        elif action == "tell a joke":
            print "Lucky for you they made you learn Gothon insults in the academy."
            print "You tell the one Gothon joke you know:"
            print "Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhaq gur ubhfr."
            print "The Gothon stops, tries not to laugh, then busts out laughing and can't move."
            print "While he's laughing you run up and shoot him square in the head"
            print "putting him down, then jump through the Weapon Armory door."
            return 'laser_weapon_armory'

        else:
            print "DOES NOT COMPUTE!"
            return 'central_corridor'

#===========================================================
# 接下来到了激光武器库.先做好场景,因为后面要引用这些场景
class LaserWeaponArmory(Scene):

    def enter(self):
        print "You do a dive roll into the Weapon Armory, crouch and scan the room"
        print "for more Gothons that might be hiding.  It's dead quiet, too quiet."
        print "You stand up and run to the far side of the room and find the"
        print "neutron bomb in its container.  There's a keypad lock on the box"
        print "and you need the code to get the bomb out.  If you get the code"
        print "wrong 10 times then the lock closes forever and you can't"
        print "get the bomb.  The code is 3 digits."
        code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9)) # 随机三位数
        guess = raw_input("[keypad]> ")
        guesses = 0 # 应该是尝试次数

        while guess != code and guesses < 10: # 尝试数量小于10
            print "BZZZZEDDD!"
            guesses += 1
            guess = raw_input("[keypad]> ")

        if guess == code:
            print "The container clicks open and the seal breaks, letting gas out."
            print "You grab the neutron bomb and run as fast as you can to the"
            print "bridge where you must place it in the right spot."
            return 'the_bridge'
        else:
            print "The lock buzzes one last time and then you hear a sickening"
            print "melting sound as the mechanism is fused together."
            print "You decide to sit there, and finally the Gothons blow up the"
            print "ship from their ship and you die."
            return 'death'



class TheBridge(Scene):

    def enter(self):
        print "You burst onto the Bridge with the netron destruct bomb"
        print "under your arm and surprise 5 Gothons who are trying to"
        print "take control of the ship.  Each of them has an even uglier"
        print "clown costume than the last.  They haven't pulled their"
        print "weapons out yet, as they see the active bomb under your"
        print "arm and don't want to set it off."

        action = raw_input("> ")

        if action == "throw the bomb":
            print "In a panic you throw the bomb at the group of Gothons"
            print "and make a leap for the door.  Right as you drop it a"
            print "Gothon shoots you right in the back killing you."
            print "As you die you see another Gothon frantically try to disarm"
            print "the bomb. You die knowing they will probably blow up when"
            print "it goes off."
            return 'death'

        elif action == "slowly place the bomb":
            print "You point your blaster at the bomb under your arm"
            print "and the Gothons put their hands up and start to sweat."
            print "You inch backward to the door, open it, and then carefully"
            print "place the bomb on the floor, pointing your blaster at it."
            print "You then jump back through the door, punch the close button"
            print "and blast the lock so the Gothons can't get out."
            print "Now that the bomb is placed you run to the escape pod to"
            print "get off this tin can."
            return 'escape_pod'
        else:
            print "DOES NOT COMPUTE!"
            return "the_bridge"


class EscapePod(Scene):

    def enter(self):
        print "You rush through the ship desperately trying to make it to"
        print "the escape pod before the whole ship explodes.  It seems like"
        print "hardly any Gothons are on the ship, so your run is clear of"
        print "interference.  You get to the chamber with the escape pods, and"
        print "now need to pick one to take.  Some of them could be damaged"
        print "but you don't have time to look.  There's 5 pods, which one"
        print "do you take?"

        good_pod = randint(1,5)
        guess = raw_input("[pod #]> ")


        if int(guess) != good_pod:
            print "You jump into pod %s and hit the eject button." % guess
            print "The pod escapes out into the void of space, then"
            print "implodes as the hull ruptures, crushing your body"
            print "into jam jelly."
            return 'death'
        else:
            print "You jump into pod %s and hit the eject button." % guess
            print "The pod easily slides out into space heading to"
            print "the planet below.  As it flies to the planet, you look"
            print "back and see your ship implode then explode like a"
            print "bright star, taking out the Gothon ship at the same"
            print "time.  You won!"

            return 'finished'

class Finished(Scene): # 胜利场景

    def enter(self):
        print "You won! Good job."
        return 'finished'

#===========================================================
# 这里是完成 Map 类
class Map(object):

    scenes = { # 把每个场景存在字典中
        'central_corridor': CentralCorridor(), # 这里都是跟前面的 return 挂钩
        'laser_weapon_armory': LaserWeaponArmory(),
        'the_bridge': TheBridge(),
        'escape_pod': EscapePod(),
        'death': Death(),
        'finished': Finished(),
    }

    def __init__(self, start_scene):
        self.start_scene = start_scene # 实例 start_scene 的属性是 start_scene

    def next_scene(self, scene_name):
        val = Map.scenes.get(scene_name) # 得到 map 中的 scenes 字典中的谁
        return val                       # 下一个场景就是这个

    def opening_scene(self):
        return self.next_scene(self.start_scene) # 打开场景,应该是从当前场景到下一个

a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()

附加练习:

1.为啥猜锁要猜11次?

因为是从0开始的,到 guesses = 10 才触发死亡,改成

guess = raw_input("[keypad]> ")
        guesses = 1 # 初始化尝试次数

        while guess != code and guesses < 10: # 尝试数量小于10
            print "BZZZZEDDD!"
            guesses += 1
            guess = raw_input("[keypad]> ")

就可以了。


2.解释房间切换原理

以一开始的房间 central_corridor 为例子,在输入 tell a joke 以后,

会 return 一个房间 'laser_weapon_armory' ,这就是下一个房间。

class Engine 里面有一个 while true,会一直等到调用 next_scene,调用时机应该就是 return的时候


我也是稀里糊涂的,希望以后有人能完整告诉我


3.难度大的房间就是猜密码了。

修改如下:

        while guess != code and guess!= "cheat" and guesses < 10: # 尝试数量小于10
            print "BZZZZEDDD!"
            guesses += 1
            guess = raw_input("[keypad]> ")

        if guess == code:
            print "The container clicks open and the seal breaks, letting gas out."
            print "You grab the neutron bomb and run as fast as you can to the"
            print "bridge where you must place it in the right spot."
            return 'the_bridge'

# 秘籍
        elif guess == "cheat":
            print "Cheating..."
            return 'the_bridge'
            
        else:
            print "The lock buzzes one last time and then you hear a sickening"
            print "melting sound as the mechanism is fused together."
            print "You decide to sit there, and finally the Gothons blow up the"
            print "ship from their ship and you die."
            return 'death'

这样挺简单的,但是不知道如何做得跟 Zed 一样更简单。


4. 格斗系统?

算了吧...













  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值