python和pygame实现烟花特效

python和pygame实现烟花特效

新年来临之际,来一个欢庆新年烟花祝贺,需要安装使用第三方库pygame,关于Python中pygame游戏模块的安装使用可见 https://blog.csdn.net/cnds123/article/details/119514520

效果图及源码

先看效果图:

源码如下:

import pygame
import random
import math

# 初始化pygame
pygame.init()

# 设置窗口大小
width, height = 800, 600
screen = pygame.display.set_mode((width, height))

# 定义颜色
black = (0, 0, 0)
red = (255, 0, 0)

# 定义烟花粒子
class Particle:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color
        self.radius = random.randint(2, 4)
        self.angle = random.uniform(0, 2 * math.pi)
        self.speed = random.uniform(1, 3)
        self.gravity = 0.1

    def move(self):
        self.x += math.cos(self.angle) * self.speed
        self.y += math.sin(self.angle) * self.speed + self.gravity
        self.radius -= 0.1  # 粒子逐渐变小

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

# 定义烟花
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.particles = []
        self.exploded = False
        self.explode_height = random.randint(100, 400)  # 设置爆炸高度

        self.speed = random.randint(5, 10)  # 设置上升速度
        self.angle = math.pi / 2  # 设置上升角度为垂直向上

    def launch(self):
        if not self.exploded:
            self.y -= self.speed * math.sin(self.angle)
            if self.y <= self.explode_height:  # 到达设定高度后爆炸
                self.explode()
                self.exploded = True

    def explode(self):
        for _ in range(100):  # 爆炸产生的粒子数量
            self.particles.append(Particle(self.x, self.y, self.color))

    def draw(self):
        if not self.exploded:
            pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 5)
        else:
            for particle in self.particles:
                particle.move()
                particle.draw()

#显示文字
#font = pygame.font.Font(None, 36)  # 设置字体和大小                
font = pygame.font.Font("C:\\Windows\\Fonts\\simsun.ttc", 36)        
text = font.render("龙年快乐", True, red)  # 渲染文本
text_rect = text.get_rect(center=(width // 2, height // 2))  # 获取文本的矩形区域

# 主循环
fireworks = []
clock = pygame.time.Clock()
running = True
while running:
    clock.tick(30)  # 控制帧率
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill(black)

    # 绘制文本
    screen.blit(text, text_rect)

    # 发射烟花
    if random.randint(1, 20) == 1:  # 控制烟花发射频率
        fireworks.append(Firework(random.randint(0, width), height))

    # 更新烟花并绘制
    for firework in fireworks[:]:
        firework.launch()
        firework.draw()
        if firework.exploded and all(p.radius <= 0 for p in firework.particles):
            fireworks.remove(firework)

    pygame.display.flip()

pygame.quit()

pygame在屏幕上显示字体的方法说明

使用pygame.font.Font函数来设置字体和大小,然后使用font.render函数将文本渲染为图像。最后,使用screen.blit函数将渲染好的文本图像绘制到屏幕上。

pygame.font.Font(None, 字体大小)使用系统默认字体,可能不支持汉字。None代表系统默认字体,如pygame.font.Font(None, 36),可能不支持汉字。怎么办?使用pygame.font. Font(“含路径的字体名”,字体大小),指定支持汉字的字体,如:pygame.font.Font("C:\\Windows\\Fonts\\simsun.ttc", 36),simsun.ttc是宋体,字体的路径和名称。在windows中如何确定字体的路径和名称呢?参见下图

改进:添加背景音乐,增添欢乐的气氛

在“# 主循环”部分前,添加如下代码

# 载入背景音乐
pygame.mixer.music.load("踩着开心的节奏 - 橙光音乐.mp3")
pygame.mixer.music.set_volume(0.5)  # 设置音量
pygame.mixer.music.play(-1)  #  播放背景音乐,-1表示循环播放

其中,pygame.mixer.music.load函数加载背景音乐文件(例如名为"踩着开心的节奏 - 橙光音乐.mp3"文件,并将其放在与代码文件相同的目录下),然后使用pygame.mixer.music.set_volume函数设置音量大小(范围为0.0-1.0)。最后,使用pygame.mixer.music.play函数播放背景音乐,参数-1表示循环播放。

请你使用自己背景音乐文件替换"踩着开心的节奏 - 橙光音乐.mp3",只要保证正确命名文件名和路径即可。

  • 16
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
好的,我可以帮你写一个基于Python烟花特效代码,首先我们需要安装pygame库来实现图形化界面。以下是代码实现: ```python import pygame import random # 初始化pygame pygame.init() # 设置屏幕宽高 screen_width = 600 screen_height = 600 # 创建屏幕对象 screen = pygame.display.set_mode((screen_width, screen_height)) # 设置标题 pygame.display.set_caption("Fireworks") # 定义一个烟花类 class Firework: def __init__(self, x, y, color): self.x = x self.y = y self.color = color self.radius = 2 self.burst_radius = 0 self.burst_speed = random.randint(1, 5) self.burst_thickness = 2 # 更新烟花位置和半径 def update(self): if self.burst_radius < 50: self.burst_radius += self.burst_speed else: self.radius = 0 # 绘制烟花 def draw(self): if self.radius > 0: pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius) else: pygame.draw.circle(screen, self.color, (self.x, self.y), self.burst_radius, self.burst_thickness) # 定义一个烟花效果类 class FireworkEffect: 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.fireworks = [] for i in range(30): firework = Firework(self.x, self.y, self.color) self.fireworks.append(firework) # 更新烟花效果 def update(self): for firework in self.fireworks: firework.update() # 绘制烟花效果 def draw(self): for firework in self.fireworks: firework.draw() # 创建烟花效果列表 firework_effects = [] # 游戏主循环 running = True while running: # 处理游戏事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 鼠标左键按下 if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: x, y = event.pos firework_effect = FireworkEffect(x, y) firework_effects.append(firework_effect) # 填充背景颜色 screen.fill((0, 0, 0)) # 更新和绘制烟花效果 for firework_effect in firework_effects: firework_effect.update() firework_effect.draw() # 刷新屏幕 pygame.display.flip() # 退出pygame pygame.quit() ``` 运行代码后,可以点击鼠标左键,实现烟花效果。希望能够帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学习&实践爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值