python烟花代码

import pygame
import random
import math

# 初始化pygame
pygame.init()

# 设置屏幕尺寸
screen_width, screen_height = 800, 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("烟花效果")

# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255)]


# 定义烟花粒子类
class Particle:
    def __init__(self, x, y, color, vx, vy, size):
        self.x = x
        self.y = y
        self.color = color
        self.vx = vx
        self.vy = vy
        self.size = size
        self.life = 255  # 粒子生命周期(透明度)

    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.vy += 0.1  # 重力效果
        self.life -= 5  # 生命周期递减
        if self.life < 0:
            self.life = 0

    def draw(self, screen):
        alpha = self.life / 255
        color = (self.color[0], self.color[1], self.color[2], alpha)
        pygame.draw.circle(screen, color, (int(self.x), int(self.y)), self.size)

    # 定义烟花类


class Fireworks:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.particles = []
        self.explode()

    def explode(self):
        num_particles = 100
        for _ in range(num_particles):
            angle = random.uniform(0, 2 * math.pi)
            speed = random.uniform(5, 15)
            vx = speed * math.cos(angle)
            vy = speed * math.sin(angle)
            size = random.randint(2, 5)
            color = random.choice(COLORS)
            self.particles.append(Particle(self.x, self.y, color, vx, vy, size))

    def update(self):
        for particle in self.particles:
            particle.update()
            if particle.life == 0:
                self.particles.remove(particle)

    def draw(self, screen):
        for particle in self.particles:
            particle.draw(screen)

        # 主循环


running = True
clock = pygame.time.Clock()
fireworks_list = []

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            x, y = pygame.mouse.get_pos()
            fireworks_list.append(Fireworks(x, y))

            # 更新烟花和粒子
    for fireworks in fireworks_list[:]:
        fireworks.update()
        if not fireworks.particles:  # 如果没有粒子了,就移除这个烟花对象
            fireworks_list.remove(fireworks)

            # 绘制
    screen.fill(BLACK)
    for fireworks in fireworks_list:
        fireworks.draw(screen)

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

烟花

### 使用 Python 实现烟花效果 为了实现一个简单的烟花动画,在 Python 中可以利用 `pygame` 库完成此任务。下面提供了一个基于 `pygame` 的烟花效果示例代码,并包含了详细的注释说明。 ```python import pygame import random import math # 初始化 Pygame 和设置屏幕尺寸 pygame.init() screen_width, screen_height = 800, 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Fireworks Show") class Particle: """定义粒子类""" def __init__(self, x, y): self.x = x self.y = y self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) angle = random.uniform(0, 2 * math.pi) speed = random.uniform(1, 7) self.dx = speed * math.cos(angle) self.dy = -speed * math.sin(angle) def create_firework(): """创建一组新的烟火粒子""" particles = [] center_x = random.randint(0, screen_width) center_y = screen_height + 10 for _ in range(random.randint(50, 100)): particle = Particle(center_x, center_y) particles.append(particle) return particles particles_list = [] running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 添加新烟花到列表中 if random.random() < 0.05: new_particles = create_firework() particles_list.extend(new_particles) screen.fill((0, 0, 0)) updated_particles = [] for p in particles_list: p.y += p.dy p.x += p.dx gravity = 0.05 air_resistance = 0.98 p.dy += gravity p.dx *= air_resistance p.dy *= air_resistance pygame.draw.circle(screen, p.color, (int(p.x), int(p.y)), 3) if p.y <= screen_height and 0 <= p.x <= screen_width: updated_particles.append(p) particles_list = updated_particles[:] pygame.display.flip() pygame.time.delay(30) pygame.quit() ``` 上述代码展示了如何使用 `pygame` 创建基本的二维平面内的烟花爆炸视觉效果[^1]。通过调整参数和增加更多功能,可以使模拟更加逼真并具有艺术感[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值