news 2026/1/8 8:47:30

Python烟花秀:2026新年祝福代码解析 BGM版

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Python烟花秀:2026新年祝福代码解析 BGM版

目录

前言

代码说明

库函数

窗口尺寸

颜色

粒子类和烟花类

甲骨文马

字体部分

背景音乐

注意事项

源码


前言

先看看效果截图吧。今天闲来无事突然想试试,好久没用电脑了,一时没找到录屏软件在哪。简单说明一下:这段Python代码实现的效果是烟花绽放后,背景音乐响起,文字缓缓浮现。虽然实现原理很简单,却花了我两个小时,不过整个过程挺有意思的。我会标注出可以修改的部分和注意事项,其他未说明的地方建议保持原样。

代码说明

库函数
import pygame import random import math import time

注意pygame,可能有些小伙伴没有,可以使用pip下载,具体也可以看作者之前的文章pip进行库下载https://blog.csdn.net/weixin_58498967/article/details/144678592

窗口尺寸
# 设置屏幕尺寸和标题 WIDTH, HEIGHT = 800, 600

可以修改,但需要注意,如果改动过大烟花部分也需要修改

颜色
BLACK = (0, 0, 0)

三个参数对应的是三原色比例,0-255可改,需要更改颜色的话可以自己搜喜欢颜色的三原色配比

粒子类和烟花类

粒子类和烟花类均不建议修改,如需修改建议找一个好一点的AI先给你详细解释再自己修改小部分参数,谨慎交给AI直接修改,小心变得面目全非。

甲骨文马

这一部分不需要可以注释,可以灵活修改,改坏了就注释呗。其实就是描点画图一样,可以化成自己的美照。

字体部分
# 设置字体 - 使用默认字体即可显示英文 font = pygame.font.Font(None, 64) text_content = "2026 happy new year" text_surface = font.render(text_content, True, GOLD) text_rect = text_surface.get_rect(center=(WIDTH // 2, HEIGHT // 3))

第二行为文字内容,可以修改自己想要的。建议用英文,每个人电脑字体环境不一样,英文是通用的。但如果你的电脑安装了其它文字环境可以使用其它字体,中文或者楷体等,谨慎使用吧,总之能用什么用什么。

第四行是文字位置,最里面的参数2 3是可以更改的,自己试试就知道了。

# 记录程序开始时间 start_time = time.time() text_alpha = 0 # 文字初始透明度为0 horse_alpha = 0 # 甲骨文初始透明度为0 fade_in_start = 1.5 # 1.5秒后开始淡入 fade_in_duration = 2.0 # 淡入持续2秒

文字渐入效果,备注应该很清楚了,直接修改后面的参数就行了

背景音乐
# 尝试加载背景音乐(可选) try: pygame.mixer.music.load("background_music.mp3") pygame.mixer.music.set_volume(0.5) pygame.mixer.music.play(-1) # 循环播放 except: print("无法加载背景音乐,将继续无声播放")
注意事项
  • 背景音乐建议mp3文件形式
  • 背景音乐与你的python程序文件必须同一路径下(同一目录下)
  • 背景音乐名字必须与第二行代码引用的文件名一致,要么改代码中的名字,要么改mp3文件的名字,同时建议最好使用英文
    pygame.mixer.music.load("background_music.mp3")

源码

import pygame import random import math import time # 初始化pygame pygame.init() pygame.mixer.init() # 初始化音频模块 # 设置屏幕尺寸和标题 WIDTH, HEIGHT = 800, 600 SCREEN = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("2026 New Year Fireworks") # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) LIGHT_RED = (255, 50, 50) # 甲骨文颜色 - 浅红色 DARK_RED = (200, 0, 0) # 甲骨文深色部分 GREEN = (0, 255, 0) BLUE = (0, 0, 255) YELLOW = (255, 255, 0) GOLD = (255, 215, 0) # 设置帧率 clock = pygame.time.Clock() FPS = 60 # 粒子类 - 调整参数使烟花更细腻 class Particle: def __init__(self, x, y, color): self.x = x self.y = y self.color = color self.size = random.randint(1, 3) # 减小粒子大小,更细腻 # 随机速度和角度 - 调整速度范围使运动更优雅 angle = random.uniform(0, 2 * math.pi) speed = random.uniform(0.5, 4) # 降低速度上限 self.vx = math.cos(angle) * speed self.vy = math.sin(angle) * speed # 重力加速度 - 减小重力使粒子上升更久 self.gravity = 0.02 # 生命周期 - 延长生命周期使烟花持续更久 self.lifetime = 80 + random.randint(0, 30) self.age = 0 # 透明度 self.alpha = 255 def update(self): # 更新位置 self.x += self.vx self.y += self.vy # 应用重力 self.vy += self.gravity # 应用空气阻力 - 增加阻力使运动更自然 self.vx *= 0.97 self.vy *= 0.97 # 更新生命周期和透明度 self.age += 1 if self.age >= self.lifetime: return False # 随时间减少透明度 - 更平滑的淡出效果 self.alpha = 255 - (self.age / self.lifetime) * 255 return True def draw(self, screen): # 创建带透明度的颜色 color_with_alpha = (*self.color, int(self.alpha)) # 绘制粒子 pygame.draw.circle(screen, color_with_alpha, (int(self.x), int(self.y)), self.size) # 烟花类 - 调整参数使烟花更细腻 class Firework: def __init__(self): # 随机发射位置 self.x = random.randint(100, WIDTH - 100) self.y = HEIGHT # 随机颜色 - 使用更丰富的颜色组合 if random.random() < 0.3: # 金色和黄色系,更喜庆 self.color = (255, random.randint(200, 255), random.randint(0, 100)) else: # 其他鲜艳颜色 self.color = (random.randint(50, 255), random.randint(50, 255), random.randint(50, 255)) # 发射速度 - 调整速度使上升更均匀 self.vy = random.uniform(-8, -5) self.vx = random.uniform(-1.5, 1.5) # 爆炸状态 self.exploded = False # 粒子列表 self.particles = [] # 烟花大小 self.size = 3 # 减小未爆炸烟花的大小 # 上升阶段的重力 self.gravity = 0.05 def update(self): if not self.exploded: # 未爆炸时继续上升 self.y += self.vy self.x += self.vx self.vy += self.gravity # 当速度变为正时,开始下降,此时爆炸 if self.vy >= 0: self.explode() else: # 爆炸后更新粒子 if not self.particles: return False # 更新所有粒子,移除已死亡的粒子 self.particles = [p for p in self.particles if p.update()] return True def explode(self): self.exploded = True # 生成大量粒子 - 增加粒子数量使烟花更细腻 particle_count = random.randint(80, 200) for _ in range(particle_count): self.particles.append(Particle(self.x, self.y, self.color)) def draw(self, screen): if not self.exploded: # 绘制未爆炸的烟花(上升的点) pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.size) else: # 绘制爆炸后的粒子 for particle in self.particles: particle.draw(screen) # 绘制甲骨文"马"字的函数 def draw_oracle_horse(screen, x, y, size, alpha): """在指定位置绘制甲骨文的"马"字,支持缩放和透明度""" # 甲骨文"马"字的线条组成 # 参考甲骨文马字的典型形态:https://upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Oracle_horse.svg/1200px-Oracle_horse.svg.png # 创建带透明度的颜色 red_with_alpha = (*LIGHT_RED, alpha) dark_red_with_alpha = (*DARK_RED, alpha) # 设置线条宽度 line_width = max(3, int(4 * size)) # 甲骨文"马"字的线条坐标 # 基于中心点(x, y)进行偏移 lines = [ # 头部和嘴巴 [(x - 30*size, y - 40*size), (x - 10*size, y - 50*size)], # 头部 [(x - 10*size, y - 50*size), (x + 10*size, y - 45*size)], # 嘴部 [(x + 10*size, y - 45*size), (x + 20*size, y - 35*size)], # 嘴部延伸 # 耳朵 [(x - 25*size, y - 50*size), (x - 30*size, y - 65*size)], # 左耳 [(x - 20*size, y - 55*size), (x - 15*size, y - 65*size)], # 右耳 # 颈部 [(x - 25*size, y - 35*size), (x - 20*size, y - 10*size)], # 颈部左侧 [(x - 15*size, y - 30*size), (x - 10*size, y - 5*size)], # 颈部右侧 # 身体 [(x - 20*size, y - 10*size), (x - 15*size, y + 20*size)], # 身体左侧 [(x - 10*size, y - 5*size), (x - 5*size, y + 25*size)], # 身体右侧 # 前腿 [(x - 18*size, y + 10*size), (x - 22*size, y + 40*size)], # 左前腿 [(x - 12*size, y + 15*size), (x - 15*size, y + 45*size)], # 右前腿 # 后腿 [(x - 8*size, y + 15*size), (x - 10*size, y + 40*size)], # 左后腿 [(x - 2*size, y + 20*size), (x - 3*size, y + 45*size)], # 右后腿 # 尾巴 [(x - 20*size, y - 10*size), (x - 40*size, y - 5*size)], # 尾巴基础 [(x - 40*size, y - 5*size), (x - 50*size, y - 15*size)], # 尾巴延伸 [(x - 45*size, y - 5*size), (x - 40*size, y - 20*size)], # 尾巴分支 # 鬃毛 [(x - 25*size, y - 35*size), (x - 35*size, y - 25*size)], # 鬃毛左侧 [(x - 20*size, y - 30*size), (x - 30*size, y - 20*size)], # 鬃毛中间 [(x - 15*size, y - 25*size), (x - 25*size, y - 15*size)], # 鬃毛右侧 ] # 绘制所有线条 for line in lines: pygame.draw.lines(screen, red_with_alpha, False, line, line_width) # 绘制眼睛(简化为一个点) eye_pos = (x - 15*size, y - 45*size) pygame.draw.circle(screen, dark_red_with_alpha, eye_pos, max(2, int(3 * size))) # 主函数 def main(): # 创建烟花列表 fireworks = [] # 设置字体 - 使用默认字体即可显示英文 font = pygame.font.Font(None, 64) text_content = "2026 happy new year" text_surface = font.render(text_content, True, GOLD) text_rect = text_surface.get_rect(center=(WIDTH // 2, HEIGHT // 3)) # 甲骨文"马"字的位置 - 在文字下方 horse_x, horse_y = WIDTH // 2, HEIGHT // 2 + 30 horse_size = 0.4 # 甲骨文的大小缩放因子 # 记录程序开始时间 start_time = time.time() text_alpha = 0 # 文字初始透明度为0 horse_alpha = 0 # 甲骨文初始透明度为0 fade_in_start = 1.5 # 1.5秒后开始淡入 fade_in_duration = 2.0 # 淡入持续2秒 # 尝试加载背景音乐(可选) try: pygame.mixer.music.load("background_music.mp3") pygame.mixer.music.set_volume(0.5) pygame.mixer.music.play(-1) # 循环播放 except: print("无法加载背景音乐,将继续无声播放") running = True while running: # 事件处理 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False # 填充黑色背景 SCREEN.fill(BLACK) # 随机生成新烟花 - 增加生成频率 if random.random() < 0.07: fireworks.append(Firework()) # 更新和绘制所有烟花 fireworks = [fw for fw in fireworks if fw.update()] for fw in fireworks: fw.draw(SCREEN) # 计算透明度(缓慢出现效果) elapsed_time = time.time() - start_time if elapsed_time > fade_in_start: fade_progress = min((elapsed_time - fade_in_start) / fade_in_duration, 1.0) text_alpha = int(255 * fade_progress) # 从0到255逐渐增加 horse_alpha = int(255 * fade_progress) # 甲骨文与文字同步淡入 # 绘制新年祝福文字 - 带透明度效果 if text_alpha > 0: # 创建带透明度的文字表面 text_surface_with_alpha = text_surface.copy() text_surface_with_alpha.set_alpha(text_alpha) SCREEN.blit(text_surface_with_alpha, text_rect) # 绘制甲骨文"马"字 - 带透明度效果 if horse_alpha > 0: draw_oracle_horse(SCREEN, horse_x, horse_y, horse_size, horse_alpha) # 更新屏幕 pygame.display.flip() # 控制帧率 clock.tick(FPS) # 清理资源 pygame.quit() if __name__ == "__main__": main()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/1/1 4:12:12

Sunshine游戏串流3大实战技巧:打造零延迟跨设备游戏体验

Sunshine游戏串流3大实战技巧&#xff1a;打造零延迟跨设备游戏体验 【免费下载链接】Sunshine Sunshine: Sunshine是一个自托管的游戏流媒体服务器&#xff0c;支持通过Moonlight在各种设备上进行低延迟的游戏串流。 项目地址: https://gitcode.com/GitHub_Trending/su/Suns…

作者头像 李华
网站建设 2026/1/1 4:12:06

NCM音频解密宝典:让你的网易云音乐随处可播

还在为网易云音乐的NCM格式文件无法在车载音响、运动耳机或第三方播放器中播放而苦恼吗&#xff1f;今天我要分享一套超实用的音频解密方法&#xff0c;让你轻松突破平台限制&#xff0c;实现真正的音乐自由&#xff01;&#x1f3b6; 【免费下载链接】ncmdump 项目地址: ht…

作者头像 李华
网站建设 2026/1/6 23:39:26

Keil C51软件在Win10家庭版安装指南

如何在 Windows 10 家庭版上成功安装 Keil C51&#xff1f;一文搞定兼容性难题 你有没有遇到过这样的情况&#xff1a;明明下载了Keil C51的安装包&#xff0c;双击 setup.exe 却卡在一半&#xff0c;弹出“Setup has encountered an error”&#xff1b;或者安装完成后打开 μ…

作者头像 李华
网站建设 2026/1/1 4:11:26

家庭相册焕然一新:普通人也能使用的AI修复工具

家庭相册焕然一新&#xff1a;普通人也能使用的AI修复工具 在整理老抽屉时&#xff0c;翻出一张泛黄的黑白照片——祖母年轻时站在老屋门前&#xff0c;笑容温婉&#xff0c;但岁月早已模糊了她的衣裙颜色、门框油漆和天空蓝。过去&#xff0c;想还原这份记忆的真实色彩&#x…

作者头像 李华
网站建设 2026/1/1 4:11:23

手把手教程:Windows平台USB转串口驱动安装

手把手教程&#xff1a;Windows平台USB转串口驱动安装全解析 从“未知设备”说起&#xff1a;你真的会装串口驱动吗&#xff1f; 在调试STM32、烧录ESP8266固件、或者给GD32下载Bootloader时&#xff0c;是不是经常遇到这种情况—— 插上USB转串口线&#xff0c;打开设备管理…

作者头像 李华
网站建设 2026/1/1 4:11:09

3个步骤让魔兽争霸3实现180fps流畅体验:WarcraftHelper优化指南

3个步骤让魔兽争霸3实现180fps流畅体验&#xff1a;WarcraftHelper优化指南 【免费下载链接】WarcraftHelper Warcraft III Helper , support 1.20e, 1.24e, 1.26a, 1.27a, 1.27b 项目地址: https://gitcode.com/gh_mirrors/wa/WarcraftHelper 还在为《魔兽争霸3》的卡顿…

作者头像 李华