Python趣学篇:用Pygame打造绚烂流星雨动画

发布于:2025-06-03 ⋅ 阅读:(25) ⋅ 点赞:(0)

名人说:路漫漫其修远兮,吾将上下而求索。—— 屈原《离骚》
创作者:Code_流苏(CSDN)(一个喜欢古诗词和编程的Coder😊)
专栏介绍《Python星球日记》


欢迎大家来到Python星球日记的趣学篇,在趣学篇,我们将带来很多有趣的适合初学者的项目,项目均由个人团队开发及AI vide coding的辅助…

🌟 想要在电脑屏幕上重现那种令人心醉的流星雨美景吗?今天我们就用Python从零开始,打造一个炫酷的流星雨动画!不仅有逼真的物理效果,还能显示中文字符,让你在编程的同时感受浪漫的星空之美。

一、项目简介与效果展示

这是一个基于Pygame开发的流星雨动画项目,具有以下特效:

🌠 多彩流星:8种不同颜色的流星随机生成,每颗都有独特的轨迹
粒子尾迹:每个流星都带有动态的粒子拖尾效果,增强视觉冲击力
🌟 闪烁星空:150颗背景星星营造真实的夜空氛围
🌌 渐变背景:深蓝到黑色的自然渐变,模拟夜空色彩
💫 斜向轨迹:所有流星都以30-60度角自然斜向下运动
🎨 中文显示:完美支持中文字体,显示"斜向流星雨来啦!"等文字

预期效果:

在这里插入图片描述

实际效果:

在这里插入图片描述

整个程序运行流畅,达到60FPS的丝滑体验,让你仿佛置身于真实的流星雨之中!

二、技术栈与核心概念

1. 主要技术栈

在这里插入图片描述

  • Pygame:Python游戏开发库,负责图形渲染和事件处理
  • 数学库(math):用于三角函数计算,实现精确的运动轨迹
  • 随机库(random):生成随机的流星属性和位置

2. 核心概念解析

粒子系统:这是游戏特效中的经典概念。想象一下,每颗流星就像一个"粒子发射器",不断产生小粒子形成美丽的尾迹。

Alpha混合:通过调整透明度实现渐变效果,让流星尾迹从亮到暗自然过渡。

面向对象设计:将流星、星星、特殊流星都封装成独立的类,代码结构清晰易维护。

三、关键实现原理

1. 流星运动轨迹

真实的流星雨并不是垂直下落,而是以一定角度斜向划过天空。我们使用三角函数来实现这种自然的运动效果:

# 设置流星的斜向运动
angle = random.uniform(math.radians(30), math.radians(60))  # 30-60度角
speed = random.uniform(12, 20)
self.speed_x = -speed * math.cos(angle)  # 水平分量
self.speed_y = speed * math.sin(angle)   # 垂直分量

这样计算出来的速度分量,让每颗流星都能以不同但合理的角度斜向飞行,营造出逼真的流星雨效果。

2. 粒子系统实现

每颗流星都会不断产生小粒子,形成绚烂的尾迹:

# 为流星添加尾迹粒子
for i in range(4):
    particle = {
        'x': self.x + random.randint(-8, 8),
        'y': self.y + random.randint(-8, 8), 
        'life': random.randint(25, 40),      # 粒子生命周期
        'color': self.color,
        'size': random.randint(1, 4)
    }
    self.tail_particles.append(particle)

每个粒子都有自己的生命周期,随着时间推移逐渐变暗消失,这就是我们看到的流星尾迹渐变效果。

在这里插入图片描述

3. 中文字体显示

程序智能适配多种操作系统的中文字体:

font_paths = [
    "C:/Windows/Fonts/simhei.ttf",      # Windows黑体
    "C:/Windows/Fonts/msyh.ttc",        # 微软雅黑  
    "/System/Library/Fonts/PingFang.ttc", # macOS苹方
    "/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf" # Linux
]

程序会自动尝试加载这些字体,确保在不同系统上都能正确显示中文。

四、完整代码及分析

import pygame
import random
import math
import sys

# 初始化pygame
pygame.init()

# 设置屏幕尺寸
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 800
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("炫酷斜向流星雨动画 - 按ESC退出")

# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (100, 150, 255)
CYAN = (0, 255, 255)
YELLOW = (255, 255, 0)
PINK = (255, 100, 150)
PURPLE = (150, 100, 255)
GREEN = (100, 255, 150)
ORANGE = (255, 150, 50)

class Meteor:
    def __init__(self):
        self.reset()
        
    def reset(self):
        self.x = random.randint(-100, SCREEN_WIDTH + 200)
        self.y = random.randint(-800, -50)
        # 设置更明显的斜向下运动 - 角度大约在30-60度之间
        angle = random.uniform(math.radians(30), math.radians(60))  # 30-60度角
        speed = random.uniform(12, 20)  # 总速度
        self.speed_x = -speed * math.cos(angle)  # 向左下
        self.speed_y = speed * math.sin(angle)   # 向下
        self.length = random.randint(60, 200)
        self.width = random.randint(2, 6)
        self.color = random.choice([BLUE, CYAN, WHITE, YELLOW, PINK, PURPLE, GREEN, ORANGE])
        self.tail_particles = []
        self.life = 255
        
    def update(self):
        # 更新位置
        self.x += self.speed_x
        self.y += self.speed_y
        
        # 添加尾迹粒子
        for i in range(4):
            particle = {
                'x': self.x + random.randint(-8, 8),
                'y': self.y + random.randint(-8, 8),
                'life': random.randint(25, 40),
                'color': self.color,
                'size': random.randint(1, 4)
            }
            self.tail_particles.append(particle)
        
        # 更新尾迹粒子
        for particle in self.tail_particles[:]:
            particle['life'] -= 1.5
            particle['x'] += random.uniform(-0.5, 0.5)
            particle['y'] += random.uniform(-0.5, 0.5)
            if particle['life'] <= 0:
                self.tail_particles.remove(particle)
        
        # 如果流星离开屏幕,重置位置
        if self.y > SCREEN_HEIGHT + 100 or self.x < -200:
            self.reset()
    
    def draw(self, surface):
        # 绘制尾迹粒子
        for particle in self.tail_particles:
            alpha = max(0, particle['life'] / 40.0)
            size = max(1, int(particle['size'] * alpha))
            color = [min(255, int(c * alpha * 1.2)) for c in particle['color']]
            
            # 创建带透明度的粒子表面
            particle_surface = pygame.Surface((size * 2, size * 2), pygame.SRCALPHA)
            pygame.draw.circle(particle_surface, (*color, int(255 * alpha)), 
                             (size, size), size)
            surface.blit(particle_surface, 
                        (int(particle['x'] - size), int(particle['y'] - size)))
        
        # 绘制流星主体 - 创建沿运动方向的渐变效果
        segments = min(self.length, 50)
        for i in range(segments):
            progress = i / segments
            alpha = 1 - progress
            
            # 沿着流星运动方向绘制尾迹
            current_x = self.x - (self.speed_x * progress * 3)  # 调整尾迹长度
            current_y = self.y - (self.speed_y * progress * 3)
            
            if 0 <= current_x <= SCREEN_WIDTH and 0 <= current_y <= SCREEN_HEIGHT:
                # 计算当前片段的颜色和大小
                color = [min(255, int(c * alpha * 1.3)) for c in self.color]
                width = max(1, int(self.width * alpha))
                
                # 创建带透明度的流星片段
                meteor_surface = pygame.Surface((width * 2, width * 2), pygame.SRCALPHA)
                pygame.draw.circle(meteor_surface, (*color, int(255 * alpha)), 
                                 (width, width), width)
                surface.blit(meteor_surface, 
                           (int(current_x - width), int(current_y - width)))

class Star:
    def __init__(self):
        self.x = random.randint(0, SCREEN_WIDTH)
        self.y = random.randint(0, SCREEN_HEIGHT)
        self.brightness = random.randint(80, 255)
        self.twinkle_speed = random.uniform(0.02, 0.08)
        self.twinkle_phase = random.uniform(0, 2 * math.pi)
        self.size = random.choice([1, 1, 1, 2])  # 大部分是小星星
        
    def update(self):
        self.twinkle_phase += self.twinkle_speed
        
    def draw(self, surface):
        current_brightness = int(self.brightness * (0.3 + 0.7 * abs(math.sin(self.twinkle_phase))))
        color = (current_brightness, current_brightness, min(255, current_brightness + 20))
        pygame.draw.circle(surface, color, (int(self.x), int(self.y)), self.size)

class ShootingStar:
    def __init__(self):
        self.reset()
    
    def reset(self):
        self.x = random.randint(-100, SCREEN_WIDTH + 100)
        self.y = random.randint(-300, SCREEN_HEIGHT//3)
        # 让特殊流星也是斜向下运动,但角度稍有不同
        self.angle = random.uniform(math.radians(25), math.radians(65))  # 25-65度角
        self.speed = random.randint(18, 28)
        self.length = random.randint(100, 150)
        self.life = random.randint(60, 100)
        self.max_life = self.life
        self.color = random.choice([WHITE, YELLOW, CYAN])
        
    def update(self):
        # 按照设定角度斜向下移动
        self.x += -self.speed * math.cos(self.angle)  # 向左
        self.y += self.speed * math.sin(self.angle)   # 向下
        self.life -= 1
        
        if self.life <= 0 or self.x < -100 or self.x > SCREEN_WIDTH + 100 or self.y > SCREEN_HEIGHT + 100:
            self.reset()
    
    def draw(self, surface):
        alpha = self.life / self.max_life
        for i in range(20):
            progress = i / 20
            # 沿着运动方向绘制尾迹
            trail_x = self.x + self.speed * math.cos(self.angle) * progress * self.length / 20
            trail_y = self.y - self.speed * math.sin(self.angle) * progress * self.length / 20
            trail_alpha = alpha * (1 - progress)
            
            if -50 <= trail_x <= SCREEN_WIDTH + 50 and -50 <= trail_y <= SCREEN_HEIGHT + 50:
                color = [int(c * trail_alpha) for c in self.color]
                size = max(1, int(3 * trail_alpha))
                trail_surface = pygame.Surface((size * 2, size * 2), pygame.SRCALPHA)
                pygame.draw.circle(trail_surface, (*color, int(255 * trail_alpha)), 
                                 (size, size), size)
                surface.blit(trail_surface, (int(trail_x - size), int(trail_y - size)))

def create_gradient_background():
    background = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
    for y in range(SCREEN_HEIGHT):
        # 创建从深蓝到黑色的渐变
        progress = y / SCREEN_HEIGHT
        r = int(5 * (1 - progress))
        g = int(10 * (1 - progress))
        b = int(30 * (1 - progress))
        color = (r, g, b)
        pygame.draw.line(background, color, (0, y), (SCREEN_WIDTH, y))
    return background

def main():
    clock = pygame.time.Clock()
    
    # 创建流星列表
    meteors = [Meteor() for _ in range(20)]
    
    # 创建星星背景
    stars = [Star() for _ in range(150)]
    
    # 创建特殊流星
    shooting_stars = [ShootingStar() for _ in range(3)]
    
    # 设置中文字体 - 尝试多种字体路径
    font = None
    font_paths = [
        "simhei.ttf",
        "C:/Windows/Fonts/simhei.ttf",
        "C:/Windows/Fonts/msyh.ttc",
        "C:/Windows/Fonts/simsun.ttc",
        "/System/Library/Fonts/PingFang.ttc",  # macOS
        "/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf",  # Linux
    ]
    
    for font_path in font_paths:
        try:
            font = pygame.font.Font(font_path, 60)
            break
        except:
            continue
    
    if font is None:
        # 如果找不到中文字体,使用系统默认字体
        font = pygame.font.Font(None, 60)
    
    # 创建多行文字
    texts = ["流星雨来啦!", "许个愿吧~"]
    text_surfaces = []
    for i, text in enumerate(texts):
        try:
            text_surface = font.render(text, True, WHITE)
        except:
            # 如果渲染中文失败,使用英文
            text_surface = font.render("Meteor Shower!" if i == 0 else "Make a wish~", True, WHITE)
        text_surfaces.append(text_surface)
    
    # 创建渐变背景
    background = create_gradient_background()
    
    running = True
    frame_count = 0
    
    print("流星雨动画已启动!")
    print("操作说明:")
    print("- 按 ESC 键退出")
    print("- 按空格键添加特殊斜向流星")
    print("- 享受这场飞舞的绚烂流星雨吧!")
    
    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
                elif event.key == pygame.K_SPACE:
                    # 添加特殊流星 - 也是斜向下运动
                    special_meteor = Meteor()
                    special_meteor.length = 300
                    special_meteor.width = 10
                    special_meteor.color = WHITE
                    # 重新设置运动方向确保是斜向下
                    angle = random.uniform(math.radians(35), math.radians(55))
                    speed = random.uniform(15, 25)
                    special_meteor.speed_x = -speed * math.cos(angle)
                    special_meteor.speed_y = speed * math.sin(angle)
                    meteors.append(special_meteor)
        
        frame_count += 1
        
        # 绘制背景
        screen.blit(background, (0, 0))
        
        # 更新和绘制星星
        for star in stars:
            star.update()
            star.draw(screen)
        
        # 更新和绘制特殊流星
        for shooting_star in shooting_stars:
            shooting_star.update()
            shooting_star.draw(screen)
        
        # 更新和绘制普通流星
        for meteor in meteors:
            meteor.update()
            meteor.draw(screen)
        
        # 绘制标题文字,添加浮动和闪烁效果
        for i, text_surface in enumerate(text_surfaces):
            alpha = int(200 + 55 * math.sin(frame_count * 0.05 + i))
            y_offset = int(10 * math.sin(frame_count * 0.03 + i))
            
            text_with_alpha = text_surface.copy()
            text_with_alpha.set_alpha(alpha)
            
            text_rect = text_surface.get_rect(center=(SCREEN_WIDTH // 2, 120 + i * 80 + y_offset))
            screen.blit(text_with_alpha, text_rect)
        
        # 定期添加随机流星
        if frame_count % 90 == 0:  # 每1.5秒
            new_meteor = Meteor()
            meteors.append(new_meteor)
        
        # 偶尔添加超级流星
        if frame_count % 300 == 0:  # 每5秒
            super_meteor = Meteor()
            super_meteor.length = 400
            super_meteor.width = 12
            super_meteor.color = random.choice([WHITE, YELLOW, CYAN])
            # 确保超级流星也是斜向下运动
            angle = random.uniform(math.radians(40), math.radians(50))
            speed = random.uniform(18, 25)
            super_meteor.speed_x = -speed * math.cos(angle)
            super_meteor.speed_y = speed * math.sin(angle)
            meteors.append(super_meteor)
        
        # 限制流星数量以保持性能
        if len(meteors) > 35:
            meteors.pop(0)
        
        # 添加一些随机的星星
        if random.randint(1, 100) < 2:
            stars.append(Star())
        if len(stars) > 200:
            stars.pop(0)
        
        pygame.display.flip()
        clock.tick(60)  # 60 FPS
    
    pygame.quit()
    sys.exit()

if __name__ == "__main__":
    main()

在这里插入图片描述

整个项目采用面向对象的设计模式,主要包含以下几个类:

Meteor:普通流星

  • reset():重置流星位置和属性
  • update():更新位置和粒子效果
  • draw():绘制流星和尾迹

Star:背景星星

  • 实现闪烁效果,营造夜空氛围

ShootingStar:特殊流星

  • 更大更亮,出现频率较低

主程序循环

  1. 事件处理(键盘输入)
  2. 更新所有对象状态
  3. 绘制所有图形元素
  4. 控制帧率(60FPS)

五、运行方法与操作指南

1. 环境准备

# 安装Pygame库
pip install pygame

2. 运行程序

python meteor_shower.py

3. 交互操作

  • ESC键:退出程序
  • 空格键:手动添加一颗特殊流星
  • 程序会自动生成各种流星效果,无需其他操作

在这里插入图片描述

4. 性能说明

  • 支持1200×800分辨率
  • 稳定60FPS运行
  • 自动优化粒子数量,保证流畅性

六、学习价值与扩展思路

1. 学习价值

这个项目非常适合Python初学者,能够学习到:

  • Pygame基础:图形绘制、事件处理、游戏循环
  • 数学应用:三角函数在编程中的实际运用
  • 面向对象编程:类的设计和使用
  • 算法优化:如何平衡视觉效果与性能

2. 扩展思路

想要让你的流星雨更加炫酷?可以尝试这些扩展:

🎵 音效系统:添加背景音乐和流星划过的音效
🎮 交互玩法:让用户点击流星获得分数
🌈 更多特效:爆炸效果、彗星、UFO等
📱 移动适配:改造成手机应用
🤖 AI元素:让流星根据音乐节拍变化

七、总结

通过这个流星雨项目,我们不仅创造了一个美丽的视觉效果,更重要的是学会了如何将数学知识编程技巧相结合。从简单的三角函数到复杂的粒子系统,每一行代码都在为最终的视觉盛宴贡献力量。

编程的魅力就在于此——我们可以用代码重现自然之美,用算法诠释浪漫情怀。无论你是编程新手还是资深开发者,都能从这样的项目中获得乐趣和成长。

快来运行这个程序,在你的屏幕上点亮一场专属的数字流星雨吧!✨

💡 小贴士:如果你对这个项目感兴趣,建议动手实践一下。编程最好的学习方式就是边写边学,在调试和优化的过程中,你会发现更多有趣的细节和改进空间!

让我们一起用代码让世界变得更有趣!🎉

创作者:Code_流苏(CSDN)(一个喜欢古诗词和编程的Coder😊)


网站公告

今日签到

点亮在社区的每一天
去签到