pygame飞机大战用精灵组层编写英雄系列(七)英雄战队开组,左右僚机护法
题外话,下载了pygame1.9.5,发现自动提示功能没有了,函数的功能提示不全,显示 missingmodule,非常不方便,卸载,下载whl安装也不行。
重新安装了1.9.4,又可以了。
准备给英雄加个僚机,僚机自动发射子弹。
听起来复杂,其实实现起来不难,lifebar的原理加上子弹发射就OK了。
from setting import *
from herobullet import *
class WingPlane(pygame.sprite.Sprite):
"""
僚机,分为左右两个
"""
def __init__(self, boss, direction="LEFT"):
self.groups = allgroup
self.boss = boss
self._layer = self.boss._layer
pygame.sprite.Sprite.__init__(self, self.groups)
self.direction = direction
if direction == "LEFT":
self.image = pygame.image.load('images/hero/leftwingplane.png')
elif direction == 'RIGHT':
self.image = pygame.image.load('images/hero/rightwingplane.png')
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
self.fire_interval = 300
self.start_time = pygame.time.get_ticks()
def update(self):
if self.direction == "LEFT":
self.rect.centerx = self.boss.rect.centerx - self.boss.rect.width
elif self.direction == "RIGHT":
self.rect.centerx = self.boss.rect.centerx + self.boss.rect.width
self.rect.centery = self.boss.rect.centery
self.fire()
def fire(self, firetype=0):
current_time = pygame.time.get_ticks()
pass_time = current_time - self.start_time
if pass_time < self.fire_interval:
return
self.start_time = current_time
#hero 子弹的发射位置当然是头部中心
wingplane_fire_pos = vect(self.rect.midtop[0], self.rect.midtop[1])
#单颗子弹
if firetype == 0:
bullet = HeroBullet()
bullet.set_bullet_type(4)
pos_x = wingplane_fire_pos.x - bullet.rect.width // 2
pos_y = wingplane_fire_pos.y - 50
bullet.set_speed(-4, angle=90)
bullet.set_pos(pos_x, pos_y)
在heroplane里把左右护法加上。初始化函数里添加
wingplane_left = WingPlane(self,'LEFT')
wingplane_right =WingPlane(self,'RIGHT')
开心上路喽。