用Python写一个烟花动画

代码实现:

import pygame
import random
import math

# 屏幕宽度
SCREEN_WIDTH = 1350
SCREEN_HEIGHT = 800

# 烟花颜色
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, angle, speed):
        self.x = x
        self.y = y
        self.angle = angle
        self.speed = speed
        self.size = random.randint(1, 2)  # 粒子大小
        self.color = random.choice(COLORS)
        self.lifetime = random.randint(1 * 60, 2 * 60)  # 粒子生命周期在1-2秒之间
        self.active = True

    def update(self):
        self.lifetime -= 1
        if self.lifetime <= 0:
            self.active = False
            return False

        dx = math.cos(self.angle) * self.speed
        dy = math.sin(self.angle) * self.speed
        self.x += dx
        self.y += dy
        return True


class Firework:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color
        self.particles = []
        for _ in range(random.randint(10, 300)):  # 粒子数量
            angle = random.uniform(0, 2 * math.pi)  
            speed = random.uniform(0, 3)  # 粒子中心,粒子速度
            particle = Particle(self.x, self.y, angle, speed)
            self.particles.append(particle)

    def update(self):
        for particle in self.particles:
            if not particle.update():
                self.particles.remove(particle)

    def draw(self, screen):
        for particle in self.particles:
            pygame.draw.circle(screen, particle.color, (int(particle.x), int(particle.y)), particle.size)


pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Fireworks')
clock = pygame.time.Clock()

fireworks = []
firework_count = 0  # 用于控制烟花的创建间隔
running = True

while running:
    clock.tick(60)  # 每秒60帧
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False
# 屏幕背景
    screen.fill((0, 0, 0))

    for firework in fireworks:
        firework.update()
        firework.draw(screen)

        if not firework.particles:  # 如果烟花的所有粒子都消失,则移除烟花
            fireworks.remove(firework)

    if firework_count == 0:  # 控制烟花的创建间隔
        fireworks.append(
            Firework(random.randint(0, SCREEN_WIDTH), random.randint(0, SCREEN_HEIGHT), random.choice(COLORS)))
        firework_count = random.randint(15, 20)  # 随机设置计数器,实现无序效果,烟花的出现间隔为1-2秒
    else:
        firework_count -= 1

    pygame.display.flip()

pygame.quit()

效果图:

Python生成exe文件:

安装打包软件:

安装PyInstaller:在命令行中执行pip install pyinstaller命令即可安装。

打包项目:

1.打包项目:在命令行中进入项目所在目录,执行pyinstaller --onefile your_script.py命令即可开始打包。其中--onefile表示将项目打包成一个可执行文件,your_script.py为项目入口文件。

2.会生成一个dist的目录,里面就是Python生成的exe文件。

当然可以!下面是一个Python的简单烟花小程序的示例: ```python import pygame import random # 初始化pygame pygame.init() # 设置窗口大小和标题 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("烟花小程序") # 定义烟花类 class Firework: 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)) self.radius = 3 self.speed = random.randint(1, 5) self.exploded = False def move(self): if not self.exploded: self.y -= self.speed def explode(self): self.exploded = True for _ in range(100): sparks.append(Spark(self.x, self.y, self.color)) def draw(self): pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius) # 定义火花类 class Spark: def __init__(self, x, y, color): self.x = x self.y = y self.color = color self.radius = 2 self.angle = random.uniform(0, 2 * math.pi) self.speed = random.uniform(1, 5) def move(self): self.x += math.cos(self.angle) * self.speed self.y += math.sin(self.angle) * self.speed def draw(self): pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius) # 创建烟花和火花列表 fireworks = [] sparks = [] # 游戏主循环 running = True while running: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 生成新的烟花 if random.random() < 0.01: fireworks.append(Firework(random.randint(0, screen_width), screen_height)) # 更新烟花和火花的位置 for firework in fireworks: firework.move() if firework.y < 200 and not firework.exploded: firework.explode() for spark in sparks: spark.move() # 绘制烟花和火花 screen.fill((0, 0, 0)) for firework in fireworks: firework.draw() for spark in sparks: spark.draw() # 移除已经消失的火花 sparks = [spark for spark in sparks if spark.radius > 0] # 更新屏幕显示 pygame.display.flip() # 退出游戏 pygame.quit() ``` 这个程序使用了Pygame库来创建窗口和处理图形绘制。它通过不断生成新的烟花对象,并在一定高度上爆炸成火花对象来模拟烟花效果。每个烟花和火花对象都有自己的位置、颜色、半径和速度等属性,通过不断更新和绘制它们来实现动画效果。 希望这个示例能帮助到你!如果你有任何相关问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值