夜幕低垂,星河倾泻,我踏上寻梦之旅,只为那划破天际的流星雨。万籁俱寂中,一颗颗璀璨如钻,带着古老的誓言与未了的愿望,转瞬即逝。仰望间,心随星动,愿此刻永恒,让流星的光芒照亮心灵的每一个角落。
文章目录
一、效果展示:
不含背景图像的简单动图如下:
二、技术说明:
1. 程序特点:
创建了50颗同时存在的流星
每颗流星都有独特的:速度、轨迹角度(60-70度范围内,使轨迹略平行)、长度、粗细、亮度
视觉效果:
1)流星带有渐变的光尾效果
2)流星移动轨迹自然流畅,尾迹会随时间逐渐消失
3)使用全屏模式带来震撼效果,根据屏幕分辨率动态调整流星数量,可按esc键退出
4)当流星移出屏幕后会自动重置到屏幕上方,确保画面中始终有足够数量的流星
要运行这个程序,需要先安装 Pygame 库:pip install pygame
2. 个性化:
1) 将背景图片放在与Python脚本相同的跟目录下,修改图片名称为background.jpg
2) 如果觉得流星太多或太少,可以调整 meteor_count 计算公式中的乘数(当前是60颗)
3) Meteor 类中的各个参数:调整流星的速度、角度、长度等属性
这个程序会创建一个逼真的流星雨效果,流星会从屏幕上方划过,留下渐变的光尾,然后逐渐消失。
3.注意事项:
程序会占用整个屏幕
确保在运行程序时能够访问ESC键
如果画面卡顿,可以适当减少流星数量
如果想要更震撼的效果,可以继续调整流星的速度、大小和数量参数
三、完整代码:
import pygame
import random
import math
import os
# 初始化 Pygame
pygame.init()
# 获取屏幕信息并设置全屏
screen_info = pygame.display.Info()
WIDTH = screen_info.current_w
HEIGHT = screen_info.current_h
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
pygame.display.set_caption("流星雨")
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# 加载并调整背景图片大小
def load_background(image_path):
try:
# 加载图片
background = pygame.image.load(image_path)
# 调整图片大小以适应屏幕
background = pygame.transform.scale(background, (WIDTH, HEIGHT))
return background
except:
print(f"无法加载背景图片: {image_path}")
# 如果加载失败,创建一个深蓝色的背景
bg_surface = pygame.Surface((WIDTH, HEIGHT))
bg_surface.fill((0, 0, 40)) # 深蓝色
return bg_surface
# 设置背景图片路径(将'background.jpg'替换为你的图片路径)
background_image = load_background('background.jpg')
# 流星类
class Meteor:
def __init__(self):
# 初始化流星位置和速度
self.reset()
self.y = random.randint(-200, HEIGHT//2)
def reset(self):
# 重置流星位置和属性
self.x = random.randint(-100, WIDTH+100)
self.y = random.randint(-200, -100)
self.speed = random.uniform(7, 15)
self.angle = random.uniform(60, 70)
self.length = random.randint(30, 60)
self.thickness = random.randint(1, 4)
self.brightness = random.randint(150, 255)
def move(self):
self.x += self.speed * math.cos(math.radians(self.angle))
self.y += self.speed * math.sin(math.radians(self.angle))
if self.y > HEIGHT or self.x > WIDTH:
self.reset()
def draw(self, surface):
end_x = self.x - self.length * math.cos(math.radians(self.angle))
end_y = self.y - self.length * math.sin(math.radians(self.angle))
for i in range(3):
alpha = 255 - i * 85
color = (self.brightness, self.brightness, self.brightness, alpha)
segment_x = self.x - (i * self.length/3) * math.cos(math.radians(self.angle))
segment_y = self.y - (i * self.length/3) * math.sin(math.radians(self.angle))
next_x = self.x - ((i+1) * self.length/3) * math.cos(math.radians(self.angle))
next_y = self.y - ((i+1) * self.length/3) * math.sin(math.radians(self.angle))
pygame.draw.line(surface, color,
(segment_x, segment_y),
(next_x, next_y),
self.thickness)
# 创建半透明表面
trail_surface = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
# 根据屏幕大小调整流星数量
meteor_count = int((WIDTH * HEIGHT) / (1920 * 1080) * 60)
meteors = [Meteor() for _ in range(meteor_count)]
# 主循环
running = True
clock = pygame.time.Clock()
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
# 绘制背景图片
screen.blit(background_image, (0, 0))
# 淡化尾迹
trail_surface.fill((0, 0, 0, 5), special_flags=pygame.BLEND_RGBA_MULT)
# 更新和绘制流星
for meteor in meteors:
meteor.move()
meteor.draw(trail_surface)
# 将尾迹表面绘制到主屏幕
screen.blit(trail_surface, (0, 0))
pygame.display.flip()
clock.tick(60)
pygame.quit()
四、背景图片:
提供两张背景图片下载:
链接: https://pan.baidu.com/s/1fRXKvYw0wZRTOj0LxULaCA?pwd=namn 提取码: namn
五、结语:
自己动手试试吧!喜欢的话记得点赞关注加收藏哦!