pygame粒子效果:ILOVEYOU!
pygame粒子效果:ILOVEYOU!import pygame
import sys
import math
import random
# 初始化Pygame
pygame.init()
# 屏幕设置
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
pygame.display.set_caption("pygame粒子效果: I LOVE YOU!")
# 颜色定义
BACKGROUND = (10, 10, 20)
RED = (255, 50, 50)
PARTICLE_COLORS = [
(255, 50, 50),# 红色
(255, 100, 100),# 浅红
(255, 150, 150),# 粉红
(255, 200, 200)# 淡粉
]
# 字体设置
font = pygame.font.SysFont(None, 180)
text = font.render("I LOVE YOU!", True, RED)
text_rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
# 粒子类
class Particle:
def __init__(self, x, y, target_x, target_y):
self.x = x
self.y = y
self.target_x = target_x
self.target_y = target_y
self.size = 2
self.color = random.choice(PARTICLE_COLORS)
self.speed = random.uniform(2, 3)
self.arrived = False
def move(self):
if not self.arrived:
dx = self.target_x - self.x
dy = self.target_y - self.y
distance = math.sqrt(dx * dx + dy * dy)
if distance < 2:
self.arrived = True
return
dx /= distance
dy /= distance
self.x += dx * self.speed
self.y += dy * self.speed
def draw(self, surface):
pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), self.size)
# 获取文字像素点
def get_text_pixels():
pixels = []
mask = pygame.mask.from_surface(text)
# 获取整个文本的边界框
for x in range(text_rect.left, text_rect.right):
for y in range(text_rect.top, text_rect.bottom):
if mask.get_at((x - text_rect.left, y - text_rect.top)):
pixels.append((x, y))
return pixels
# 创建粒子系统
particles = []
text_pixels = get_text_pixels()
# 减少粒子数量:只取部分像素点
sampled_points = []
step = 5# 采样步长,控制粒子数量
for i in range(0, len(text_pixels), step):
sampled_points.append(text_pixels)
# 主循环
clock = pygame.time.Clock()
running = True
particles_created = 0
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.fill(BACKGROUND)
# 生成新粒子(按顺序)
if particles_created < len(sampled_points):
# 每次生成30个粒子
for _ in range(30):
if particles_created < len(sampled_points):
start_x = WIDTH // 2
start_y = 500
target_x, target_y = sampled_points
particles.append(Particle(start_x, start_y, target_x, target_y))
particles_created += 1
# 更新和绘制粒子
for particle in particles:
particle.move()
particle.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit() 以下是修正后的Pygame粒子效果代码,实现了"I LOVE YOU!"文字粒子动画:
import pygame
import sys
import math
import random
# 初始化Pygame
pygame.init()
# 屏幕设置
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
pygame.display.set_caption("pygame粒子效果: I LOVE YOU!")
# 颜色定义
BACKGROUND = (10, 10, 20)
RED = (255, 50, 50)
PINK = (255, 150, 200)
PARTICLE_COLORS =
# 字体设置
font = pygame.font.SysFont(None, 180)
text = font.render("I LOVE YOU!", True, RED)
text_rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
# 粒子类
class Particle:
def __init__(self, x, y, target_x, target_y):
self.x = x
self.y = y
self.target_x = target_x
self.target_y = target_y
self.size = random.randint(1, 3)
self.color = random.choice(PARTICLE_COLORS)
self.speed = random.uniform(1.5, 3.5)
self.arrived = False
self.trail = []
def move(self):
if not self.arrived:
dx = self.target_x - self.x
dy = self.target_y - self.y
distance = math.sqrt(dx * dx + dy * dy)
if distance < 2:
self.arrived = True
return
dx /= distance
dy /= distance
self.x += dx * self.speed
self.y += dy * self.speed
# 记录轨迹用于拖尾效果
if len(self.trail) > 5:
self.trail.pop(0)
self.trail.append((int(self.x), int(self.y)))
def draw(self, surface):
# 绘制粒子轨迹
for i, pos in enumerate(self.trail):
alpha = i * 50
trail_color = (self.color, self.color, self.color)
pygame.draw.circle(surface, trail_color, pos, max(1, self.size * i//3))
# 绘制粒子主体
pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), self.size)
# 获取文字像素点
def get_text_pixels():
pixels = []
mask = pygame.mask.from_surface(text)
for x in range(text_rect.left, text_rect.right):
for y in range(text_rect.top, text_rect.bottom):
if mask.get_at((x - text_rect.left, y - text_rect.top)):
pixels.append((x, y))
return pixels
# 创建粒子系统
particles = []
text_pixels = get_text_pixels()
# 减少粒子数量:只取部分像素点
sampled_points = []
step = 5# 采样步长,控制粒子数量
for i in range(0, len(text_pixels), step):
sampled_points.append(text_pixels)
# 主循环
clock = pygame.time.Clock()
running = True
particles_created = 0
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.type == pygame.VIDEORESIZE:
WIDTH, HEIGHT = event.w, event.h
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
text_rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
# 填充背景
screen.fill(BACKGROUND)
# 生成新粒子
if particles_created < len(sampled_points):
for _ in range(30):
if particles_created < len(sampled_points):
start_x = random.randint(0, WIDTH)
start_y = HEIGHT + 50
target_x, target_y = sampled_points
particles.append(Particle(start_x, start_y, target_x, target_y))
particles_created += 1
# 更新和绘制粒子
for particle in particles[:]:
particle.move()
particle.draw(screen)
# 移除到达目标且轨迹消失的粒子
if particle.arrived and len(particle.trail) == 0:
particles.remove(particle)
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
代码主要修改点:
1. 补全了粒子颜色列表 PARTICLE_COLORS
2. 修正了采样点生成逻辑 sampled_points.append(text_pixels)
3. 添加了粒子拖尾效果和轨迹衰减
4. 增加了窗口大小调整支持
5. 优化了粒子初始位置随机分布
6. 添加了粒子回收机制(到达目标后消失)
运行效果说明:
- 粒子从屏幕底部随机位置飞向 "I LOVE YOU!" 文字轮廓
- 粒子呈现红色/粉色渐变效果
- 粒子移动时带有拖尾光效
- 支持窗口大小动态调整
- 按ESC键或关闭窗口退出程序
提示:可通过调整 step 值控制粒子密度(值越大粒子越少)
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 膜拜大佬
页:
[1]