上一篇将主要的函数封装到了一个对象中,还把列表变量,替换成类属性,本篇着重讲解类的完善过程
一、showText
类:A,新增字体大小 B,调整显示文本更新方法,更改为:display
方法中,更新显示文本,取消创建对象是传入显示文本。
二、EnemyPlane
类:`
添加:ismove(True/False) postion 两个参数,这样创建普通敌机可以选择移动、不移动,固定刷新位置,随机刷新。
删除了血量显示,之前显示它只是为了更方便观察敌机状态。
三、
EnemyBullet
类改动比较大:完整源码如下 showText EnemyPlane EnemyBullet
class EnemyBullet(BaseBullet): #敌机子弹
group = []
def __init__(self, pygame_screen, postion,movingPath,delayTime,deviation,bullet_image):
self.movingPath = movingPath #花式子弹名称
self.delayTime = delayTime #第一发子弹停留延时,给玩家一个提示,这里要发射子弹了
self.setpX,self.setpY = deviation #偏移量,做散弹
Base.__init__(self, pygame_screen,postion, bullet_image)
EnemyBullet.group.append(self)
def move(self):
if self.movingPath == "normal":
self.y += 3
elif self.movingPath == "laser":
if self.delayTime == 0:
self.y += 10
else:
self.delayTime -= 1
elif self.movingPath == "Rbiasline":
self.y += self.setpY
self.x += self.setpX * 3
elif self.movingPath == "Lbiasline":
self.y += self.setpY
self.x -= self.setpX
elif self.movingPath == "Rline":
if self.delayTime == 0:
self.y += 10
else:
self.x += 2
self.delayTime -= 1
elif self.movingPath == "Lline":
if self.delayTime == 0:
self.y += 10
else:
self.x -= 2
self.delayTime -= 1
EnemyBullet
子弹发射补充相应的参数
EnemyBullet(self.screen, (self.x + (self.w / 2) - 4 , self.y + (self.h)),
"normal",0,(0,0),Image_EnemyBullet)
四、HeroPlane
类 showText EnemyPlane EnemyBullet HeroPlane
class HeroPlane(BasePlane):
group = []
def __init__(self, pygame_screen):
BasePlane.__init__(self, pygame_screen, (210, 700) ,Image_HeroPlane, 5)
self.moveToLeft = False # 控制玩家飞机移动位置
self.moveToRight = False
self.moveToUp = False
self.moveToDown = False
self.keepFire = False
self.moveSpeed = 3
self.health = 6
self.maxHealth = 6
self.live = 10
self.invincible = True
self.invincibleTime = 400 #无敌时间
self.invincibleTiming = 0 #无敌倒计时
# 新增属性
self.bulletLevel = 1 # 子弹使用级别来区分
self.bombs = 0 # 炸弹数
self.reduction = 0 # 缩小状态
self.miniplane = 0 # 变形成mini飞机
# 血量、无敌状态、炸弹数移动到左下角集中显示
self.showHealthPic = pygame.transform.scale(self.image, (25, 30))
self.showbombs = pygame.image.load(Image_bombs)
self.showbombs = pygame.transform.scale(self.showbombs, (31, 27))
self.showinvincibleTime = ShowText(self.screen, Font_Consola, (80, 80, 80), (50 , Screen_Height - 172),18)
self.showbombCount = ShowText(self.screen, Font_Consola, (149, 85, 85), (58, Screen_Height - 126),18)
self.x = (Screen_Width - self.w) / 2 # 调整boss刷新位置
self.__crate_images() # 添加爆炸动画图片组
HeroPlane.group.append(self)
def __crate_images(self):
for i in range(4):
self.bombList.append(pygame.image.load("./res/hero_blowup_n" + str(i + 1) + ".png"))
def move(self):
#通过新增属性,选择不同的飞机图片进行显示
self.image = pygame.image.load(Image_HeroPlane)
# 飞机普通状态显示的图片
if self.moveToRight and self.x < 480 - self.w: # 移动越界判断
self.x += self.moveSpeed
if self.moveToLeft and self.x > self.moveSpeed:
self.x -= self.moveSpeed
if self.moveToDown and self.y < 852 - self.h:
self.y += self.moveSpeed
if self.moveToUp and self.y > self.moveSpeed:
self.image = pygame.image.load(Image_HeroPlaneUp) # 飞机往前飞状态显示的图片
self.y -= self.moveSpeed
if self.reduction == 1 :
self.image = pygame.transform.scale(self.image, (50, 62))
self.w, self.h = self.image.get_size()
if self.miniplane == 1 :
self.image = self.image = pygame.image.load(Image_MiniPlane)
self.w, self.h = self.image.get_size()
def useBomb(self):
#使用炸弹
if self.bombs > 0:
self.bombs -= 1
enemyObjs = [EnemyPlane.group, BossPlane.group, RewardPlane.group]
for enemyObj in enemyObjs:
for index in range(len(enemyObj) - 1, -1, -1):
if isinstance(enemyObj[index], BossPlane):
enemyObj[index].hit = True
else:
enemyObj[index].live = 0
enemyObj[index].hit = True
EnemyBullet.group = []
def fire(self):
#玩家飞机开火
#根据不同子弹等级,发射不同的子弹样式
if self.keepFire and self.bulletCdOk:
PlaySound(Sound_HeroFire)
if self.bulletLevel == 1 or self.bulletLevel == 3 or self.bulletLevel == 4:
Bullet(self.screen, (self.x + (self.w / 2) - 2 , self.y - 2), "line")
if self.bulletLevel == 2:
Bullet(self.screen, (self.x + (self.w / 2) - 34, self.y + 20), "line")
Bullet(self.screen, (self.x + (self.w / 2) + 28, self.y + 20), "line")
if self.bulletLevel == 3 or self.bulletLevel == 4:
Bullet(self.screen, (self.x + (self.w / 2) - 34, self.y + 20), "Lbiasline")
Bullet(self.screen, (self.x + (self.w / 2) + 28, self.y + 20), "Rbiasline")
if self.bulletLevel == 4:
Bullet(self.screen, (self.x , self.y + self.h / 2), "Lline")
Bullet(self.screen, (self.x + (self.w * 6 / 8) , self.y + self.h / 2), "Rline")
self.bulletCoolDownstate = 0
self.bulletCdOk = False
def display(self):
#左下角玩家信息显示
if self.invincible:
self.invincibleTiming += 1
if self.invincibleTiming == self.invincibleTime:
# 无敌时间到
self.invincibleTiming = 0
self.invincible = False
else:
#无敌倒计时显示
self.showinvincibleTime.display(str(int((self.invincibleTime - self.invincibleTiming) // (self.invincibleTime / 10))))
BasePlane.display(self)
#血条显示
self.screen.blit(self.showHealthPic, (10 , Screen_Height - 184))
if self.bombs > 0:
self.screen.blit(self.showbombs,(7,Screen_Height - 140 ))
self.showbombCount.display('X'+str(self.bombs))
for i in range(self.health):
self.screen.blit(self.healthImage, (10 + i * 9, Screen_Height - 198))
玩家飞机状态信息显示方式修改:
玩家飞机正常状态,缩小状态,变形状态
本篇讲解了 showText EnemyPlane EnemyBullet HeroPlane
四个类的修改,下一篇会继续讲解其他类的修改。