1. 程式人生 > >ex36 自己編的一個冒險小遊戲(未完待續)

ex36 自己編的一個冒險小遊戲(未完待續)

may num eth front exit import dea org island

  1 #-*- coding: UTF-8 -*-
  2 from sys import exit
  3 #作出判斷選擇進森林還是出海冒險
  4 def start():
  5     print "Now you are on an island,"
  6     print "you got a lot of things,but you‘re lonely and bored at your surroundings,"
  7     print "you gonna do something."
  8     do_sth = raw_input(">")
  9
if "yes" in do_sth: 10 print "OK,now let‘s make a choice for what to do next." 11 print "You can go inside the forest or you can take a ship to the sea." 12 choice = raw_input(">") 13 if "forest" in choice: 14 forest() 15 elif "sea" in
choice: 16 sea() 17 else: 18 dead("you‘ve just made the wrong choice,and you died.please restart the game.") 19 else: 20 print "Come on man ,don‘t be a sluggard ,say yes and let‘move." 21 start() 22 23 24 25 26 #定義死亡:打印死亡原因,並退出遊戲
27 def dead(why): 28 print why,"What a pity." 29 exit(0) 30 31 #定義 兔子 事件,選擇“shoot"之後,包裏有槍就打死兔子並帶走 32 def rabbit(): 33 print "Now there is a rabbit in front of you.Propably you can shoot it and take it as your prey." 34 print "shoot or not?" 35 choice_of_rabit = raw_input(">") 36 if "sho" in choice_of_rabit :#為什麽這裏用 if choice_of_rabit = "shoot"就不行? 37 if "gun" in package: 38 print "Bang,you got it!awesome."#如果我想要任何時候輸入一個指令都能夠查看我包裹裏的東西,該怎麽做? 39 package.append("dead rabbit") 40 print "Check your package.",package#這麽寫行麽? 41 else: 42 print "Stupid! You forgot to take a gun." 43 else: 44 print "God bless you.Go ahead." 45 46 #定義“熊”事件,包含“run"和”fight"兩種應對模式 47 def bear(): 48 print "Hey,watch out man." 49 cotinue = raw_input(">") 50 print "There is a bear in front of you ,and it‘s staring at you ." 51 cotinue = raw_input(">") 52 print "So what would do? run? or fight?" 53 while True: 54 choice_of_bear1 = raw_input(">") 55 if "run" in choice_of_bear1: 56 run() 57 break 58 elif "fight" in choice_of_bear1: 59 fight() 60 break 61 else: 62 print"illeagal input,please try again." 63 64 65 #逃跑 函數,需要判斷包裹內物品然後決定生死 66 def run (): 67 if "dead rabbit" in package: 68 print "Oh,I got an idea,maybe you can throw your rabbit so the bear would be distracted." 69 cotinue = raw_input(">") 70 print"Do you wanna throw your rabbit?" 71 while True: 72 choice_of_run = raw_input(">") 73 if "yes" in choice_of_run: 74 print"Excellent,the bear is eating the rabbit,and you made it to run." 75 break 76 elif "no" in choice_of_run: 77 dead("You died because of your mean") 78 break 79 else: 80 "Illeagal input ,say yes or no." 81 else: 82 dead("The bear catch you and eats you.") 83 84 #戰鬥 函數,同樣判斷包裹內物品決定生死,但我覺得其實應該可以在使用axe後者dagger哪裏不設置死亡,而是造成不同傷害值。 85 def fight(): 86 print "Now my hero,pick up something from your bag,and start fighting." 87 print "What do you want?" 88 while True: 89 choice_of_fight = raw_input(">") 90 if choice_of_fight in package: 91 if "gun" in choice_of_fight: 92 print"BANG!BANG BANG BANG!You shoot it,and the bear died." 93 break 94 else: 95 dead("You fight with %s,you‘re so brave,but still killed by the bear." %choice_of_fight) 96 else: 97 print "You don‘t have a %s,please make sure it‘s in your package." %choice_of_fight 98 99 100 101 102 103 104 105 106 107 108 109 #整個森林冒險函數 110 111 def forest(): 112 ware_house = ["water","axe","dagger","bread","gun","boots","electric torch","compass","life jacket","beer","dog"] 113 print "You need to make a package before you leave.You can only choose five stuffs.Now go and get what you want." 114 print ware_house 115 stuff_number = len (package) 116 while stuff_number < 5: 117 stuffs = raw_input(">") 118 if stuffs in ware_house : 119 package.append(stuffs) 120 ware_house.remove(stuffs) 121 print "Now that what you have:",package 122 print "You can also take something from ware_house.",ware_house#這裏想不明白:第五次輸入之後,按理說應該直接到“You‘ve taken enough things "那裏,但是現在的情況是還會多打印兩行 123 else: 124 print "Wrong input,please try again." 125 stuff_number = len (package) 126 else: 127 print "You‘ve taken enough things ,or it will be to heavy." 128 cotinue = raw_input(">") 129 print"--------------------------------------------------------" 130 print"Now let‘s start the journey." 131 print"There is a mushroom on the roadside,you wanna take it?" 132 133 while True: 134 choice_mshrom = raw_input(">") 135 if "yes" in choice_mshrom: 136 package.append("mushroom") 137 print "OK,you have mushroom in your package now." 138 break 139 elif "no" in choice_mshrom: 140 print "Well,a good choice,Ah~" 141 break 142 else: 143 print"illeagal input,say yes or no."#這裏如果出現非法輸入,應當是回到前面的,怎麽實現該?解決方案:While True 來實現。 144 print"--------------------------------------------------------" 145 rabbit() 146 print"--------------------------------------------------------" 147 bear() 148 149 #要想正常運行那些函數,首先要定義一個package,否則會出錯。 150 package = [] 151 start() 152 153 154 155 156

ex36 自己編的一個冒險小遊戲(未完待續)