照着笨方法学python的书写了一个小游戏
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from sys import exit
from random import randint
def death():
quips = ["你死了,你有点傻","干得好,你死了","你是一个失败者","我的狗比你做得好"]
print quips[randint(0, len(quips) - 1)]
exit(1)
def central_corridor():
print "行星Gothons侵入了你的船"
print "你是最后一个幸存的成员"
print "你的任务是从武器军械库获得中子毁灭炸弹, "
print "把它放在船上,然后跑到分离舱内"
print "\n"
print "你正在沿着中央走廊跑到武器军械库"
print "一个Gothon跳出来,它有红色的鳞状皮肤,黑暗的肮脏的牙齿穿着邪恶的小丑服装"
print "他挡住了大门"
print "军械库即将爆炸"
print "现在你有三个选择,1.射击Gothon 2.逃跑 3.给他开个玩笑"
action = raw_input("> ")
if action == "1":
print "你向Gothon开火"
print "你的激光击中他的服装,但完全对他没有伤害"
print "他陷入一个疯狂的愤怒,并开始对你进行攻击"
print "你死了,然后他吃了你。"
return 'death'
elif action == "2":
print "在你巧妙的躲闪中,你摔倒了"
print "头撞在金属墙上,然后昏倒了。"
print "在Gothon踩踏你后,你醒了过来"
print "Gothon杀了你"
return 'death'
elif action == "3":
print "他们让你学习Gothon对你们的侮辱。"
print "你告诉Gothon一个笑话:"
print "Gothon顿停下来,然后大笑起来,不能动弹。"
print "当他笑的时候,你跑起来并向他开枪。"
print "然后绕过他,跳到武器军械库的门口。"
return 'laser_weapon_armory'
else:
print "请重新输入答案,1、2或3"
return 'central_corridor'
def laser_weapon_armory():
print "你在武器库里俯冲,蹲伏,扫视房间"
print "为了躲藏更多的Gothon"
print "你站起来跑向房间的远侧"
print "发现中子弹在箱子里,箱子上有一个密码锁"
print "你需要密码才能把炸弹拿出来"
print "错了10次,锁就永远关上了,你不能得到炸弹了"
print "密码是3位数字,每一位都由1到9随机组成"
code = "%d%d%d" % (randint(9, 9), randint(9, 9),
randint(9, 9))
guess = raw_input("[keypad]> ")
guesses = 0
while guess != code and guesses < 10:
print "输入错误"
guesses += 1
guess = raw_input("[keypad]> ")
if guess == code:
print "箱子咔哒一声打开"
print "你抓住中子弹,尽快跑出去"
print "你必须把它放在正确的位置。"
return 'the_bridge'
else:
print "锁最后一次嗡嗡响"
print "炸弹融化"
print "Gothon杀了你"
print "你死了"
return 'death'
def the_bridge():
print "此时Gothon已经到了你身边"
print "此时你有两个选择1.扔掉炸弹2.用炸弹要挟他们"
action = raw_input("> ")
if action == "1":
print "在惊恐中你把炸弹扔给Gothon"
print "然后你跑向门"
print "Gothon从你背后杀了你"
print "当你死的时候,你看到另一个Gothon疯狂地试图解除炸弹。"
print "你希望炸弹爆炸"
print "但是它熄灭了。"
return 'death'
elif action == "2":
print "你拿炸弹要挟他们,想和他们同归于尽"
print "Gothons举起双手开始出汗。"
print "你后退到门前,小心地打开了门。"
print "你把炸弹放到了地板上"
print "然后你跑出门,并关上了门"
print "然后把锁锁上,这样Gothon就逃不出去了。"
print "现在炸弹被放置好了,你需要逃到逃生舱才能不被炸伤"
return 'escape_pod'
else:
print "请输入有效答案1或2"
return "the_bridge"
def escape_pod():
print "现在需要跳到一个分离舱,才能躲避船体爆炸"
print "但是你没有时间去看。有分离舱(1-5),只有一个能与船体分离,你选哪一个?"
good_pod = "%d"%(randint(2, 2))
guess = raw_input("请输入一个数字>")
if guess != good_pod:
print "你跳到分离舱%s并按下弹出按钮。" %guess
print "分离舱没有与船分离"
print "你被炸死了"
return death()
else:
print "你跳到分离舱%s并按下弹出按钮。" %guess
print "分离舱与船分离"
print "你赢了!"
exit(0)
ROOMS = {
'death': death,
'central_corridor': central_corridor,
'laser_weapon_armory': laser_weapon_armory,
'the_bridge': the_bridge,
'escape_pod': escape_pod
}
def runner(map, start):
next = start
while True:
room = map[next]
print "\n--------"
next = room()
runner(ROOMS, 'central_corridor')
注:
ROOMS = {
'death': death,
'central_corridor': central_corridor,
'laser_weapon_armory': laser_weapon_armory,
'the_bridge': the_bridge,
'escape_pod': escape_pod
}
def runner(map, start):
next = start
while True:
room = map[next]
print "\n--------"
next = room()
runner(ROOMS, 'central_corridor')
这段代码可以改为
central_corridor()
然后把代码中的所有return '…'改为return …
如return "the_bridge"改为return the_bridge