上一篇讲解玩家飞机控制方法,以及子弹越界判断。本篇继续讲解敌机、补给包、Boss的生成及控制。
和玩家飞机一样,继承BasePlane
创建几个敌机相关类。
普通敌机EnemyPlane
:move
控制移动
敌机BOSSBossPlane
:血量大,不移动
携带补给包的敌机 RewardPlane
:类似 BOSS,多了一个self.reward
属性,保存补给类型,飞机被摧毁可以通过这个属性,来刷新相应的补给包。
class EnemyPlane(BasePlane):
def __init__(self, pygame_screen, Bullets):
BasePlane.__init__(self, pygame_screen, (randint(10, 460), 3), Image_EnemyPlane_1, 2, Bullets) # 随机X坐标刷新敌机
if self.x >= 240: # 根据刷新位置,生成初始移动方向
self.movedirection = 'Left'
else:
self.movedirection = 'Right'
self.movespeedH = 1 # 横向移动速度
self.movespeedV = 1 # 纵向移动速度
self.health = 6
self.maxHealth = 6
self.score = 100 # 被击落得分
self.__crate_images()
def __crate_images(self):
for i in range(4):
self.bombList.append(pygame.image.load("./res/enemy0_down" + str(i + 1) + ".png"))
def move(self):
self.y += self.movespeedH
if self.movedirection == 'Left':
self.x -= self.movespeedV
if self.x < 20:
self.movedirection = 'Right'
else:
self.x += self.movespeedV
if self.x > 460:
self.movedirection = 'Left'
def fire(self):
if self.bulletCdOk:
random_num = randint(1, 200)
if random_num == 8 or random_num == 20:
self.bulletList.append(EnemyBullet(self.screen, (self.x + (self.w / 2) - 4, self.y + (self.h))))
self.bulletCdOk = False
self.bulletCoolDownstate = 1
def display(self, ):
BasePlane.display(self)
# 血条显示
for i in range(self.health):
self.screen.blit(self.healthImage, (self.x + i * 9, self.y - 13))
class RewardPlane(BasePlane):
def __init__(self, pygame_screen, Bullets, Rewarditem):
BasePlane.__init__(self, pygame_screen, (60, 240), Image_RewardPlane1, 2, Bullets)
self.health = 50
self.maxHealth = 50
self.score = 5000
self.reward = Rewarditem
self.__crate_images()
def __crate_images(self):
for i in range(4):
self.bombList.append(pygame.image.load("./res/enemy1_down" + str(i + 1) + ".png"))
def move(self):
pass
def fire(self):
if self.bulletCdOk:
random_num = randint(1, 200)
if random_num == 8 or random_num == 20:
self.bulletList.append(EnemyBullet(self.screen, (self.x + (self.w / 2) - 4, self.y + (self.h))))
self.bulletCdOk = False
self.bulletCoolDownstate = 1
def display(self):
BasePlane.display(self)
onstep = self.maxHealth // 8 # 血量换算成格数
healthpercent = self.health // onstep
# 血条显示
for i in range(healthpercent):
self.screen.blit(self.healthImage, (self.x + self.w + 5, self.y + self.h - 24 - i * 9))
class BossPlane(BasePlane):
def __init__(self, pygame_screen, Bullets):
BasePlane.__init__(self, pygame_screen, (160, 3), Image_BossPlane1, 2, Bullets)
self.health = 200
self.maxHealth = 200
self.score = 10000
self.__crate_images()
# PlaySound(Sound_EnemyPlane_1) # 刷新是播放音效
def __crate_images(self):
for i in range(4):
self.bombList.append(pygame.image.load("./res/enemy2_down" + str(i + 1) + ".png"))
def move(self):
pass
def fire(self):
if self.bulletCdOk:
random_num = randint(1, 200)
if random_num == 8 or random_num == 20:
self.bulletList.append(EnemyBullet(self.screen, (self.x + (self.w / 2) - 4, self.y + (self.h))))
self.bulletCdOk = False
self.bulletCoolDownstate = 1
def display(self):
BasePlane.display(self)
onstep = self.maxHealth // 22 # 血量换算成格数
healthpercent = self.health // onstep
if healthpercent < 10:
self.image = pygame.image.load(Image_BossPlane1Half)
# 血条显示
for i in range(healthpercent):
self.screen.blit(self.healthImage, (self.x + self.w + 5, self.y + self.h - 44 - i * 9))
更新Main()
函数,使用 enemybulltes = []
和 enemyplanes = []
保存敌机子弹,和敌机对象。列表形式保存,方便之后的越界判断、碰撞判断。
def Main():
pygame.init()
screen = pygame.display.set_mode((480, 852), 0, 32)
# 添加背景
Bg = BackGround(screen, Image_Background, 'dynamic')
Bullets = []
enemybulltes = []
enemyplanes = []
enemyplanes.append(EnemyPlane(screen,enemybulltes))
enemyplanes.append(BossPlane(screen, enemybulltes))
enemyplanes.append(RewardPlane(screen, enemybulltes,'shotbullet'))
hero = HeroPlane(screen, Bullets)
while True:
Bg.display()
hero.move() #生成飞机移动后位置
hero.bulletCoolDown() # 玩家飞机子弹冷却
hero.fire() # 玩家飞机发射子弹
hero.display() #绘制玩家飞机
for enemy in enemyplanes:
enemy.bulletCoolDown()
enemy.move()
enemy.fire()
enemy.display()
# IsOverBound(Bullets, EnemyBullets, enemyplanes, Rewards)
IsOverBound(Bullets)
pygame.display.update()
MainControl(hero, screen)
Main
调用越界判断函数 IsOverBound
,补上传参: enemyBulltes,enemyPlanes
IsOverBound(bullets,enemyBulltes,enemyPlanes)