# 昵 称:XieXu & CSDN@追光者♂
# 时 间: 2023/10/10/0010 20:33
import random
import pygame
import math
import sys
pygame.init()
# 窗口大小和帧率设置
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Fireworks')
clock = pygame.time.Clock()
FPS = 60
# 定义烟花类
class Firework:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
self.done = False
self.exploded = False
self.particles = []
self.num_particles = 50
self.velocity = random.randint(5, 10)
self.theta = 0
self.radius = random.randint(100, 200)
self.burst_radius = self.radius * random.uniform(1.5, 2.5)
# 计算粒子位置
def update_particles(self):
if self.exploded:
for particle in self.particles:
particle[0] += particle[2]
particle[1] += particle[3]
particle[3] += 0.1
# 更新烟花状态
def update(self):
if not self.exploded:
self.x += self.velocity * math.cos(self.theta)
self.y -= self.velocity * math.sin(self.theta)
self.theta += 0.05
if self.y <= self.radius:
self.exploded = True
for i in range(self.num_particles):
angle = random.uniform(0, 2 * math.pi)
speed = random.uniform(1, 5)
self.particles.append([self.x, self.y, speed * math.cos(angle), speed * math.sin(angle)])
# 绘制烟花
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:
pygame.draw.circle(screen, self.color, (int(particle[0]), int(particle[1])), 2)
# 判断烟花是否结束
def is_done(self):
return self.done
# 生成随机烟花
def spawn_random_firework():
x = random.randint(50, 750)
y = 550
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
return Firework(x, y, color)
# 初始化烟花列表
fireworks = []
# 渲染循环
while True:
# 处理游戏退出事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 添加新的烟花
if random.random() < 0.1:
fireworks.append(spawn_random_firework())
# 绘制背景
screen.fill((0, 0, 0))
# 更新和绘制烟花
for firework in fireworks:
firework.update()
firework.update_particles()
firework.draw()
if firework.is_done():
fireworks.remove(firework)
# 更新屏幕
pygame.display.flip()
# 控制帧率
clock.tick(FPS)
# 退出游戏
pygame.quit()
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
大家好,这是2023-10-10,今天通过Python语言编写的一个烟花小球的特效,此为完整的源代码,可以直接在Python环境下运行,欢迎大家下载、练习和交流。另外,主页专栏“Python从入门到人工智能”持续更新中,也热烈欢迎大家订阅和学习。
资源推荐
资源详情
资源评论
收起资源包目录
使用Python实现的烟火小球特效__yanhua_.zip (1个子文件)
使用Python实现的烟火小球特效__yanhua_.py 3KB
共 1 条
- 1
资源评论
追光者♂
- 粉丝: 2w+
- 资源: 516
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功