python代码画动态烟花源码,烟花代码编程python复制

本文介绍了如何使用Python的pygame库创建动态烟花效果,并指导读者如何将代码打包成.exe可执行文件,包括安装PyInstaller工具和打包步骤。
摘要由CSDN通过智能技术生成

大家好,小编来为大家解答以下问题,爱心代码编程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文件。

Python烟花代码通常指用于创建动态效果的库或模块,特别是在处理图形界面(GUI)或者数据可视化时。比如,`pygame`是一个常用的Python游戏开发库,可以用来制作简单的动效果,包括“烟花”般的效果。通过控制像素点的颜色、位置和运动轨迹,你可以模拟出各种视觉上的火花绽放。 以下是一个简单的使用pygame绘制烟花效果的例子: ```python import pygame import random # 初始化Pygame pygame.init() # 设置窗口大小 screen = pygame.display.set_mode((500, 500)) # 烟花颜色和起始位置 colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)] positions = [(random.randint(0, screen.get_width()), random.randint(0, screen.get_height())) for _ in range(100)] while True: # 清空屏幕 screen.fill((0, 0, 0)) # 遍历烟花,每次更新位置并改变颜色 for i, (color, pos) in enumerate(positions): size = int(random.random() * 5 + 1) pygame.draw.circle(screen, color, pos, size) if size <= 0 or i >= len(positions) - 1: # 当烟花消失或所有烟花发射完后删除 positions.pop(i) else: pos[0] += random.randint(-1, 1) # 随机移动 pos[1] += random.randint(-1, 1) # 更新显示 pygame.display.flip() # 检查事件(如关闭窗口) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() ``` 这个代码会创建一个包含100个随机位置和颜色的烟花,每个烟花会在屏幕上随机移动直到消失。如果你想创建更复杂的烟花效果,可以考虑使用专门的动库或者结合数学原理模拟烟花的爆炸过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值