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. iTOP-4412开发板_驱动_adc驱动升级和测试例程

    本文档介绍 iTOP-4412 开发板的 adc 驱动的升级和测试例程.自带的驱动只能支持一路 adc,本文介绍如何修改可以支持 4 路 adc 的控制.1 硬件简介如下图所示,这是 4412 的 d ...

  2. yum安装mysql

    安装 CentOS7默认数据库是mariadb,配置等用着不习惯,因此决定改成mysql,但是CentOS7的yum源中默认好像是没有mysql的.为了解决这个问题,我们要先下载mysql的repo源 ...

  3. PHP -- 七牛云 在线视频 获取某一帧作为封面图

    ### 最近碰到视频处理,需要视频封面? 但用的是七牛云存储视频,索性搜了一下,怎么获取视频的某一帧作为视频的封面图... 发现了七牛官网又自身的接口 ### https://developer.qi ...

  4. 关于 UNIX 的哲理名言(中英文对照)

    UNIX 的特点: Everything (including hardware) is a file.所有的事物(甚至硬件本身)都是一个的文件. Configuration data stored ...

  5. java学习 之 java基本数据类型

    java 8个基本数据类型 public class BasicDataType { public static void main(String args[]) { //数据类型 //Byte Sy ...

  6. 纯css画直角三角形

    所有的三角形,都是通过盒子模型来设定. border(边框)的不同大小来决定 border-width: 边框的宽度 border-style: 边框的样式 border-color: 边框的颜色 1 ...

  7. 咸鱼入门到放弃8--jsp<三>jsp内置对象

    NO. 内置对象 类型 1 pageContext javax.servlet.jsp.PageContext 2 request javax.servlet.http.HttpServletRequ ...

  8. python3 使用代理

    #代理使用 >>> proxy_handler=urllib.request.ProxyHandler({'http':'211.81.31.18:8081'}) >>& ...

  9. Spark环境搭建(一)-----------HDFS分布式文件系统搭建

    下载的压缩文件放在~/software/    解压安装在~/app/ 一:分布式文件系统搭建HDFS 1,下载Hadoop HDFS环境搭建    使用版本:hadoop-2.6.0-cdh5.7. ...

  10. 15树莓派安装图形界面截图工具Shutter

    2017-09-27 13:02:54 参数-添加/删除软件-搜索软件名称“shutter”,勾选,点击“应用”,等待安装完成后点击“确定”.(需下载安装包后安装) 补充:更多截图软件可以https: ...