飞机大战Python程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

importpygamefromplane_spritesimport*classPlaneGame(object):#初始化开始游戏类方法启动游戏def__init__(self):#创建窗口时钟调用创建精灵方法self.screen=pygame.display.set_mode(SCREEN_RECT.size)self.clock=pygame.time.Clock()self.__create_spites()pygame.time.set_timer(CREATE_ENEMY_EVENT,1000)pygame.time.set_timer(HERO_FIRE_EVENT,500)#添加精灵def__create_spites(self):#背景精灵bg1=BackGroup()bg2=BackGroup(True)self.back_ground=pygame.sprite.Group(bg1,bg2)#敌机精灵self.enemy_group=pygame.sprite.Group()#英雄精灵self.hero=Hero()self.hero_group=pygame.sprite.Group(self.hero)#监听def__event_handler(self):#关闭×退出游戏foreventinpygame.event.get():ifevent.type==pygame.QUIT:PlaneGame.__Gameover()elifevent.type==CREATE_ENEMY_EVENT:#创建敌机denemy=Enemy()self.enemy_group.add(enemy)elifevent.type==HERO_FIRE_EVENT:self.hero.fire()#按键keys_pressed=pygame.key.get_pressed()ifkeys_pressed[pygame.K_RIGHT]:self.hero.speed=2elifkeys_pressed[pygame.K_LEFT]:self.hero.speed=-2else:self.hero.speed=0#碰撞检测def__check_collide(self):pygame.sprite.groupcollide(self.hero.bullets,self.enemy_group,True,True)enemys=pygame.sprite.spritecollide(self.hero,self.enemy_group,True)iflen(enemys)0:self.hero.kill()PlaneGame.__Gameover()#更新图象更新屏幕def__update_sprites(self):self.back_ground.update()self.back_ground.draw(self.screen)self.enemy_group.update()self.enemy_group.draw(self.screen)self.hero_group.update()self.hero_group.draw(self.screen)self.hero.bullets.update()self.hero.bullets.draw(self.screen)#游戏结束@staticmethoddef__Gameover():pygame.quit()exit()defstart_game(self):#时钟监听碰撞检测更新图象更新屏幕结束self.clock.tick(FRAME_PER_SEC)self.__event_handler()self.__check_collide()self.__update_sprites()pygame.display.update()if__name__==__main__:#创建对象game=PlaneGame()#启动游戏whileTrue:game.start_game()importrandomimportpygame#定义常量SCREEN_RECT=pygame.Rect(0,0,480,700)FRAME_PER_SEC=60CREATE_ENEMY_EVENT=pygame.USEREVENTHERO_FIRE_EVENT=pygame.USEREVENT+1classGameSprite(pygame.sprite.Sprite):def__init__(self,image_name,speed=1):super().__init__()self.image=pygame.image.load(image_name)self.rect=self.image.get_rect()self.speed=speeddefupdate(self):self.rect.y+=self.speedclassBackGroup(GameSprite):def__init__(self,is_alt=False):super().__init__(.\images\huibeijing.png)ifis_alt:self.rect.y=-self.rect.height#背景精灵defupdate(self):super().update()ifself.rect.y=SCREEN_RECT.height:self.rect.y=-self.rect.heightclassEnemy(GameSprite):#随机精灵#调用父类,图片def__init__(self):super().__init__(.\images\diji1.png)#随机速度self.speed=random.randint(1,3)#随机位置self.rect.bottom=0max_x=SCREEN_RECT.width-self.rect.widthself.rect.x=random.randint(0,max_x)defupdate(self):#调用父类super().update()#判断是否删除ifself.rect.y=SCREEN_RECT.height:self.kill()def__del__(self):#print(敌机挂了)passclassHero(GameSprite):#英雄图片def__init__(self):super().__init__(.\images\eiji1.png,0)self.rect.centerx=SCREEN_RECT.centerxself.rect.bottom=SCREEN_RECT.bottom-120self.bullets=pygame.sprite.Group()defupdate(self):self.rect.x+=self.speedifself.rect.x0:self.rect.x=0elifself.rect.rightSCREEN_RECT.right:self.rect.right=SCREEN_RECT.rightdeffire(self):foriin(0,1,2):bullet=Bullet()bullet.rect.bottom=self.rect.y-i*20bullet.rect.centerx=self.rect.centerxself.bullets.add(bullet)classBullet(GameSprite):def__init__(self):super().__init__(.\images\zidan1.png,-2)defupdate(self):super().update()ifself.rect.bottom0:self.kill()def__del__(self):pass

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功