@敌机类
import random
import pygame
from pygame.sprite import Sprite
# 敌机类
class SmallEnemy(Sprite):
# 构造方法:确定颜值、确定初始位置、引擎功率
def __init__(self, winWidth, winHeight):
Sprite.__init__(self)
self.winWidth = winWidth
self.winHeight = winHeight
# 外形
self.mSurface = pygame.image.load("./images/enemy1.png").convert_alpha()
# 爆炸
self.dSurface1 = pygame.image.load("./images/enemy1_down1.png").convert_alpha()
self.dSurface2 = pygame.image.load("./images/enemy1_down2.png").convert_alpha()
self.dSurface3 = pygame.image.load("./images/enemy1_down3.png").convert_alpha()
self.dSurface4 = pygame.image.load("./images/enemy1_down4.png").convert_alpha()
self.dList = [self.dSurface1, self.dSurface2, self.dSurface3, self.dSurface4]
self.dIndex = 0
# 确定敌机位置
self.rect = self.mSurface.get_rect()
self.reset()
# 敌机飞行速度
self.speed = 5
# 添加碰撞检测遮罩
self.mask = pygame.mask.from_surface(self.mSurface)
# 重置敌机位置和生命
def reset(self):
self.rect.left = random.randint(0, self.winWidth - self.rect.width)
self.rect.top = 0 - random.randint(0, 1000)
self.isAlive = True
self.dIndex = 0
# 机械地向下飞行
def move(self):
if self.rect.top < self.winHeight:
self.rect.bottom += self.speed
else:
self.isAlive = False
self.reset()
# fCount=当前第几帧
def destroy(self, fCount, winSurface,dSound):
winSurface.blit(self.dList[self.dIndex],self.rect)
if fCount % 3 == 0:
# 切下一副面孔
self.dIndex += 1
if self.dIndex == 4:
dSound.play()
self.reset()
@添加敌机
# 创建敌机Group
seGroup = pygame.sprite.Group()
for i in range(ENEMY_NUM):
se = SmallEnemy(width, height)
seGroup.add(se)
@绘制敌机
# 绘制敌机
for se in seGroup:
windowSurface.blit(se.mSurface, se.rect)
# 每一帧都让敌机飞行5公里
se.move()
@子弹类
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
# 构造方法:颜值、位置、速度
# position = 机头位置
def __init__(self):
super().__init__()
# 颜值
self.mSurface = pygame.image.load("./images/bullet1.png").convert_alpha()
self.rect = self.mSurface.get_rect()
# 将子弹的顶部中央对齐机头位置
# self.reset(position)
self.isAlive = False
# 子弹速度
self.speed = 15
# 添加碰撞检测遮罩
self.mask = pygame.mask.from_surface(self.mSurface)
# 重置子弹位置和生命
def reset(self, position):
self.rect.left = position[0] - self.rect.width // 2
self.rect.bottom = position[1]
self.isAlive = True
# 飞行方法
def move(self):
if self.rect.bottom > 0:
self.rect.top -= self.speed
else:
self.isAlive = False
@射击
# 重复与复用10颗子弹
BULLET_NUM = 10
# 创建子弹
bList = []
bIndex = 0
for i in range(BULLET_NUM):
b = Bullet()
bList.append(b)
# 每10帧在机头射出一颗子弹
if count % 10 == 0:
b = bList[bIndex] # 取出一颗子弹
b.reset(hero.rect.midtop) # 立即装载到【当前】机头位置
# windowSurface.blit(b.mSurface, b.rect) # 画子弹
bulletSound.play()#呼啸声
bIndex = (bIndex + 1) % BULLET_NUM # 序号递增
# 每帧都让子弹飞
for b in bList:
if b.isAlive:
windowSurface.blit(b.mSurface, b.rect) # 画子弹
b.move()
@敌机爆炸
# 子弹-敌机碰撞检测
for b in bList:
if b.isAlive:
hitEnemyList = pygame.sprite.spritecollide(b, seGroup, False, pygame.sprite.collide_mask)
if len(hitEnemyList) > 0:
b.isAlive = False
for se in hitEnemyList:
se.isAlive = False
# 绘制敌机
for se in seGroup:
if se.isAlive:
windowSurface.blit(se.mSurface, se.rect)
# 每一帧都让敌机飞行5公里
se.move()
else:
se.destroy(count, windowSurface, bombSound)
# fCount=当前第几帧
def destroy(self, fCount, winSurface,dSound):
winSurface.blit(self.dList[self.dIndex],self.rect)
if fCount % 3 == 0:
# 切下一副面孔
self.dIndex += 1
if self.dIndex == 4:
dSound.play()
self.reset()
@游戏记分
# 业务变量
score = 0 # 统计得分
# 精灵碰撞检测
for b in bList:
if b.isAlive:
hitEnemyList = pygame.sprite.spritecollide(b, seGroup, False, pygame.sprite.collide_mask)
if len(hitEnemyList) > 0:
b.isAlive = False
for se in hitEnemyList:
se.isAlive = False
score += 100 # 打死一个敌人加100分