python炫酷烟花表白源代码,html代码烟花特效python

大家好,小编来为大家解答以下问题,python绘制烟花特定爆炸效果,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_.py命令即可开始打包熟练掌握Python就业是不是好一点。其中--onefile表示将项目打包成一个可执行文件,your_.py为项目入口文件。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值