话不多说,直接上代码分析……
因为我们之前在创建己方飞机的时候,没有对音效和重生的属性和方法进行定义,所以我们先把这一部分补充完成:
class myPlane(pygame.sprite.Sprite):
def __init__(self,screen,screen_size):
pygame.sprite.Sprite.__init__(self)
self.image1 = pygame.image.load('./images/me1.png').convert_alpha()
self.image2 = pygame.image.load('./images/me2.png').convert_alpha()
self.mask = pygame.mask.from_surface(self.image1)
self.rect = self.image1.get_rect()
self.width,self.height = screen_size[0],screen_size[1]
self.rect.left,self.rect.bottom = (self.width-self.rect.width)//2, self.height-10
self.screen = screen
self.speed = 10
self.switch_image = True
self.delay = 100
self.destroy_images = []
self.destroy_images.extend([
pygame.image.load('./images/me_destroy_1.png').convert_alpha(),
pygame.image.load('./images/me_destroy_2.png').convert_alpha(),
pygame.image.load('./images/me_destroy_3.png').convert_alpha(),
pygame.image.load('./images/me_destroy_4.png').convert_alpha()])
self.active = True
self.down_sound = pygame.mixer.Sound("sound/me_down.wav") # 添加音效
self.down_sound.set_volume(0.2)
def moveUp(self):
if self.rect.top > 0:
self.rect.top -= self.speed
else:
self.rect.top = 0
def moveDown(self):
if self.rect.bottom < self.height-10:
self.rect.bottom += self.speed
else:
self.rect.bottom = self.height-10
def moveLeft(self):
if self.rect.left > 0:
self.rect.left -= self.speed
else:
self.rect.left = 0
def moveRight(self):
if self.rect.right < self.width:
self.rect.right += self.speed
else:
self.rect.right = self.width
def animation(self):
if self.switch_image:
self.screen.blit(self.image1, self.rect)
else:
self.screen.blit(self.image2, self.rect)
if self.delay % 5 == 0:
self.switch_image = not self.switch_image
self.delay -= 1
if self.delay == 0:
self.delay = 100
def time_delay(self): # 作为整个游戏动态切换的时钟
self.delay -= 1
if self.delay == 0:
self.delay = 100
def reset(self): # 添加重生的方法
self.active = True
self.rect.left, self.rect.bottom = (self.width - self.rect.left) // 2, self.height - 10
def play_sound(self): # 播放坠毁音效
self.down_sound.play()
添加音效和重生方法,这边就不做赘述了,之前敌机讲的非常清楚了,下面我们来看下,怎么实现击中敌机的判断,敌机坠毁了又应该做些什么:
for each in bullet1:
if each.active == True: # 如果子弹没有飞出界面及击中飞机
each.move()
screen.blit(each.image, each.rect) # 绘制子弹
enemies_hit = pygame.sprite.spritecollide(each, enemies, False, pygame.sprite.collide_mask) # 检测子弹与敌机是否发生重叠(即为击中)
if enemies_hit: # 如果发生击中了,enemies就是True
each.active = False # 更改子弹的状态
for e in enemies_hit:
if e in mid_enemies or e in big_enemies: # 如果击中的是中型或者大型飞机,那么就扣血
e.energy -= 1
if e.energy == 0: # 如果血扣完了,那就拜拜了
e.active = False
else: # 如果是小型飞机,那就直接拜拜了
e.active = False
else: # 子弹重生
each.reset(heroPlane.rect.midtop)
到这里,就可以和上一篇的连起来了,如果发生子弹和敌机重叠的情况,即视为击中,那么就扣血,血扣完了就更改敌机的active,敌机每次首先会判断敌机的状态,如果active是True那么就正常的飞行,反之就触发击毁效果,并且重生飞机。
下面我们来看一下,怎么判断己方飞机有没有被敌机击中:
enemies_collided = pygame.sprite.spritecollide(heroPlane, enemies, False, pygame.sprite.collide_mask) # 一样的,判断己方飞机是否和敌机组的飞机产生重合
if enemies_collided: # 如果产生重合那么就被击毁了
heroPlane.active = False # 更改己方飞机的状态
for each in enemies_collided:
each.active = False # 碰撞后,将敌机状态设置为False
好了,我们接下来就要实现己方飞机击毁后血量的显示和重生了:
# 我们需要在while True前加上下面的代码
hero_destroy_index = 0 # 击毁后己方飞机动画的序号
life_image = pygame.image.load("images/life.png").convert_alpha() # 加载血量的图片
life_rect = life_image.get_rect() # 获取血量的rect
life_NUM = 3 # 设置血量为3
# 在while True里面,我们要对己方飞机被击毁后应该做什么事情进行处理
heroPlane.time_delay() # 运行动态效果时钟
if heroPlane.active == True: # 判断己方飞机的状态
heroPlane.animation() # 如果没有被击毁就正常移动
else:
if not (heroPlane.delay % 3): # 做一定的延迟
screen.blit(each.destroy_images[hero_destroy_index], each.rect) # 绘制绘制坠毁的图片
hero_destroy_index = (hero_destroy_index + 1) % 4 # 更改图片的序号
if hero_destroy_index == 0: # 如果是最后一张图片
life_NUM -= 1 # 血量减一
heroPlane.play_sound() # 播放坠毁音效
heroPlane.reset() # 重生飞机
if life_NUM > 0: # 判断血量是否大于0
# 绘制血量图片
for i in range(life_NUM):
screen.blit(life_image, (width - 10 - (i + 1) * life_rect.width, height - 10 - life_rect.height))
这样我们所需要的击毁效果和生命值的显示就完成了,看下整体代码和效果吧!
import pygame
import sys
from pygame.locals import *
import myclass
pygame.init()
screen_size = width, height = 480, 700
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('飞机大战')
bg = pygame.image.load('./images/background.png').convert()
pygame.mixer.music.load('./sound/game_music.ogg')
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play(-1)
def add_big_enemies(group1, group2, num):
for big_enemy_num in range(num):
each_big_enemy = myclass.BigEnemy(screen_size)
group1.add(each_big_enemy)
group2.add(each_big_enemy)
def add_mid_enemies(group1, group2, num):
for mid_enemy_num in range(num):
each_mid_enemy = myclass.MidEnemy(screen_size)
group1.add(each_mid_enemy)
group2.add(each_mid_enemy)
def add_small_enemies(group1, group2, num):
for small_enemy_num in range(num):
each_small_enemy = myclass.SmallEnemy(screen_size)
group1.add(each_small_enemy)
group2.add(each_small_enemy)
def main():
clock = pygame.time.Clock()
heroPlane = myclass.myPlane(screen, screen_size)
enemies = pygame.sprite.Group()
big_enemies = pygame.sprite.Group()
add_big_enemies(big_enemies, enemies, 2)
mid_enemies = pygame.sprite.Group()
add_mid_enemies(mid_enemies, enemies, 4)
small_enemies = pygame.sprite.Group()
add_small_enemies(small_enemies, enemies, 15)
bullet1 = []
bullet1_index = 0
BULLET1_NUM = 5
for i in range(BULLET1_NUM):
bullet1.append(myclass.Bullet(heroPlane.rect.midtop, True))
small_destroy_index = 0
mid_destroy_index = 0
big_destroy_index = 0
hero_destroy_index = 0
life_image = pygame.image.load("images/life.png").convert_alpha()
life_rect = life_image.get_rect()
life_NUM = 3
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
key_pressed = pygame.key.get_pressed()
if key_pressed[K_UP]:
heroPlane.moveUp()
if key_pressed[K_DOWN]:
heroPlane.moveDown()
if key_pressed[K_LEFT]:
heroPlane.moveLeft()
if key_pressed[K_RIGHT]:
heroPlane.moveRight()
screen.blit(bg, (0, 0))
heroPlane.time_delay()
for each in big_enemies:
if each.active == True:
each.move()
screen.blit(each.image, each.rect)
else:
if not (heroPlane.delay % 3):
screen.blit(each.destroy_images[big_destroy_index], each.rect)
big_destroy_index = (big_destroy_index + 1) % 6
if big_destroy_index == 0:
each.play_sound()
each.reset()
for each in mid_enemies:
if each.active == True:
each.move()
screen.blit(each.image, each.rect)
else:
if not (heroPlane.delay % 3):
screen.blit(each.destroy_images[mid_destroy_index], each.rect)
mid_destroy_index = (mid_destroy_index + 1) % 4
if mid_destroy_index == 0:
each.play_sound()
each.reset()
for each in small_enemies:
if each.active == True:
each.move()
screen.blit(each.image, each.rect)
else:
if not (heroPlane.delay % 3):
screen.blit(each.destroy_images[small_destroy_index], each.rect)
small_destroy_index = (small_destroy_index + 1) % 4
if small_destroy_index == 0:
each.play_sound()
each.reset()
if heroPlane.active == True:
heroPlane.animation()
else:
if not (heroPlane.delay % 3):
screen.blit(heroPlane.destroy_images[hero_destroy_index], heroPlane.rect)
hero_destroy_index = (hero_destroy_index + 1) % 4
if hero_destroy_index == 0:
life_NUM -= 1
heroPlane.play_sound()
heroPlane.reset()
if life_NUM > 0:
for i in range(life_NUM):
screen.blit(life_image, (width - 10 - (i + 1) * life_rect.width, height - 10 - life_rect.height))
if not (heroPlane.delay % 10):
bullet1[bullet1_index].reset(heroPlane.rect.midtop)
bullet1_index = (bullet1_index + 1) % BULLET1_NUM
for each in bullet1:
if each.active == True:
each.move()
screen.blit(each.image, each.rect)
enemies_hit = pygame.sprite.spritecollide(each, enemies, False,pygame.sprite.collide_mask)
if enemies_hit:
each.active = False
for e in enemies_hit:
if e in mid_enemies or e in big_enemies:
e.energy -= 1
if e.energy == 0:
e.active = False
else:
e.active = False
else:
each.reset(heroPlane.rect.midtop)
enemies_collided = pygame.sprite.spritecollide(heroPlane, enemies, False, pygame.sprite.collide_mask)
if enemies_collided:
heroPlane.active = False
for each in enemies_collided:
each.active = False
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()