飞机大战Python程序Word格式.docx
《飞机大战Python程序Word格式.docx》由会员分享,可在线阅读,更多相关《飞机大战Python程序Word格式.docx(6页珍藏版)》请在冰豆网上搜索。
bg1=BackGroup()
bg2=BackGroup(True)
self.back_ground=pygame.sprite.Group(bg1,bg2)
#敌机精灵
pww#
:
丄Nm/\m一人1/\1日1\1日一日丄\/曰占0==edAriueAej!
|Q
()jeAoeiuB0—eiueoeueid
:
linoeiue6Ad==edAviueAeji
0;
e64ueAeeiue6Adui;
ueAe」oj驱撤用irx圉沃#
(j|es)je|pueq_;
ueAe—jep
场册#
(ojeqj|es)dno」9q!
」ds〜iue6Ad=dnoj6_ojeq\nes
()ojeH=ojeqjies
爸蘇髯篦#
()dnoj0e;
!
Jdseiue6Ad=dnoj6_ALueue\nes
enemy=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=2
elifkeys_pressed[pygame.K_LEFT]:
self.hero.speed=-2
else:
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)
#游戏结束
@staticmethod
def__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()
importrandom
#定义常量
SCREEN_RECT=pygame.Rect(O,0,480,700)
FRAME_PER_SEC=60
CREATE_ENEMY_EVENT=pygame.USEREVENT
HERO_FIRE_EVENT=pygame.USEREVENT+1
classGameSprite(pygame.sprite.Sprite):
def__init__(self,image_name,speed=1):
super().__init__()
self.image=pygame.image.Ioad(image_name)
self.rect=self.image.get_rect()
self.speed=speed
defupdate(self):
self.rect.y+=self.speed
classBackGroup(GameSprite):
def__init__(self,is_alt=False):
super().__init__("
.\images\huibeijing.png"
)
ifis_alt:
self.rect.y=-self.rect.height
冃景精火
super().update()
ifself.rect.y>
=SCREEN_RECT.height:
classEnemy(GameSprite):
#随机精灵
#调用父类,图片
.\images\diji1.png"
#随机速度
self.speed=random.randint(1,3)
#随机位置
self.rect.bottom=0
max_x=SCREEN_RECT.width-self.rect.widthself.rect.x=random.randint(0,max_x)
#调用父类
#判断是否删除
self.kill()
def__del__(self):
#print("
敌机挂了"
)
pass
classHero(GameSprite):
#英雄图片
super().__init__(
"
.\images\eiji1.png"
0)
self.rect.eenterx=SCREEN_RECT.centerx
self.rect.bottom=SCREEN_RECT.bottom-120
self.bullets=pygame.sprite.Group()
self.rect.x+=self.speed
ifself.rect.x<
self.rect.x=0
elifself.rect.right>
SCREEN_RECT.right:
self.rect.right=SCREEN_RECT.right
deffire(self):
foriin(0,1,2):
bullet=Bullet()
bullet.rect.bottom=self.rect.y-i*20
bullet.rect.centerx=self.rect.centerxself.bullets.add(bullet)
classBullet(GameSprite):
-2)
.\images\zidanl.png"
ifself.rect.bottom<
self.kill()