小雷のBlog - 跨年代码
type: channel
link: https://blog.xzlei.cn/tag/%E8%B7%A8%E5%B9%B4%E4%BB%A3%E7%A0%81/
items: 2026新年快乐Python代码

2026新年快乐Python代码
type: item
date: 2026-01-01T14:09:00+08:00
description: AI摘要 本文提供了一个用Python编写的新年快乐代码，通过pygame库实现烟花效果，并在屏幕上显示“再见2025，你好2026”的字样。代码中包含了烟花类、粒子类和痕迹类，以及更新和绘制烟花的函数。运行代码前需确保已安装pygame库。 此内容根据文章生成，仅用于文章内容的解释与总结 今天是2026的第一天，新的一年，祝我们得偿所愿，平安喜乐，烟火漫漫，开心满满，八方来财，岁岁平安，万事尽可期待，一路好运常在，我们都要，越来越好！新年快乐～～下面是用Python编写的新年快乐代码：import pygame as pg import random as ra import math pg.init() pg.display.set_caption("🎇") winScreen = pg.display.Info() screenWidth = winScreen.current_w screenHeight = winScreen.current_h vector = pg.math.Vector2 trail_colors = [(45, 45, 45), (60, 60, 60), (75, 75, 75), (125, 125, 125), (150, 150, 150)] # 烟花类 class Firework: def __init__(self): # 随机生成颜色 self.colour = (ra.randint(0, 255), ra.randint(0, 255), ra.randint(0, 255)) # 随机生成三种颜色 self.colours = ( (ra.randint(0, 255), ra.randint(0, 255), ra.randint(0, 255)), (ra.randint(0, 255), ra.randint(0, 255), ra.randint(0, 255)), (ra.randint(0, 255), ra.randint(0, 255), ra.randint(0, 255)) ) # 生成一个表示发射出的火花的粒子对象 self.firework = Particle(ra.randint(0,screenWidth), screenHeight, True, self.colour) # 初始化爆炸状态为 False self.exploded = False self.particles = [] # 爆炸产生的粒子数量范围 self.min_max_particles = vector(666, 999) def update(self, win): g = vector(0, ra.uniform(0.15, 0.4)) if not self.exploded: # 给发射出的火花施加重力 self.firework.apply_force(g) self.firework.move() for tf in self.firework.trails: tf.show(win) self.show(win) if self.firework.vel.y >= 0: self.exploded = True self.explode() else: for particle in self.particles: # 给爆炸产生的粒子施加随机力 particle.apply_force(vector(g.x + ra.uniform(-1, 1) / 20, g.y / 2 + (ra.randint(1, 8) / 100))) particle.move() for t in particle.trails: t.show(win) particle.show(win) def explode(self): amount = ra.randint(int(self.min_max_particles.x), int(self.min_max_particles.y)) for i in range(amount): # 在爆炸位置生成粒子对象并添加到粒子列表中 self.particles.append(Particle(self.firework.pos.x, self.firework.pos.y, False, self.colours)) def show(self, win): # 绘制发射出的火花 pg.draw.circle(win, self.colour, (int(self.firework.pos.x), int(self.firework.pos.y)), self.firework.size) def remove(s…