2027中国马年欢庆动画效果程序(两种代码) - particles
2026中国马年欢庆动画效果程序(两种代码)
2026 年 2 月 17 日是中国农历丙午马年。新年临近了,编程爱好者,可以用程序庆贺。
用两种方式实现动画效果:用python+pygame实现、用HTML5 (Canvas + JavaScript)实现。
一、用python+pygame实现
需要安装pygame库。打开cmd命令行运行:pip install pygame
效果截图
源码
import pygame
import random
import math
# --- 初始化 ---
pygame.init()
pygame.display.set_caption("2026 丙午马年 - 金马腾飞,欢庆中国年")
# --- 屏幕 ---
WIDTH, HEIGHT = 1000, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
# --- 颜色 (保留原版配色) ---
RED_DARK = (40, 0, 0)
RED_BRIGHT = (220, 20, 60)
GOLD = (255, 215, 0)
GOLD_DARK = (200, 160, 0) # 后腿暗色
ORANGE = (255, 140, 0)
YELLOW = (255, 255, 100)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# --- 字体 ---
def get_chinese_font(size):
font_names = ['simhei', 'microsoftyahei', 'pingfangsc', 'stheiti']
for name in font_names:
try:
return pygame.font.SysFont(name, size)
except:
continue
return pygame.font.Font(None, size)
font_large = get_chinese_font(80)
font_medium = get_chinese_font(40)
font_small = get_chinese_font(24)
# --- 类定义 ---
class Particle:
""" 金色粒子 """
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
self.vx = -random.uniform(2, 5) # 向后飞
self.vy = random.uniform(-1, 1)
self.size = random.uniform(3, 6)
self.life = 255
def update(self):
self.x += self.vx
self.y += self.vy
self.life -= 8
self.size -= 0.1
def draw(self, surface):
if self.life > 0 and self.size > 0:
s = pygame.Surface((int(self.size*2), int(self.size*2)), pygame.SRCALPHA)
pygame.draw.circle(s, (*self.color, int(self.life)), (int(self.size), int(self.size)), int(self.size))
surface.blit(s, (self.x, self.y))
class Firework:
""" 原版简单线条烟花 """
def __init__(self):
self.x = random.randint(100, WIDTH - 100)
self.y = HEIGHT
self.dest_y = random.randint(100, 400)
self.color = random.choice([GOLD, ORANGE, YELLOW, (0, 255, 255), (255, 0, 255)])
self.speed = random.uniform(8, 12)
self.exploded = False
self.particles = []
def update(self):
if not self.exploded:
self.y -= self.speed
if self.y <= self.dest_y:
self.explode()
else:
for p in self.particles:
p[0] += p[2] # x
p[1] += p[3] + 0.1 # y + gravity
p[4] -= 5 # life
self.particles = [p for p in self.particles if p[4] > 0]
def explode(self):
self.exploded = True
for _ in range(50):
angle = random.uniform(0, 6.28)
speed = random.uniform(2, 5)
self.particles.append([self.x, self.y, math.cos(angle)*speed, math.sin(angle)*speed, 255])
def draw(self, surface):
if not self.exploded:
pygame.draw.line(surface, self.color, (self.x, self.y), (self.x, self.y + 10), 2)
else:
for p in self.particles:
s = pygame.Surface((4,4), pygame.SRCALPHA)
pygame.draw.circle(s, (*self.color, p[4]), (2,2), 2)
surface.blit(s, (p[0], p[1]))
class Lantern:
""" 原版灯笼 (保留不动) """
def __init__(self, x, y):
self.x_orig = x
self.y = y
self.offset = random.random() * 10
def draw(self, surface, t):
swing = math.sin(t*2 + self.offset) * 10 # 摆动幅度
x = self.x_orig + swing
# 灯笼线
pygame.draw.line(surface, (80, 20, 0), (self.x_orig, 0), (x, self.y), 2)
# 灯身
rect = pygame.Rect(x-30, self.y, 60, 50)
pygame.draw.ellipse(surface, RED_BRIGHT, rect)
# 金盖子
pygame.draw.rect(surface, GOLD, (x-15, self.y-5, 30, 6))
pygame.draw.rect(surface, GOLD, (x-15, self.y+48, 30, 6))
# 流苏
for i in range(-12, 13, 6):
# 流苏末端摆动更明显
end_swing = (x - self.x_orig) * 0.5
pygame.draw.line(surface, GOLD, (x+i, self.y+52), (x+i*1.5 + end_swing, self.y+90), 2)
# 福字
txt = font_small.render("福", True, GOLD)
surface.blit(txt, (x - txt.get_width()//2, self.y + 12))
class VividHorse:
"""
【定稿版金马】
包含:双椭圆身体 + 梯形脖子 + 几何头 + **耳朵** + 强壮四肢
"""
def __init__(self):
self.x = -150
self.y = HEIGHT // 2 + 60
self.base_y = self.y
self.scale = 0.9 # 稍微调整大小
self.speed = 5
self.timer = 0
self.particles = []
def update(self):
self.timer += 0.2
self.x += self.speed
# 奔跑起伏
self.y = self.base_y + math.sin(self.timer) * 10
# 循环播放
if self.x > WIDTH + 150:
self.x = -150
# 产生粒子
if int(self.timer * 10) % 3 == 0:
self.particles.append(Particle(self.x - 60*self.scale, self.y + 10, GOLD))
for p in self.particles:
p.update()
self.particles = [p for p in self.particles if p.life > 0]
def draw_leg(self, surface, anchor_x, anchor_y, phase, is_back_leg, color):
""" 绘制分段的腿 """
thigh_angle = math.sin(self.timer + phase) * 0.6
calf_angle = math.sin(self.timer + phase - 1.5) * 0.6 + 0.5
thigh_len = 35 * self.scale
calf_len = 30 * self.scale
knee_x = anchor_x + math.sin(thigh_angle) * thigh_len
knee_y = anchor_y + math.cos(thigh_angle) * thigh_len
hoof_x = knee_x + math.sin(thigh_angle + calf_angle) * calf_len
hoof_y = knee_y + math.cos(thigh_angle + calf_angle) * calf_len
width = int(12 * self.scale) if is_back_leg else int(10 * self.scale)
# 大腿
pygame.draw.line(surface, color, (anchor_x, anchor_y), (knee_x, knee_y), width)
# 小腿
pygame.draw.line(surface, color, (knee_x, knee_y), (hoof_x, hoof_y), int(6 * self.scale))
def draw(self, surface):
for p in self.particles:
p.draw(surface)
tx = lambda v: self.x + v * self.scale
ty = lambda v: self.y + v * self.scale
# 1. 后侧腿 (暗色)
self.draw_leg(surface, tx(-30), ty(25), 0, True, GOLD_DARK)
self.draw_leg(surface, tx(30), ty(30), 3.14, False, GOLD_DARK)
# 2. 身体 (双椭圆结构)
pygame.draw.ellipse(surface, GOLD, (tx(-50), ty(0), 50*self.scale, 45*self.scale)) # 臀
pygame.draw.ellipse(surface, GOLD, (tx(0), ty(-5), 55*self.scale, 50*self.scale)) # 胸
pygame.draw.rect(surface, GOLD, (tx(-30), ty(5), 40*self.scale, 35*self.scale)) # 连接
# 3. 脖子 (梯形)
neck_points = [
(tx(15), ty(5)),
(tx(25), ty(-10)),
(tx(45), ty(-50)),
(tx(30), ty(-40)) 拼多多帮助力入口
]
pygame.draw.polygon(surface, GOLD, neck_points)
# 4. 头部 (多边形) - 恢复上一版的造型
head_points = [
(tx(43), ty(-52)),
(tx(60), ty(-45)), # 鼻尖
(tx(55), ty(-35)),
(tx(40), ty(-42))
]
pygame.draw.polygon(surface, GOLD, head_points)
# 5. 耳朵 (恢复之前漏掉的代码!)
# 在头顶画一个向上的小三角形
ear_points = [
(tx(42), ty(-52)), # 根部后
(tx(45), ty(-62)), # 耳尖 (高一点)
(tx(48), ty(-50)) # 根部前
]
pygame.draw.polygon(surface, GOLD, ear_points)
# 眼睛
pygame.draw.circle(surface, BLACK, (int(tx(50)), int(ty(-45))), 2)
# 鬃毛
for i in range(4):
offset_x = i * 4
offset_y = i * 3
pygame.draw.line(surface, ORANGE, (tx(26+offset_x), ty(-12-offset_y)), (tx(10+offset_x), ty(-8-offset_y)), 3)
# 尾巴
tail_start = (tx(-45), ty(5))
tail_tip = (tx(-80), ty(10 + math.sin(self.timer)*5))
pygame.draw.line(surface, GOLD, tail_start, tail_tip, int(5*self.scale))
# 6. 前侧腿 (亮色)
self.draw_leg(surface, tx(-30), ty(25), 0.5, True, GOLD)
self.draw_leg(surface, tx(30), ty(30), 3.14 + 0.5, False, GOLD)
# --- 主程序 (原版界面) ---
def main():
running = True
# 原版灯笼生成
lanterns = []
for x in range(100, WIDTH, 150):
lanterns.append(Lantern(x, random.randint(-15, 5)))
fireworks = []
horse = VividHorse()
while running:
t = pygame.time.get_ticks() / 1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
fireworks.append(Firework())
if random.random() < 0.03:
fireworks.append(Firework())
# --- 渲染 ---
# 1. 原版红黑背景
for y in range(HEIGHT):
shade = int(RED_DARK[0] + (RED_BRIGHT[0]-RED_DARK[0]) * (y/HEIGHT) * 0.3)
pygame.draw.line(screen, (shade, 0, 0), (0, y), (WIDTH, y))
# 2. 原版文字
title = font_large.render("2026 丙午马年", True, GOLD)
shadow = font_large.render("2026 丙午马年", True, (50, 0, 0))
screen.blit(shadow, (WIDTH//2 - title.get_width()//2 + 4, HEIGHT//2 - 100 + 4))
screen.blit(title, (WIDTH//2 - title.get_width()//2, HEIGHT//2 - 100))
sub = font_medium.render("金马腾飞 · 万事如意", True, (255, 230, 200))
screen.blit(sub, (WIDTH//2 - sub.get_width()//2, HEIGHT//2 - 20))
# 3. 烟花
for fw in fireworks:
fw.update()
fw.draw(screen)
fireworks = [f for f in fireworks if not f.exploded or len(f.particles)>0]
# 4. 马
horse.update()
horse.draw(screen)
# 5. 灯笼
for l in lanterns:
l.draw(screen, t)
pygame.display.flip()
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
main()
二、用HTML5 (Canvas + JavaScript)实现
不用安装运行环境,直接用浏览器就能打开。
效果截图
源码:
2026 丙午马年 - 骏马奔腾,新春大吉