pygame系列_箭刺Elephant游戏_源码下载
2013-10-10 20:47
447 查看
这个游戏原名为:Chimp,我们可以到:
http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html
获取到源码和详细的源码讲解
下面是我对游戏的改编:
运行效果:
当箭刺到大象的时候,大象的身体就会翻转,并且发出声音,当然没有刺到的时候,也会发出另外的声音。
在游戏中,有很多地方值得我们参考,如加载图片,声音和异常处理等
=========================================
代码部分:
=========================================
源码下载:http://files.cnblogs.com/hongten/spear_elephant.zip
http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html
获取到源码和详细的源码讲解
下面是我对游戏的改编:
运行效果:
当箭刺到大象的时候,大象的身体就会翻转,并且发出声音,当然没有刺到的时候,也会发出另外的声音。
在游戏中,有很多地方值得我们参考,如加载图片,声音和异常处理等
=========================================
代码部分:
=========================================
#python elephant #Import Modules import os, pygame from pygame.locals import * __author__ = {'name' : 'Hongten', 'mail' : 'hongtenzone@foxmail.com', 'blog' : 'http://www.cnblogs.com/hongten', 'QQ' : '648719819', 'Version' : '1.0'} if not pygame.font: print('Warning, fonts disabled') if not pygame.mixer: print('Warning, sound disabled') #functions to create our resources def load_image(name, colorkey=None): fullname = os.path.join('data', name) try: image = pygame.image.load(fullname) except pygame.error as message: print('Cannot load image:', fullname) raise (SystemExit, message) image = image.convert() if colorkey is not None: if colorkey is -1: colorkey = image.get_at((0,0)) image.set_colorkey(colorkey, RLEACCEL) return image, image.get_rect() def load_sound(name): class NoneSound: def play(self): pass if not pygame.mixer or not pygame.mixer.get_init(): return NoneSound() fullname = os.path.join('data', name) try: sound = pygame.mixer.Sound(fullname) except pygame.error as message: print('Cannot load sound:', fullname) raise (SystemExit, message) return sound #classes for our game objects class Spear(pygame.sprite.Sprite): """moves a clenched spear on the screen, following the mouse""" def __init__(self): pygame.sprite.Sprite.__init__(self) #call Sprite initializer self.image, self.rect = load_image('spear-w.png', -1) self.punching = 0 def update(self): "move the spear based on the mouse position" pos = pygame.mouse.get_pos() self.rect.midtop = pos if self.punching: self.rect.move_ip(5, 10) def punch(self, target): "returns true if the spear collides with the target" if not self.punching: self.punching = 1 hitbox = self.rect.inflate(-5, -5) return hitbox.colliderect(target.rect) def unpunch(self): "called to pull the spear back" self.punching = 0 class Elephant(pygame.sprite.Sprite): """moves a elephant critter across the screen. it can spin the monkey when it is punched.""" def __init__(self): pygame.sprite.Sprite.__init__(self) #call Sprite intializer self.image, self.rect = load_image('elephant-nw.png', -1) screen = pygame.display.get_surface() self.area = screen.get_rect() self.rect.topleft = 10, 10 self.move = 9 self.dizzy = 0 def update(self): "walk or spin, depending on the monkeys state" if self.dizzy: self._spin() else: self._walk() def _walk(self): "move the monkey across the screen, and turn at the ends" newpos = self.rect.move((self.move, 0)) if self.rect.left < self.area.left or \ self.rect.right > self.area.right: self.move = -self.move newpos = self.rect.move((self.move, 0)) self.image = pygame.transform.flip(self.image, 1, 0) self.rect = newpos def _spin(self): "spin the monkey image" center = self.rect.center self.dizzy = self.dizzy + 12 if self.dizzy >= 360: self.dizzy = 0 self.image = self.original else: rotate = pygame.transform.rotate self.image = rotate(self.original, self.dizzy) self.rect = self.image.get_rect(center=center) def punched(self): "this will cause the monkey to start spinning" if not self.dizzy: self.dizzy = 1 self.original = self.image def main(): """this function is called when the program starts. it initializes everything it needs, then runs in a loop until the function returns.""" #Initialize Everything pygame.init() screen = pygame.display.set_mode((468, 60)) pygame.display.set_caption('Monkey Fever') pygame.mouse.set_visible(0) #Create The Backgound background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((250, 250, 250)) #Put Text On The Background, Centered if pygame.font: font = pygame.font.Font(None, 36) text = font.render("Pummel The Elephant, And Win $$$", 1, (10, 10, 10)) textpos = text.get_rect(centerx=background.get_width()/2) background.blit(text, textpos) #Display The Background screen.blit(background, (0, 0)) pygame.display.flip() #Prepare Game Objects clock = pygame.time.Clock() whiff_sound = load_sound('elephant-jump.wav') punch_sound = load_sound('elephant-mmove.wav') elephant = Elephant() spear = Spear() allsprites = pygame.sprite.RenderPlain((spear, elephant)) #Main Loop while 1: clock.tick(60) #Handle Input Events for event in pygame.event.get(): if event.type == QUIT: return elif event.type == KEYDOWN and event.key == K_ESCAPE: return elif event.type == MOUSEBUTTONDOWN: if spear.punch(elephant): punch_sound.play() #punch elephant.punched() else: whiff_sound.play() #miss elif event.type is MOUSEBUTTONUP: spear.unpunch() allsprites.update() #Draw Everything screen.blit(background, (0, 0)) allsprites.draw(screen) pygame.display.flip() #Game Over #this calls the 'main' function when this script is executed if __name__ == '__main__': main()
源码下载:http://files.cnblogs.com/hongten/spear_elephant.zip
相关文章推荐
- pygame系列_小球完全弹性碰撞游戏_源码下载
- pygame系列_font游戏字体_源码下载
- pygame系列_箭刺Elephant游戏
- [置顶]▁▂▃ 【2011-9-22日前Himi所有博文锦集-CHM文档】+【Android游戏开发系列源码】+【免费提供打包下载】▃▂▁
- [置顶]▁▂▃ 【2011-9-22日前Himi所有博文锦集-CHM文档】+【Android游戏开发系列源码】+【免费提供打包下载】▃▂▁
- cocos2dx 3.10 网狐土豪金版PC源码搭建+手机端大厅游戏源码下载
- QBlog V2.5 源码开放下载(ASP.NET 番外系列之开端)
- Cocos2dx系列笔记7:一个简单的跑酷游戏《萝莉快跑》的消化(附下载)
- Silverlight实用窍门系列:3.Silverlight鼠标动态绘制矩形【实例源码下载】
- 下载游戏源码如何在eclipse下运行
- 假日1737棋牌源码加几款精品游戏源码,附带数据库完整下载
- 奇葩版Flappy Bird,HTML5 Flappy游戏源码下载
- android源码-安卓源码-Android源码下载-安卓游戏源码
- 殖民帝国游戏源码下载 FreeCol(Java)
- S5P4418裸机开发系列教程--源码下载
- Cocos2d-x简单游戏<植物大战僵尸>代码实现|第三部分:通关场景<后续会提供源码下载链接>
- android源码大全 IOS游戏源代码打包下载 小游戏|视频教程 微信小程序源码带后台全套|公众号平台
- Python基于pygame实现的font游戏字体(附源码)
- unigine源代码源码下载 unigine2013游戏引擎源码