ex45 自己制作一个游戏

好难,于是我就基本照着ex43的作者写的游戏写了一遍,多加了tips模块和一个格斗系统的类。

目前存在的问题是

1、我希望能够随时调用help秘笈。但是没能够实现;

2、class FightSystem()实在是太乱了,我都受不了了,可是又暂时不想花时间去完善,毕竟这个界面已经快看吐了。

3、缺少好的注释


 

下面学习一下ex45里边提到的一些编程风格,习惯方面的东西。

1、函数风格

  • 函数命名的时候可以命名成一个动词
  • 要让函数保持小巧精巧,看看你的FightIngSystem,成啥样了都。

2、类的风格

  • 类的命名使用驼峰式大小写:FightIngSystem;函数命名添加下划线:fighting_system;
  • __init__函数不应该做太多事,越简单越好;
  • 用一致的方式处理函数参数。如果类需要处理users、dogs、和cats,就保持这个顺序,所有函数的顺序都保持(users, dogs, cats)
  • 永远使用 class Name (object) 的方式定义类,否则会遇到大麻烦(虽然我并不知道还能用什么方式来定义类)

3、代码风格

  • 为了方便阅读,要在自己的代码字符之间留下一些空白。方便大脑通过空白和垂直对齐的位置来扫描和区隔视觉元素。
  • 一定要注意代码的易读性!如果一段代码你无法朗读出来,那么这个代码的可读性可能就有问题。
  • 如果喜欢某个人的代码风格,可以试着去模仿,直到哪天找到自己的风格;
  • 不要把风格太当回事儿,因为程序员工作的一部分就是和别人写的代码打交道。严以律己,宽以待人。

4、好的注释

  • 写注释的时候写清楚“为什么要这样做”
  • 注释要短小精悍,并且在代码更改后记得更新。
  • That'it .

以下是代码。只能保证基本运行,很多方面还未完善。只是最近看这个实在看腻了,等后边有什么时间我再来修改一下。

  1 #-*- coding: UTF-8 -*-
  2 from random import randint
  3 from sys import exit
  4 import tips
  5 import fighting_system 
  6 
  7 
  8 class Scene(object):
  9 
 10     def enter(selt):
 11         print "It's just a base class for for different scenes, to make them easy to recognize."
 12         exit(1)
 13 
 14 class Engine(object):#需要实例化并输入参数,启动对象是整个地图。#地图要有的功能:切换地图,打开地图
 15     def __init__(self,scene_map):
 16         self.scene_map = scene_map #这是要初始化的,规矩。
 17     def play(self):
 18         current_scene = self.scene_map.opening_scene()#对象是整个地图嘛,那么在通过实例化给scene_map之后,自然就是直接调用#那么自然最后就是返回到某个具体的scene了。
 19         #play的作用在于在map不断切换地图之后,需要再切换之后随即打开
 20         while True:
 21             print "\n------------"
 22             next_scene_name = current_scene.enter()
 23             current_scene = self.scene_map.next_scene(next_scene_name)
 24         
 25         #遗留问题:1、以第一个场景子类为例,为什么后边调用的是enter函数,而不是直接进入。另外,为什么if语句什么的是放在定义的函数外面。放在函数中会出现什么情况?
 26         
 27         
 28 class FigthingSystem(Scene):
 29 
 30             
 31     def enter(self):
 32         hero_skill_dict = {"A":2,
 33                    "B":4,
 34                    "C":6,
 35                    "D":8
 36         }    
 37         Gothons_skill_dict = {"a":1,
 38                       "b":3,
 39                       "c":5,
 40                       "d":7
 41         }
 42         hero_skills_list = hero_skill_dict.keys()
 43         Gothon_skills_list = Gothons_skill_dict.keys()
 44         print "OK, now you can start fighting~~"
 45         hero_blood = 10
 46         Gothon_blood = 8
 47         while hero_blood > 0 and Gothon_blood > 0:
 48             print "Here are your skills:\n%s"%hero_skills_list
 49             print "Your blood:%s ; Gothon's blood :%s" %(hero_blood,Gothon_blood)
 50             hero_hit = raw_input(">")
 51             if hero_hit in hero_skills_list:
 52                 Gothon_hit = Gothon_skills_list[randint(0,len(Gothon_skills_list)-1)]
 53                 print "Gothon:%s" %Gothon_hit
 54                 hero_value = hero_skill_dict[hero_hit]
 55                 Gothon_value = Gothons_skill_dict[Gothon_hit]
 56                 if hero_value > Gothon_value:
 57                     d = hero_value - Gothon_value
 58                     Gothon_blood = Gothon_blood - d
 59                 
 60                 elif hero_value < Gothon_value:
 61                     d = Gothon_value - hero_value
 62                     hero_blood = hero_blood -d
 63                 print "Your blood:%s ; Gothon's blood :%s" %(hero_blood,Gothon_blood)
 64             else:
 65                 print "Illeagle input, please try again."
 66     
 67         if hero_blood > 0 and Gothon_blood <=0:
 68             print "Hey,good job, you beats the Gothon,and you can go through the door now."
 69             return 'weapon_aromory'
 70         elif hero_blood <= 0 and Gothon_blood > 0:
 71             print "I'm sorry you are defeated,game over."
 72             print "GAME OVER!!!"*5
 73             return 'death'
 74         
 75 
 76         
 77                 
 78         
 79 #打出来的情况比较糟糕,后期调试主意完善        
 80 class Death(Scene):
 81 
 82     greeting = [
 83         "You really suck at this,maybe you can restart again.",
 84         "You know what?You died hahahahaha,stand up and have a rest,if you wanna try it again,go ahead.",
 85         "Your mom is calling you for dinner, put down your keyboard,cause you've already died.",
 86         "Sing a song ,I'll let you back to the game.HAHAHA you know it's a joke,byebye."
 87     ]
 88     
 89     def enter(self):
 90         print Death.greeting[randint(0,len(self.greeting)-1)]
 91         exit(1)
 92     
 93 class CentralCorridor(Scene):
 94 #实现功能:1、介绍游戏背景;2、介绍本场景情况;3、进入格斗系统;4、能够调用秘笈
 95     def enter(self):
 96         print "The Gothons of planet #25 have invaded your ship and destored"
 97         print "your entire crew.You are the last surviving member and your last"#每行不超过79个字符,这是规定
 98         print "mission is to get the bomb from the weapons armory,put it in the"
 99         print "bridge,destory the ship befor you escape with a escape pod."
100         print "OK,now start your journey,Bravo."
101         raw_input("Enter any key to continue >")
102         print "You are running down the centeral corridor to the weapons armory"
103         print "a Gothon jumps out,red scary skin, the same heght as humanbeing"
104         print "He's blocking the door to the weapons armory and about to pull"
105         print "a weapon to blast you."
106         nothing = raw_input(">")
107         print "If you don't know how to do,enter 'help',and you will get some tips."#Q:如果想要实现任何时候输入help都能调用tips,该怎么做?
108         ask = raw_input(">")
109         if ask == "help":
110             print tips.Centcoridor_tip()
111         else:
112             pass
113         action = raw_input ("run or fight>")
114     
115         if action == "run":
116             print "You are scared,so just turn around and run desprately."
117             print "Oh the Gothon is gonna shoot you and 'BANG!', it missed the target."
118             print "How lucky are you ,so you can keep running,and......"
119             print "\n"
120             print "OMG, there is no more road tu run into,and the Gothon holds his weapon again."
121             print "Do you wanna ask for help,I'm sure you didn't forget how to do."
122         #个人认为这段有点啰嗦
123             ask = raw_input(">")
124             if ask == "help":
125                 print tips.Centcoridor_tip()
126             else:
127                 pass
128         #这里可以提示需不需要看下秘笈,然后调用秘笈模块
129             print "BANG!BANG,BANG,BANG!"
130             return "death"
131         elif action == "fight":
132             return 'fighting_system'
133             
134             
135 
136 class WeaponAromory(Scene):
137     def enter(self): 
138         print "You do a dive roll into the Weapon Aromory,crouch and scan the room."
139         print "for more Gothons that might be hiding. It's dead quiet, to quiet."
140         print "You stand up and run to the far side of the room and find the "
141         print "neutron bomb in its container. There is a keypad lock on the box"
142         print "and you need the code to get the bomb out.If you get the code"
143         print "Wrong for 10 times then the lock closes forever and you can't"
144         print "get the bomb.the code is 3 digit"
145         print "and you still got a  chance to ask for help"
146         help = raw_input("help or not>")
147         if help == "yes":
148             print tips.weaponaromory_tip()
149         else:
150             pass
151         code = "%d%d%d" %(9,4,randint(1,9))
152         guess = raw_input ("[keypad]>")
153         guesses = 0
154         
155         while guess != code and guesses < 10:
156             guesses += 1
157             print "BZZZZEDDDD"
158             guess = raw_input ("[keypad]")
159             
160         if guess == code:
161             print "The container is open and the seal breaks.Let the gas out."
162             print "You grab the neutron bomb and run as fast as you can to the"
163             print "bridge where you must place it in the right spot."
164             return 'the_bridge'
165         else:
166             print "The lock buzzes one last time and then you here a sickening."
167             print "melting sound as the mechanism is fuesd together."
168             print "You decide to sit there and finally the Gothon blow up the"
169             print "ship from their ship and you die."
170             return 'death'
171 
172 class TheBridge(Scene):
173     def enter(self):
174         print "You burst onto the bridge with the bomb"
175         print "and surprise 5 Gothons who are trying to "
176         print "take control of the ship.They see your bomb and "
177         print "don't want to set it off."
178         
179         action = raw_input(">")
180         if action  == "throw the bomb":
181             print "In a panic you throw the bomb at the group of Gothons"
182             print "and make a leap to the door."
183             print "But a Gothon shots you as you trun your back and"
184             print "try to run away.You died, but the good thing is"
185             print "the ship will be blasted by the bommb."
186             return 'death'
187         elif action == "slowly place the bomb":
188             print "You point your blaster at your bomb and the Gothons"
189             print "put their hands up and start to sewat."
190             print "You jump back through the door punch the close"
191             print "button.Now the bomb is placed and you run to the escape pod."
192             return 'escape_pod'
193         else:
194             "illeagal inpout."
195             return'the_bridge'
196             
197             
198 class EscapePod(Scene):
199     def enter(self):
200         print "Now as you've blow up the bridege,your mission in completed."
201         print "You should take a escape pod to leave here.Now there parks 3 escape pod,"
202         print "NO.1,NO.2 and NO.3. Two of them are broken and you need to "
203         print "choose the good one.Come on, make the choice."
204         right_pod = randint(1,3)
205         choice = int(raw_input("escape_pod>"))
206         if choice == right_pod:
207             print "Congratulations!You win!"
208         elif choice != right_pod:
209             print "Well ,you chose a bad escape pod and you just sank with it."
210             return 'death'
211             
212     
213 
214 class Map:
215     scenes = {
216         'central_corridor': CentralCorridor(),
217         'weapon_aromory': WeaponAromory(),
218         'the_bridge': TheBridge(),
219         'escape_pod': EscapePod(),
220         'death': Death(),
221         'fighting_system': FigthingSystem()
222     }
223     
224     def __init__(self,start_scene):
225         self.start_scene = start_scene
226     
227     def next_scene(self,scene_name):
228         return Map.scenes.get(scene_name)
229     
230     def opening_scene(self):
231         return self.next_scene(self.start_scene)
232         
233     
234 a_map = Map('central_corridor')
235 a_game = Engine(a_map)
236 a_game.play()
主体运行部分
 1 #-*- coding: UTF-8 -*-
 2 
 3 def Centcoridor_tip():
 4     tip = open('tip_for_centeralcorridor.txt')
 5     for line in  tip.readlines():
 6         print line
 7     #遗留问题:为什么最后会打印一个“none",如果想要去掉该怎么做?
 8 
 9     
10 
11 
12 def weaponaromory_tip():
13     tip = open('tip_for_weaponaromory.txt')
14     for line in tip.readlines():
15         print line
16         
用于调用帮助的tips模块

 

posted @ 2017-12-21 21:26  丁头零零  阅读(476)  评论(0编辑  收藏  举报