源码:
#Lucifer的雷电
import pygame, os
import time
import random
from pygame.sprite import Sprite
from pygame.sprite import Group
def fire_music():
# 设置开火音乐
effect = pygame.mixer.Sound('sounds/fire.wav')
pygame.mixer.Sound.play(effect)
class Boss(Sprite):
def __init__(self,boss_img_name):
super().__init__()
# 加载BOSS图片
self.image = pygame.image.load('图片/boss_1.png').convert_alpha()
# 转换BOSS大小
# self.image = pygame.transform.scale(self.image, (1, 12))
# 生成BOSS矩形框架
self.rect = self.image.get_rect()
self.blood = 1000
# boss左右移动的速度
self.speed = 3.5
def move(self):
if self.rect.centerx>=512:
self.speed =-self.speed
if self.rect.centerx<=0:
self.speed = -self.speed
self.rect.centerx +=self.speed
class Enemy(Sprite):
def __init__(self,screen):
# 必须设置继承精灵 不然在使用精灵函数时会报错
super().__init__()
# 获取屏幕对象
self.screen = screen
# 随机 生成5个编号
alien_num = random.randint(1,5)
# 随机 加载五个飞机中的某个
self.image = pygame.image.load('图片/alien_' + str(alien_num) + '.png')
# picture = pygame.transform.scale(picture, (1280, 720))
self.image = pygame.transform.scale(self.image,(62,62))
# 获取飞机的 rect
self.rect = self.image.get_rect()
# 击落本机获得的分数
self.score = 10
# 加载子弹的图片
self.bullet_img = pygame.image.load("图片/alien_bullet.png").convert_alpha()
self.bullet_img = pygame.transform.scale(self.bullet_img, (12, 12))
# 以下为可以调节子弹尺寸的代码
# picture = pygame.transform.scale(picture, (1280, 720))
#飞机的移动速度
self.speed = random.randint(3,5)
#生成子弹精灵组合
self.bullets = Group()
# 敌机射击频率
self.shoot_frequency = 0
# 飞机出现
def move(self):
self.rect.top += 5
#暂时不用射击
# self.shoot()
# self.moveBullet()
# 发射子弹
def shoot(self):
if self.shoot_frequency % 200 == 0:
bullet = Enemy_Bullet(self.bullet_img, self.rect.midbottom)
self.bullets.add(bullet)
self.shoot_frequency += 1
if self.shoot_frequency > 200:
self.shoot_frequency = 1
# 删除子弹
def moveBullet(self):
for bullet in self.bullets:
bullet.move()
if bullet.rect.bottom < 0:
self.bullets.remove(bullet)
# 绘制子弹
def drawBullets(self, scr):
self.bullets.draw(scr)
class Enemy_Bullet(pygame.sprite.Sprite):
def __init__(self, init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("图片/alien_bullet.png").convert_alpha()
self.image = pygame.transform.scale(self.image, (12, 12))
self.rect = self.image.get_rect()
# 敌机子弹初始位置设置
self.rect.midbottom = init_pos
self.rect.centery +=36
self.speed = 8
def move(self):