# Python 高级编程:2026 丙午马年元旦祝福程序
# 主题:烟花 + 动态文字 + 祝福动画 + 音效提示(可选) + 烟火粒子系统
# 运行环境:Python 3.8+ + pygame(pip install pygame)
import pygame
import sys
import random
import math
import time
from datetime import datetime
# 初始化 Pygame
pygame.init()
pygame.mixer.init() # 如果想加音效
# 屏幕设置
WIDTH, HEIGHT = 1280, 720
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("2026 丙午马年 元旦快乐!")
clock = pygame.time.Clock()
# 颜色定义(马年火元素 + 节日喜庆)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 50, 50)
GOLD = (255, 215, 0)
ORANGE = (255, 140, 0)
PURPLE = (180, 100, 255)
CYAN = (0, 255, 255)
# 字体
font_title = pygame.font.SysFont('simhei', 80, bold=True) # 中文黑体
font_sub = pygame.font.SysFont('simhei', 50)
font_small = pygame.font.SysFont('simhei', 32)
# 粒子类(烟花爆炸效果)
class Particle:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
self.angle = random.uniform(0, math.tau)
self.speed = random.uniform(2, 8)
self.size = random.randint(3, 8)
self.lifetime = random.randint(40, 90)
self.gravity = 0.08
self.alpha = 255
def update(self):
self.x += math.cos(self.angle) * self.speed
self.y += math.sin(self.angle) * self.speed + self.gravity * (90 - self.lifetime)
self.lifetime -= 1
self.alpha = int(255 * (self.lifetime / 90)) if self.lifetime > 0 else 0
def draw(self, surface):
if self.lifetime > 0:
s = pygame.Surface((self.size * 2, self.size * 2), pygame.SRCALPHA)
pygame.draw.circle(s, (*self.color, self.alpha), (self.size, self.size), self.size)
surface.blit(s, (int(self.x - self.size), int(self.y - self.size)))
# 烟花类
class Firework:
def __init__(self):
self.x = random.randint(100, WIDTH - 100)
self.y = HEIGHT
self.target_y = random.randint(100, HEIGHT // 3)
self.color = random.choice([RED, GOLD, ORANGE, PURPLE, CYAN])
self.speed = random.uniform(8, 15)
self.exploded = False
self.particles = []
def update(self):
if not self.exploded:
self.y -= self.speed
if self.y <= self.target_y:
self.exploded = True
for _ in range(120):
self.particles.append(Particle(self.x, self.y, self.color))
else:
for p in self.particles[:]:
p.update()
if p.lifetime <= 0:
self.particles.remove(p)
def draw(self, surface):
if not self.exploded:
# 上升尾迹
pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), 6)
pygame.draw.circle(surface, WHITE, (int(self.x), int(self.y)), 3)
else:
for p in self.particles:
p.draw(surface)
# 文字动画类
class TextAnimation:
def __init__(self, text, font, color, x, y, duration=3000):
self.text = text
self.font = font
self.color = color
self.x = x
self.y = y
self.start_time = pygame.time.get_ticks()
self.duration = duration
self.alpha = 0
def update(self):
elapsed = pygame.time.get_ticks() - self.start_time
if elapsed < self.duration // 3:
self.alpha = int(255 * (elapsed / (self.duration // 3)))
elif elapsed > self.duration * 2 // 3:
self.alpha = int(255 * (1 - (elapsed - self.duration * 2 // 3) / (self.duration // 3)))
else:
self.alpha = 255
def draw(self, surface):
if self.alpha > 0:
surf = self.font.render(self.text, True, self.color)
surf.set_alpha(self.alpha)
surface.blit(surf, (self.x - surf.get_width()//2, self.y - surf.get_height()//2))
# 主祝福内容(可自定义)
blessings = [
"2026 丙午马年 元旦快乐!",
"马到成功 骏业日新",
"火马奔腾 热情似火",
"新年新气象 心想事成",
"健康平安 财源广进",
"家庭和睦 爱情甜蜜",
"学业有成 事业腾飞",
"岁岁平安 年年有余",
"愿你2026 如马般自由奔放!"
]
# 初始化
fireworks = []
texts = []
current_blessing_idx = 0
next_text_time = pygame.time.get_ticks() + 2000
# 主循环
running = True
pygame.mouse.set_visible(False) # 隐藏鼠标更沉浸
while running:
dt = clock.tick(60)
current_time = pygame.time.get_ticks()
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((8, 8, 30))
# 随机发射烟花(每隔0.8~2秒一朵)
if random.random() < 0.03:
fireworks.append(Firework())
# 更新 & 绘制烟花
for fw in fireworks[:]:
fw.update()
fw.draw(screen)
if fw.exploded and not fw.particles:
fireworks.remove(fw)
# 祝福文字轮播动画
if current_time > next_text_time:
if texts:
texts.pop(0) # 移除旧的
text = blessings[current_blessing_idx % len(blessings)]
anim = TextAnimation(
text,
font_title if "丙午马年" in text else font_sub,
random.choice([GOLD, RED, ORANGE, CYAN]),
WIDTH // 2,
HEIGHT // 2 + random.randint(-80, 80),
duration=5000
)
texts.append(anim)
current_blessing_idx += 1
next_text_time = current_time + random.randint(4000, 7000)
for t in texts:
t.update()
t.draw(screen)
# 小祝福 + 时间显示
now = datetime.now()
time_str = now.strftime("%Y年%m月%d日 %H:%M:%S")
time_surf = font_small.render(f"Las Vegas 时间:{time_str} 2026元旦快乐!", True, WHITE)
screen.blit(time_surf, (20, HEIGHT - 40))
horse_tip = font_small.render("丙午马年 · 火马奔腾 · 热情似火 · 马到成功", True, (255, 180, 100))
screen.blit(horse_tip, (WIDTH - horse_tip.get_width() - 20, HEIGHT - 40))
pygame.display.flip()
pygame.quit()
sys.exit()
程序亮点(高级编程技巧展示)
- 粒子系统:真实烟花爆炸物理(速度+重力+衰减+alpha渐隐)
- 动画文字:淡入淡出 + 随机位置/颜色/间隔
- 事件驱动:键盘ESC退出 + 随机烟花发射
- 中文字体:使用
simhei(Windows自带黑体)完美显示中文 - 时间实时:结合
datetime显示当前时间(拉斯维加斯时区) - 祝福轮播:内容可无限扩展,自定义你的专属祝福
快速运行方式
- 确保安装 pygame:
pip install pygame
- 复制代码保存为
happy_new_year_2026.py - 执行:
python happy_new_year_2026.py
- 按 ESC 优雅退出
2026丙午马年元旦快乐!
愿你马到成功,热情似火,奔腾向前!
在新的一年里,一切顺利,心想事成~ 🐎🔥✨