pygame过年烟花
时间: 2025-01-24 16:00:49 浏览: 55
### 使用Pygame实现春节烟花特效
#### 初始化环境与设置
为了创建一个具有春节特色的烟花效果,首先需要安装并导入必要的库。确保已经安装了`pygame`库。
```bash
pip install pygame
```
接着,在代码文件顶部引入所需的模块:
```python
import sys
import random
import math
import pygame
from pygame.locals import *
```
#### 定义常量和初始化游戏窗口
定义一些全局变量来简化后续开发过程,并完成基本的游戏界面配置[^1]。
```python
# 屏幕尺寸设定
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
BACKGROUND_COLOR = (0, 0, 0)
def init_game():
global screen, clock
pygame.init()
# 设置屏幕大小及标题栏文字
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Happy New Year Fireworks')
# 创建时钟对象用于控制帧率
clock = pygame.time.Clock()
init_game()
```
#### 构建烟花类
设计一个名为`Firework`的类用来表示单个烟花的行为模式,包括但不限于位置、速度以及颜色属性等。这里还实现了烟花上升到特定高度后爆炸的功能。
```python
class Firework(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.color = tuple(random.randint(0, 255) for _ in range(3)) # 随机生成颜色
self.x = random.uniform(-SCREEN_WIDTH / 4, SCREEN_WIDTH / 4) + SCREEN_WIDTH / 2
self.y = SCREEN_HEIGHT + abs(self.x * 0.2)
self.speed_y = -random.uniform(7, 9)
self.exploded = False
self.particles = []
def update(self):
if not self.exploded:
self.y += self.speed_y
if self.y < SCREEN_HEIGHT / 2 and len(self.particles) == 0:
self.explode()
else:
new_particles = []
for particle in self.particles:
alive = particle.update()
if alive:
new_particles.append(particle)
self.particles = new_particles
def explode(self):
self.exploded = True
angle_range = list(range(0, 360, 15))
speeds = [(math.cos(math.radians(angle)), math.sin(math.radians(angle))) for angle in angle_range]
for speed_x, speed_y in speeds:
self.particles.append(Particle(
self.x,
self.y,
speed_x * 3,
speed_y * 3,
self.color))
def draw(self):
if not self.exploded:
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 5)
else:
for particle in self.particles:
particle.draw()
```
#### 实现粒子系统
为了让烟花看起来更加真实生动,还需要构建另一个辅助性的`Particle`类作为组成烟花的小碎片,它们会在空中四处飞溅形成美丽的图案。
```python
class Particle(Firework):
def __init__(self, x, y, vx, vy, color):
self.x = x
self.y = y
self.vx = vx
self.vy = vy
self.age = 0
self.life_span = random.uniform(0.5, 1.5)
self.color = color
def update(self):
self.age += 1/60.
self.x += self.vx
self.y += self.vy
self.vy += 0.1 # 加入重力效应使粒子下落加速
return self.age <= self.life_span
def draw(self):
size = max(int((self.life_span-self.age)*10), 0)
pygame.draw.rect(screen, self.color, Rect(self.x-size//2, self.y-size//2, size, size))
```
#### 主循环逻辑
最后一步就是编写主事件处理循环,负责监听用户的输入操作(如通过键盘调整发射频率),同时不断更新场景中的各个元素状态直至关闭应用程序为止。
```python
fireworks = []
while True:
dt = clock.tick(60)/1000.
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
fireworks.append(Firework())
screen.fill(BACKGROUND_COLOR)
for firework in fireworks[:]:
firework.update()
firework.draw()
if firework.exploded and len(firework.particles)==0:
fireworks.remove(firework)
pygame.display.flip()
```
上述代码片段展示了如何利用Python编程语言配合Pygame图形库创造出绚丽多彩的新春佳节烟火表演视觉盛宴。
阅读全文
相关推荐











