ex35 分支与函数——一个丧心病狂的小冒险游戏
码的我好累,直接上代码了。
1 #-*- coding: UTF-8 -*- 2 from sys import exit 3 4 def gold_room(): 5 print "This room is full of gold. How much do you take?" 6 7 next = raw_input(">") 8 if "0" in next or "1" in next:#书上原来的办法,通过0和1来判断输入的是否为数字,但是这种情况,如果输入2,那就不适用了。 9 how_much = int(next) 10 if str(next).isdigit() is True:#改进后的方法,通过isdigit判断输入是否为数字,是则返回True,然后下面通过int来把输入的字符串转化为整型。 11 how_much = int(next) 12 else: 13 dead("Man,learn to type a number.") 14 15 if how_much < 50:#判断输入大小 16 print "Nice,you're not greedy,you win." 17 exit(0)#用exit(o)来表示程序是否是由于遇到错误而中断退出的...其实这个还是不太懂 18 else: 19 dead("You greedy bastard.") 20 21 22 23 def bear_room(): 24 print "There is a bear here." 25 print "The bear has a bunch of honey." 26 print "The fat beer is in front of another door." 27 print "How are you going to move the bear?" 28 bear_moved = False#这里很巧妙,因为接下来的游戏是需要特定的有先后两步的操作才能完成,否则都视为失败, 29 30 while True : 31 next = raw_input(">") 32 33 if next == "take honey": 34 dead("The bear looks at you then slaps your face off.") 35 elif next == "taunt bear" and not bear_moved:#第一步必须是taunt_bear,完成这一步之后bear_moved变成了True。 36 print "The bear has moved from the door.You can go through it now." 37 bear_moved = True 38 elif next == "taunt bear " and bear_moved:#这里的意思是,你逗弄过熊了还要去逗,人家毛了。 39 dead("The bear gets pissed off and chews your leg off.") 40 elif next == "open door" and bear_moved:#只有在完成了第一步之后,第二步输入“open door",这一命令才会返回True。 41 gold_room() 42 else: 43 print "I got no ideas what that means." 44 45 46 def cthulhu_room(): 47 print "here you see the great evil Cthulhu." 48 print "He,it and whatever stars at you and you go insane." 49 print "Do you flee for your life or eat your head?" 50 51 next = raw_input(">") 52 53 if "flee" in next: 54 start() 55 elif "head" in next: 56 dead("Well that was tasty!") 57 else: 58 cthulhu_room() 59 60 61 def dead(why): 62 print why,"Good job!" 63 exit(0) 64 65 def start(): 66 print "You are in a dark room." 67 print "There is a door to your right and left." 68 print "Which one do you take?" 69 70 next = raw_input(">") 71 72 if next == "left": 73 bear_room() 74 elif next == "right": 75 cthulhu_room() 76 else: 77 dead("You stumble around the room unti you starve.") 78 79 start()#如果前面那个没加完整的括号,后边就会报错。