1. A game

 from sys import exit
from random import randint
from textwrap import dedent # 使可以使用三引号型的字符串 # 具体实现了场景间的切换
class Engine(object): def __init__(self, scene_map):
self.scene_map = scene_map def play(self):
current_scene = self.scene_map.opening_scene() # 初始场景的类
last_scene = self.scene_map.next_scene('finished') while current_scene != last_scene:
next_scene_name = current_scene.enter() # 返回一个场景的名字
current_scene = self.scene_map.next_scene(next_scene_name) # 确保print出最后一个场景
current_scene.enter() # 各场景共有的父类
class Scene(object): def enter(self):
pass # 具体的每个场景,其中定义了一些说明和交互
class Death(Scene): quips = ["You died.", "Please try again.", "That's not very good."] # 类变量,可以通过类名调用 def enter(self):
print(Death.quips[randint(0,2)])
exit(1) class CentralCorridor(Scene): def enter(self):
print("""
You are met with some strange things.
This is a locked room.
It will be a good idea to leave right away.
You see a Gothon blocking the door.
Try to do something.
""")
action = input('>')
if action == 'shoot!':
return 'death'
elif action == 'tell a joke!':
return 'laser_weapon_armory'
else:
print("Nothing happened.")
return 'central_corridor' class LaserWeaponArmory(Scene): def enter(self):
print("""
This is the laser weapon armory.
Destroy their weapon!
Please enter the code.
""") code = ""
guess = input('>')
guesses = 0 while guess != code and guesses < 5:
print("That's not right!")
guesses += 1
guess = input('Try again >') if guess == code:
print("Bingo! The weapon is destroyed!")
return 'the_bridge'
else:
print("A bell rings and you are locked here to die!")
return 'death' class TheBridge(Scene): def enter(self):
print("A bridge is in front of you and you see a huge bomb. ")
action = input('>') if action == "throw it away":
print("It exploded as you aprroached it. You died.")
return 'death'
elif action == "leave it behind":
print("It seems nothing happened. You can safely pass it.")
return 'escape_pod'
else:
return 'death' class EscapeRod(Scene): def enter(self):
print("""
You finally make it to the escape rod.
There are five ships in front of you.
Which one do you want to take?
""")
num = input('>')
correct_ship = ''
if num == correct_ship:
print("You got on the ship. Everything seems getting on well. You won!")
return 'finished'
else:
print("The ship is not working well. It exploded as take off!. You died!")
return 'death' class Finished(Scene): def enter(self):
print("Congratulations! You successfully escaped from the ship!")
return 'finished' # 定义了各场景的名字,定义了进出场景的方法
class Map(object): scenes = {'central_corridor': CentralCorridor(),
'laser_weapon_armory': LaserWeaponArmory(),
'the_bridge': TheBridge(),
'escape_pod': EscapeRod(),
'death': Death(),
'finished': Finished()
} def __init__(self, start_scene):
self.start_scene = start_scene def next_scene(self, scene_name):
nextScene = Map.scenes.get(scene_name)
return nextScene # 根据传入名称返回一个场景的类 def opening_scene(self):
return self.next_scene(self.start_scene) # 根据出发点名称返回出发点场景的类 a_map = Map("central_corridor")
a_game = Engine(a_map)
a_game.play()

【Python基础】lpthw - Exercise 43 基本的面向对象分析和设计的更多相关文章

  1. 解析UML的面向对象分析与设计

    经常听到有朋友抱怨,说学了UML不知该怎么用,或者画了UML却觉得没什么作用.其实,就UML本身来说,它只是一种交流工具,它作为一种标准化交流符号,在OOA&D过程中开发人员间甚至开发人员与客 ...

  2. UML和模式应用学习笔记-1(面向对象分析和设计)

    UML和模式应用学习笔记-1(面向对象分析和设计) 而只是对情节的记录:此处的用例场景为:游戏者请求掷骰子.系统展示结果:如果骰子的总点数是7,则游戏者赢得游戏,否则为输 (2)定义领域模型:在领域模 ...

  3. 《UML和模式应用》读书笔记(一)面向对象分析和设计简单示例

    在开始进行对象分析和设计之前,先通过“扔骰子”这个软件(游戏者扔两个骰子,如果总是是7,则赢,否则输),来简单分析下这个过程. 1:用例 需求分析,可能包括人们如何应用的场景或情节,这些都可以被编写成 ...

  4. 面向对象分析与设计—OOD部分

    第三部分 面向对象设计 3.1 面向对象设计(OOD)的定义? 在面向对象分析阶段,已经针对用户需求建立起用面向对象概念描述的系统分析模型.在设计阶段,要考虑为实现系统而采用的计算机设备.操作系统.网 ...

  5. .NET应用架构设计—面向对象分析与设计四色原型模式(彩色建模、领域无关模型)(概念版)

    阅读目录: 1.背景介绍 2.问自己,UML对你来说有意义吗?它帮助过你对系统进行分析.建模吗? 3.一直以来其实我们被一个缝隙隔开了,使我们对OOAD遥不可及 4.四色原型模式填补这个历史缝隙,让我 ...

  6. 面向对象分析与设计—OOA部分

    第二部分 面向对象分析 2.1 面向对象分析(OOA)的定义? OOA——面向对象的分析,就是运用面向对象方法进行系统分析,对问题域(问题所涉及的范围)和系统责任(所开发的系统应具备的职能)进行分析与 ...

  7. python基础-第七篇-7.2面向对象(进阶篇)

    进入到今天的探索前,我先对上节内容进行一下回顾: 面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用 类是一个模板,模板中包装了多个函数可供使用 对象是基于类创建的,实例用于调用被包装在类 ...

  8. python基础之递归,声明式编程,面向对象(一)

    在函数内部,可以调用其他函数,如果一个函数在内部调用自身本身,这个函数就是递归函数.递归效率低,需要在进入下一次递归时保留当前的状态,解决方法是尾递归,即在函数的最后一步(而非最后一行)调用自己,但是 ...

  9. python 基础之第十一天(面向对象)

    #############面向对象##################### 类: In [1]: class MyClass(object): ##用class定义一个类 ...: def psta ...

随机推荐

  1. SQL Server - ISNULL

    ISNULL 使用指定的替换值替换 NULL. 语法       ISNULL ( check_expression , replacement_value ) 参数    check_express ...

  2. JS 禁用鼠标右键

    oncontextmenu="window.event.returnValue=false" style="overflow-y: hidden; overflow-x: ...

  3. 拓扑排序(Topological Sorting)

    一.什么是拓扑排序 在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列.且该序列必须满足下面两个 ...

  4. Object.freeze(); 方法冻结一个对象。

    Object.freeze() 方法可以冻结一个对象.一个被冻结的对象再也不能被修改: 冻结了一个对象则不能向这个对象添加新的属性,不能删除已有属性,不能修改该对象已有属性的可枚举性.可配置性.可写性 ...

  5. js操作:selenium无法操作隐藏元素问题

    对于前端隐藏元素,一直是selenium自动化定位元素的隐形杀手,脚本跑到隐藏元素时位置时报各种各样的错误, 隐藏的下拉菜单又没有办法避免,此帖只为交流隐藏元素自动化定位处理方法(3种操作)      ...

  6. ffmpeg学习目录收集

    ffmpeg工具参数中文详细解释 雷霄骅 - [总结]FFMPEG视音频编解码零基础学习方法

  7. shell生成rsync同步脚本

    test #!/bin/bash # # Rsync Install Script # Last Updated # ##### modify by Jinayf ##### ######手动修改以下 ...

  8. Android 基础一 TextView,Style样式,Activity 传值,选择CheckBox 显示密码

    1.修改TextView字体 mTextView = (TextView) findViewById(R.id.textview1); mTextView.setText("I am her ...

  9. tensorflow结果可视化-【老鱼学tensorflow】

    这次我们把上次的结果进行可视化显示,我们会把神经网络的优化过程以图像的方式展示出来,方便我们了解神经网络是如何进行优化的. 首先,我们把测试数据显示出来: # 显示测试数据 fig = plt.fig ...

  10. 安装酷痞到IIS7.x共用80端口Windows(64位)系统下运行多个酷痞

    需求: 1.酷痞直接运行的模式是自宿主运行.由于win系统一般都由iis提供多个网站服务,并首先占用了80端口,如果想酷痞可以直接通过主机头和iis共用80端口会出现这种运行模式的需求. 以下是实现方 ...