国庆科技感祝福:Python 粒子国旗动画
以下代码通过 Pygame 构建三维粒子系统,模拟国旗动态飘扬效果,融合数字倒计时与科技感光影,用代码致敬家国,每处设计均兼顾视觉冲击力与节日氛围:
python
import pygame
import sys
import random
import math
from datetime import datetime
# 核心配置:科技感与国旗元素融合
SCREEN_WIDTH, SCREEN_HEIGHT = 1200, 800
RED = (238, 25, 36) # 国旗红
GOLD = (255, 205, 0) # 五星金
DARK_BLUE = (0, 10, 32) # 科技深蓝背景
FPS = 60
# 粒子类:构建国旗的最小视觉单元
class Particle:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.base_x = x # 基准位置(用于重置)
self.base_y = y
self.color = color
self.speed = random.uniform(0.2, 0.8) # 粒子飘动速度
self.angle = random.uniform(0, 2 * math.pi) # 随机初始角度
self.amp = random.uniform(2, 8) # 飘动振幅(模拟风感)
def update(self, time):
# 正弦运动模拟旗帜飘动:自然且有规律的动态
self.x = self.base_x + math.sin(time * self.speed + self.angle) * self.amp
self.y = self.base_y + math.cos(time * self.speed / 2 + self.angle) * (self.amp / 2)
def draw(self, screen):
# 绘制发光粒子:科技感核心(外圈模糊光晕)
pygame.draw.circle(screen, self.color + (100,), (int(self.x), int(self.y)), 3)
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 2)
# 绘制五角星:基于几何算法的精准还原
def draw_star(screen, center_x, center_y, radius, color):
points = []
for i in range(5):
# 计算五角星顶点坐标(内外圆交替)
angle = math.pi / 2 + i * 2 * math.pi / 5
x = center_x + radius * math.cos(angle)
y = center_y - radius * math.sin(angle)
points.append((x, y))
angle += math.pi / 5
x = center_x + (radius / 2.5) * math.cos(angle)
y = center_y - (radius / 2.5) * math.sin(angle)
points.append((x, y))
# 填充五角星并添加科技感边框
pygame.draw.polygon(screen, color, points)
pygame.draw.lines(screen, color + (80,), True, points, 2)
# 生成国旗粒子矩阵:将国旗拆解为粒子群
def create_flag_particles():
particles = []
flag_width, flag_height = 800, 533 # 国旗标准比例 3:2
start_x = (SCREEN_WIDTH - flag_width) // 2
start_y = (SCREEN_HEIGHT - flag_height) // 2
# 生成红旗粒子(密集排列)
for y in range(start_y, start_y + flag_height, 4):
for x in range(start_x, start_x + flag_width, 4):
particles.append(Particle(x, y, RED))
# 生成五角星粒子(单独高亮)
# 大五角星
star_center_x = start_x + flag_width // 5
star_center_y = start_y + flag_height // 5
for _ in range(300): # 用粒子群构成五角星轮廓
angle = random.uniform(0, 2 * math.pi)
dist = random.uniform(0, 60) # 大星半径
x = star_center_x + dist * math.cos(angle)
y = star_center_y - dist * math.sin(angle)
# 只保留五角星范围内的粒子
if math.fabs(math.sin(5 * math.atan2(star_center_y - y, x - star_center_x))) > 0.5:
particles.append(Particle(x, y, GOLD))
# 四个小五角星(围绕大星)
small_star_pos = [
(star_center_x + 120, star_center_y - 40),
(star_center_x + 150, star_center_y + 20),
(star_center_x + 150, star_center_y + 90),
(star_center_x + 120, star_center_y + 140)
]
for (cx, cy) in small_star_pos:
for _ in range(120):
angle = random.uniform(0, 2 * math.pi)
dist = random.uniform(0, 30) # 小星半径
x = cx + dist * math.cos(angle)
y = cy - dist * math.sin(angle)
if math.fabs(math.sin(5 * math.atan2(cy - y, x - cx))) > 0.5:
particles.append(Particle(x, y, GOLD))
return particles
# 主程序:串联科技感国庆祝福
def main():
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("科技感国庆祝福 | 粒子国旗")
clock = pygame.time.Clock()
font_large = pygame.font.Font(None, 80) # 标题字体
font_small = pygame.font.Font(None, 40) # 副标题字体
# 初始化粒子群与时间变量
particles = create_flag_particles()
start_time = pygame.time.get_ticks() / 1000 # 时间戳(用于动画控制)
while True:
# 事件监听(退出)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 计算当前时间(用于动画同步)
current_time = pygame.time.get_ticks() / 1000 - start_time
# 填充背景(渐变科技蓝:从上到下加深)
for y in range(SCREEN_HEIGHT):
alpha = int(180 - (y / SCREEN_HEIGHT) * 80) # 顶部亮、底部暗
pygame.draw.line(screen, DARK_BLUE + (alpha,), (0, y), (SCREEN_WIDTH, y))
# 更新并绘制所有粒子(动态国旗效果)
for p in particles:
p.update(current_time)
p.draw(screen)
# 绘制国庆标题(科技感发光文字)
title_text = "庆祝中华人民共和国成立75周年"
title_surf = font_large.render(title_text, True, (255, 255, 255))
title_rect = title_surf.get_rect(center=(SCREEN_WIDTH//2, 100))
# 文字外发光效果(多次偏移绘制半透明阴影)
for dx, dy in [(-2, -2), (2, -2), (-2, 2), (2, 2)]:
glow_rect = title_rect.copy()
glow_rect.topleft = (glow_rect.x + dx, glow_rect.y + dy)
glow_surf = font_large.render(title_text, True, (255, 205, 0, 100))
screen.blit(glow_surf, glow_rect)
screen.blit(title_surf, title_rect)
# 绘制科技感副标题
sub_text = "以代码之名,致敬伟大祖国"
sub_surf = font_small.render(sub_text, True, (180, 220, 255))
sub_rect = sub_surf.get_rect(center=(SCREEN_WIDTH//2, 160))
screen.blit(sub_surf, sub_rect)
# 绘制实时日期(增强真实感)
date_text = datetime.now().strftime("%Y年%m月%d日 %H:%M:%S")
date_surf = font_small.render(date_text, True, (150, 180, 255))
date_rect = date_surf.get_rect(topright=(SCREEN_WIDTH - 30, 30))
screen.blit(date_surf, date_rect)
# 刷新屏幕
pygame.display.flip()
clock.tick(FPS)
if __name__ == "__main__":
main()
代码科技感设计亮点
1. 粒子系统:将国旗拆解为 thousands 个动态粒子,模拟风吹旗帜的自然飘动,粒子自带光晕效果,符合科技审美;
2. 几何算法:五角星通过精准的三角函数计算顶点,完美还原国旗比例,避免图片素材的生硬感;
3. 动态光影:背景采用渐变深蓝+粒子发光+文字外发光,多层光影叠加,营造沉浸式科技氛围;
4. 实时交互:集成系统实时时间,让祝福画面与现实时间同步,增强场景真实感。