python画烟花绽放出字,如何用python画烟花

本文介绍了如何使用Python的Pygame库创建简单的烟花动画效果,通过鼠标点击生成烟花粒子并随时间变化。两个示例代码展示了从基础粒子到更复杂的动态烟花生成的过程。
摘要由CSDN通过智能技术生成

大家好,小编来为大家解答以下问题,python画烟花绽放出字,如何用python画烟花,今天让我们一起来看看吧!

Source code download: 本文相关源码

在这里插入图片描述
下面是一个用Python编写的简单烟花特效代码,使用了Pygame库来实现图形显示。请确保你已经安装了Pygame库,如果没有安装,可以使用pip install pygame来安装python爱心代码

import pygame
import random

# 初始化Pygame
pygame.init()

# 屏幕大小
width, height = 800, 600
screen = pygame.display.set_mode((width, height))

# 烟花粒子类
class Particle:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.color = (random.randint(50, 255), random.randint(50, 255), random.randint(50, 255))
        self.size = 5
        self.speed = random.randint(1, 5)
        self.angle = random.uniform(0, 2 * 3.14159)

    def move(self):
        self.x += self.speed * 0.5 * cos(self.angle)
        self.y += self.speed * 0.5 * sin(self.angle)
        self.size -= 0.05

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

particles = []

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    x, y = pygame.mouse.get_pos()

    for i in range(30):
        particles.append(Particle(x, y))

    for particle in particles:
        particle.move()
        if particle.size <= 0:
            particles.remove(particle)

    # 清屏
    screen.fill((0, 0, 0))

    # 绘制粒子
    for particle in particles:
        particle.draw()

    pygame.display.flip()

pygame.quit()

这个代码创建了一个窗口,当你点击鼠标时,会在鼠标位置生成烟花粒子效果。这只是一个简单的示例,你可以根据需要进行扩展和改进。注意,这只是一个基础的烟花特效,实际的烟花特效通常更加复杂和精致。

在这里插入图片描述

代码二:

import pygame
import sys
import random

pygame.init()

# 设置屏幕尺寸和标题
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Fireworks")

# 定义颜色
WHITE = (255, 255, 255)

# 定义烟花粒子类
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))
        self.radius = 2
        self.dx = random.randint(-5, 5)
        self.dy = random.randint(-5, 5)

    def move(self):
        self.x += self.dx
        self.y += self.dy

    def draw(self):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius)

particles = []

clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        # 当鼠标点击时,产生新的烟花
        if event.type == pygame.MOUSEBUTTONDOWN:
            x, y = pygame.mouse.get_pos()
            for _ in range(100):
                particle = Particle(x, y)
                particles.append(particle)

    screen.fill(WHITE)

    # 更新和绘制烟花粒子
    for particle in particles:
        particle.move()
        particle.draw()

    # 移除已经消失的烟花粒子
    particles = [particle for particle in particles if particle.radius < 100]

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值