# -*- coding: utf-8 -*-
from sys import exit
from random import randint
class Scene(object):
def enter(self):
print "This scene is not yet configured. Subclass it and implement enter()."
exit(1)
class Engine(object):
# 初始化接受一个Map类型
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) # 将下个游戏场景置为当前场景,循环至退出
class Death (Scene):
quips = ["你挂啦,可长点心吧",
"愚蠢的人类,接受制裁吧",
"你怕不是闭着眼睛乱按的",
"我觉得我家的哈皮都玩的比你好,略略略"]
def enter(self):
print Death.quips[randint(0, len(self.quips)-1)]
exit(1)
class CentralCorridor(Scene):
def enter(self):
print " 行星纪元233年,地球人类已经走出太阳系,开始向宇宙深处进行探索。"
print "人类联合政府(GHU)培训并选拔了一批精英宇航员,将他们送往太空以寻找"
print "适合人类居住的行星。预计在10年内陆续发射560艘'宙斯'级飞船(可容2048人)。"
print "235年,你乘坐'盖娅'号离开了地球,飞向了未知的'森林'。。。来自Percal25"
print "号行星的哥顿人劫持了你所在的飞船并且破坏了飞船上的通讯设备"
print " 全体船员阵亡,你是唯一的幸存者。"
print "\n"
print "最终任务:从武器库中获得中子弹,将其放置在主控舱,最后乘坐救生舱脱离。"
print "\n"
print " 你准备穿过中央走廊进入武器库,一个哥顿人突然出现,绿色的地中海发型,尖利突出的牙齿,"
print "佩戴着类似黑框眼镜的物件,身着奇异外套,望向你的眼睛含着泪水(也许)。他挡在武器库前,"
print "手上的武器已经对准了你。你准备......"
print "\n"
print "1.开枪,先下手为强(输入:shoot!)"
print "2.躲避,不能硬钢(输入:dodge!)"
print "3.给哥顿人讲个笑话(输入:tell a joke)"
print "请输入后面的英文指令"
action = raw_input("> ")
if action == "shoot!":
print " 你迅速开枪射击,哥顿人身上的奇怪服"
print "装似乎在前方生成了一个薄膜挡下了所有的火力."
print "只见一束激光从他的枪中射出,瞬间切断了你的"
print "左臂,你连开几枪,但仍然无法击穿敌方的护甲."
print " 他的外套是他母亲在某地摊上买的,"
print "那个黑框眼镜似的物件是一位长者所馈赠"
print "哥顿人飞升起来,对着你的膝盖就是一拳"
print "他就这样饶有趣味的看你挣扎了10多分钟"
print "之后,开枪杀掉了你。"
print "你没得了"
return 'death'
if action == "dodge!":
print " 你发现身后有个供躲避的掩体,"
print "你躲藏起来,准备寻找合适的时机。"
print "哥顿人直接一枪就精准透过掩体地射穿了你的大脑,"
print "你最后的意识停留在'黑框眼镜'上,然后便消散了。"
print "哥顿人对你的脑子很感兴趣,开始研究了起来。"
print "哎呦,你死了,但这才刚开始"
return 'death'
if action == "tell a joke":
print "你的头盔自带翻译功能"
print "你挑了一个自认为非常搞笑的笑话(其实不好笑):"
print "是金吉拉房价,很高就阿瑟东,方今阿飞骄傲哦加减法了,房价"
print "."*10
print "哥顿人尝试去忍,但是还是笑的停不下来"
print "你趁机在他后脑勺来了一枪"
print "放倒哥顿人后,在武器库门外扫描了自己滴手纹"
print "输入密码,打开了库门。"
return 'laser_weapon_armory'
else:
print "你可能是输错了,对吧"
return 'central_corridor'
class LaserWeaponArmory(Scene):
def enter(self):
print "虽然现在暂时安全了,但不知道哥顿人何时会发现这里。"
print "四周安静的可怕,也许这里面也藏着哥顿人。"
print "你小心地打开扫描仪,不能贸然开灯"
print "你搜寻着中子弹的位置,小步且快速地贴近"
print "万幸,中子弹还躺在盒子中。"
print "盒子被锁住了,旁边有一个可供输入的数字键盘。"
print "看来必须得输入密码才能打开。"
print "注意:你共有十次机会,超出,容器会自动锁定,你将永远无法获得中子弹。"
print "提示:密码是三位数。"
code = "%d%d%d" % (randint(0,9), randint(0,9), randint(0,9))
uscript = raw_input("哇靠,这太难了,是否需要提示 yes/no? > ")
if uscript == "yes":
print "密码是%s。" % code
else:
pass
input = raw_input("[键入]> ")
guesses = 0
while code != input and guesses <9:
print "error.请重新输入。"
guesses += 1
input = raw_input("[键入密码]> ")
if code == input:
print "容器已经打开。"
print "成功获取中子弹,你需要将他安装在主控舱的正确位置"
print "前往主控舱。"
return 'the_bridge'
else:
print "核弹已被永久锁定。"
print "武器库的警报自动开启。"
print "任务已经失败,你颓然的坐在这里。"
print "警报声吸引了许多哥顿人,等你回过神来已经被抓住了。"
print "看着被炸坏的武器库舱门,你知道死神已近。"
return 'death'
class TheBridge(Scene):
def enter(self):
print "你带着中子弹进入了主控舱。"
print "这里有几个哥顿人正在尝试控制这艘飞船"
print "他们看到准备进来的你,正要拔出自己的武器。"
print "你将装有中子弹的箱子放在自己胸前,"
print "用他们的语言说了句'中子弹',哥顿人一时间投鼠忌器。"
print "接下来你准备:"
print "1.丢他们炸弹(throw the bomb)"
print "2.设置炸弹,一点不慌(slowly place the bomb)"
action = raw_input('> ')
if action == "throw the bomb":
print "哥顿人接住了你的炸弹,"
print "并反手赏了你一发激光。"
print "你一定只是好奇这个选择会发什么"
print "我们这可是很严肃的游戏(真滴)"
print "这么搞笑的选项结果显而易见啦。"
return 'death'
elif action == "slowly place the bomb":
print "这种时候肯定是要淡定的啦"
print "威胁哥顿人不要轻举妄动"
print "似乎是漫不经心地跟他们来个6人转,"
print "让哥顿人自己走到你的圈套中"
print "你按下一个按钮,一个透明屏障将哥顿人困住"
print "迅速安装好炸弹后,再通过主控舱的紧急通道去乘坐救生舱"
return 'escape_pod'
else:
print "没有其他的选择了,怎么的(凶)"
return 'the_bridge'
class EscapePod(Scene):
def enter(self):
print "你费劲千幸万苦来到这里,但这里的设备已经损毁严重"
print "这儿有5艘救生飞船"
print "是时候做出选择了"
good_pod = randint(1,5)
tip = raw_input('需要提示吗 yes/no?> ')
if tip == 'yes':
print good_pod
else:
pass
guess = int(raw_input('乘坐几号飞船> '))
if guess != good_pod:
print "很可惜,你没能做出正确的选择。"
print "那天,你化成了一束光。"
print "茫茫宇宙的尘埃"
return 'death'
else:
print "你跳进%d号飞船" % guess
print "运气不错,成功启动了飞船,逃离了。"
print "你回头看去。"
print "'盖娅'号短暂的照亮了这片宇宙"
print "你赢了,恭喜"
exit(1)
class Map(object):
scenes = {'death': Death(),
'central_corridor': CentralCorridor(),
'laser_weapon_armory': LaserWeaponArmory(),
'the_bridge': TheBridge(),
'escape_pod': EscapePod()
}
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) # 根据场景名返回场景类
a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()
小结:
一开始我哪知道游戏引擎该干什么。
但读懂程序后还是很有收获得,这其实是一个"有限状态机"(finite state machine)
这个游戏巨坑,猜数字也太变态了
也许修改为死亡后从当前场景复活体验也不会好到哪去。