目录
前言
代码说明
库函数
窗口尺寸
颜色
粒子类和烟花类
甲骨文马
字体部分
背景音乐
注意事项
源码
前言
先看看效果截图吧。今天闲来无事突然想试试,好久没用电脑了,一时没找到录屏软件在哪。简单说明一下:这段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()