pygame

import pygame
import random

# 初始化 Pygame
pygame.init()
# 声音初始化
pygame.mixer.init()

# 设置窗口大小
screen_width = 1200
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pygame Background Image")

# 加载背景图片
background_image = pygame.image.load("background.png")
# 调整背景图片大小以适应窗口
background_image = pygame.transform.scale(background_image, (screen_width, screen_height))

# 定义颜色
BLACK = (0, 0, 0)

# 加载音效
explode_sound = pygame.mixer.Sound('explode.wav')


# 定义烟花类
class Firework:
def __init__(self):
# 随机选择烟花发射的位置
self.x = random.randint(92, screen_width)
self.y = screen_height
# 随机设置烟花的速度
self.vy = random.randint(-7, -4)
# 随机初始化烟花的颜色
self.color = pygame.Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
# 定义颜色变化的步长
self.color_step = random.uniform(0.2, 0.7)
# 记录烟花是否爆炸
self.exploded = False
# 存储烟花爆炸后的粒子
self.particles = []

def update(self):
if not self.exploded:
# 更新烟花的位置
self.y += self.vy
# 实现颜色渐变
r = max(0, min(255, self.color.r + random.choice([-1, 1]) * self.color_step))
g = max(0, min(255, self.color.g + random.choice([-1, 1]) * self.color_step))
b = max(0, min(255, self.color.b + random.choice([-1, 1]) * self.color_step))
self.color = pygame.Color(int(r), int(g), int(b))
# 当烟花到达一定高度时爆炸
if self.y < screen_height // 6:
self.explode()
else:
# 更新粒子的状态
for particle in self.particles[:]:
particle[0] += particle[2]
particle[1] += particle[3]
particle[3] += 0.1
particle[4] -= 1
if particle[4] <= 0:
self.particles.remove(particle)

def explode(self):
self.exploded = True
explode_sound.play() # 播放爆炸音效
# 生成多个粒子
for _ in range(600):
angle = random.uniform(0, 2 * 3.14159)
speed = random.uniform(2, 4)
vx = speed * pygame.math.Vector2(1, 0).rotate_rad(angle).x
vy = speed * pygame.math.Vector2(1, 0).rotate_rad(angle).y
# 粒子信息:[x, y, vx, vy, lifespan]
self.particles.append([self.x, self.y, vx, vy, random.randint(20, 40)])

def draw(self):
if not self.exploded:
# 绘制未爆炸的烟花
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 2)
else:
# 绘制爆炸后的粒子
for particle in self.particles:
pygame.draw.circle(screen, self.color, (int(particle[0]), int(particle[1])), 1)

# 创建烟花列表


fireworks = []

# 定义楼房区域
building_height = 300 # 根据实际背景图片调整(楼房占屏幕下方300像素)
sky_height = screen_height - building_height # 天空区域高度

# 主循环
clock = pygame.time.Clock()
running = True
while running:
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# 随机生成新的烟花
if random.random() < 0.05:
fireworks.append(Firework())

# 分层渲染
# 1. 绘制天空背景
screen.blit(background_image, (0, 0), (0, 0, screen_width, sky_height)) # 仅绘制天空部分

# 2. 绘制未爆炸的烟花(在楼房后方)
for firework in fireworks[:]:
if not firework.exploded:
firework.update()
firework.draw()

# 3. 绘制楼房前景
screen.blit(background_image, (0, sky_height), (0, sky_height, screen_width, building_height))

# 4. 绘制爆炸后的烟花(在楼房前方)
for firework in fireworks[:]:
if firework.exploded:
firework.update()
firework.draw()
if firework.exploded and len(firework.particles) == 0:
fireworks.remove(firework)

# 更新显示
pygame.display.flip()
clock.tick(25)

# 退出 Pygame
pygame.quit()

你可能感兴趣的:(pygame,python,开发语言)