未加星标

python hard way中的一个小例子,简单的游戏引擎

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
python hard way中的一个小例子,简单的游戏引擎

3小时前来源:CSDN博客

这个是我在学习python时候在书上抄的小例子,上面只有注释是我自己写的。。。。

import random #import urllib import urllib2#同一资源定位符库import socket #套接字库,用来设置服务器from sys import exit classscene(object):#在子类的也将定义enter函数,因此子类中的enter函数将会继承覆盖那父类中的enter函数,此类中此程序中并无实际意义defenter(self):print"this scene is not yet configured. subclass it and implement enter"classengine(object):def__init__(self,scene_map):#scene_map是Map类的对象,将一个对象用作其他类的参数 self.scene_map=scene_map defplay(self):#相当于游戏的开始按键 current_scene=self.scene_map.opening_scene #self.scene_map.opening_scene最后会返回一个类的对象,即返回一个场景类的对象whileTrue: print"\n" next_sence_name=current_scene.enter 通过场景类对象来调用场景类中定义的enter函数 #enter函数最后会返回一个新的场景(这次返回的不是场景对象,而是场景字符串)#current_scene对象会调用场景类中的enter函数,此时的next_scene_name是一个字符串类型 current_scene=self.scene_map.next_sence(next_scene_name)#current_scene变成下一个场景的对象了,然后循环classDeath(scene):#子类场景中定义的enter函数最后会返回一个新场景的字符串,那这个字符串将作为Map类的新参数,进行下一步操作 quips=["you died. you kinda suck at this", "your mom would be proud...if she were smarter", "such a loser", "i have a small puppy that's better at this"] defenter(self):print Death.quips[randint(0,len(self.quips)-1)] exit(1) classCentral_corridor(scene):#中央控制走廊defenter(self):print"the Gothons of planet percal #25 have invaded your ship and destroyed"print"your entire crew.you are the last surviving member and your last"print"mission is to get the neutrn destruct bomb from the weapons armory"print"put it in the bridge,and blow the ship up after getting into an"print"escape pod"print"\n"print"you are running down the central corridor to the weapons armory when"print"a Gothons jumps out ,red scaly skin,dark grimy teeth,and evil clown costume"print"flowing around his hate filled body,he's blocking the door to the"print"armory and about to pull a weapon to blast you" action=raw_input("") if action=="shoot": print"quick on the draw you yank out your blaster and fire it at the Gothons"#哈哈,铁血战士的剧情,不过铁血战士可是不会吃人肉的print"his clown costume is flowing and moving around his body,which throws"print"off you aim. your laser hits his costume but misser him entirely . this"print"completely ruins his brand new costume his mother bought him, which"print"makes him fly into a rage and blast you repeatedly in the face until"print"you are died. then he eats you"return"death"elif action=="dodge!": print"like a world class boxer you dodge,weave,slip and slider right"print"as the Gothons's blaster cranks a loser past your head"print"in the middle of you areful dodge your foot slips and you "print"bang you head on the metal wall and pass out"print"your wake up shortly after only to die as the Gothons stomps on"print"you head and eats you "return"death"elif action=="tell a joke": print"lucky for you they made you learn Gothons insults in the academy"print"you tell the one Gothons joke you kown"print"从前有一只猪,它很笨所以它死了"print"what cold ,the Gothon man will decide to killl you and eat you head"print"but write minutes,the Gothon man fell wrong,and let you go"return"laser_weapon_armory"else: return"central_corridor"classLaser_weapon_armory(scene):#激光武器库defenter(self):print"you do a live"print"虽然你活下来了,但是你很快会死亡,因为我是gothons"print""classThe_bridge(scene):#飞船主控舱defenter(self):print"恭喜你,你将进入飞船的主控仓,还有一步,你将逃过gothons人的追杀,祝你好运"classEscape_pod(scene):#救生舱defenterself):print"恭喜你,你已经逃出生天了"classMap(object): scenes={"central_corridor":Centralcorridor, #通过字典来调用类,从而实际上返回的是一个对象,!!!!!很巧妙:Laserweaponarmory, "the_bridge":TheBridge, #字典也可用来调用函数,但是必须字典的键和值是可哈希的!!!!"escape_pod":EscapePod, "death":Death} def__init__(self,start_scene):#确定开始场景,即你开始所在飞船的位置 self.start_scene=start_scene defnext_sence(self,scene_name):return Map.scenes.get(scene_name) #返回字典的键,实质上是返回一个类的对象!!!!!defopening_scene(self):return self.next_scene(self.start_scene) #此函数并无实质意义,实质上只是调用next-sence函数 a_map=Map('central_corridor') #创建一个map类的对象 a_game=Engine(a_map) #将map类的对象作为参数传递给engine类 a_game.play #python的灵活性将体现在这里,可以将对象用作参数,并且利用通过字典来调用类,从而返回对象,不过python中一切皆是对象~

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: SDN服务器博客定位
分页:12
转载请注明
本文标题:python hard way中的一个小例子,简单的游戏引擎
本站链接:http://www.codesec.net/view/482048.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(27)