鱼C论坛

 找回密码
 立即注册
查看: 63|回复: 2

[技术交流] pygame粒子效果:ILOVEYOU!

[复制链接]
发表于 昨天 09:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
pygame粒子效果:ILOVEYOU!
  1. import pygame
  2. import sys
  3. import math
  4. import random

  5. # 初始化Pygame
  6. pygame.init()

  7. # 屏幕设置
  8. WIDTH, HEIGHT = 800, 600
  9. screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
  10. pygame.display.set_caption("pygame粒子效果: I LOVE YOU!")

  11. # 颜色定义
  12. BACKGROUND = (10, 10, 20)
  13. RED = (255, 50, 50)
  14. PARTICLE_COLORS = [
  15.     (255, 50, 50),  # 红色
  16.     (255, 100, 100),  # 浅红
  17.     (255, 150, 150),  # 粉红
  18.     (255, 200, 200)  # 淡粉
  19. ]

  20. # 字体设置
  21. font = pygame.font.SysFont(None, 180)
  22. text = font.render("I LOVE YOU!", True, RED)

  23. text_rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2))

  24. # 粒子类
  25. class Particle:
  26.     def __init__(self, x, y, target_x, target_y):
  27.         self.x = x
  28.         self.y = y
  29.         self.target_x = target_x
  30.         self.target_y = target_y
  31.         self.size = 2
  32.         self.color = random.choice(PARTICLE_COLORS)
  33.         self.speed = random.uniform(2, 3)
  34.         self.arrived = False

  35.     def move(self):
  36.         if not self.arrived:
  37.             dx = self.target_x - self.x
  38.             dy = self.target_y - self.y
  39.             distance = math.sqrt(dx * dx + dy * dy)

  40.             if distance < 2:
  41.                 self.arrived = True
  42.                 return

  43.             dx /= distance
  44.             dy /= distance

  45.             self.x += dx * self.speed
  46.             self.y += dy * self.speed

  47.     def draw(self, surface):
  48.         pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), self.size)

  49. # 获取文字像素点
  50. def get_text_pixels():
  51.     pixels = []
  52.     mask = pygame.mask.from_surface(text)

  53.     # 获取整个文本的边界框
  54.     for x in range(text_rect.left, text_rect.right):
  55.         for y in range(text_rect.top, text_rect.bottom):
  56.             if mask.get_at((x - text_rect.left, y - text_rect.top)):
  57.                 pixels.append((x, y))

  58.     return pixels

  59. # 创建粒子系统
  60. particles = []
  61. text_pixels = get_text_pixels()

  62. # 减少粒子数量:只取部分像素点
  63. sampled_points = []
  64. step = 5  # 采样步长,控制粒子数量
  65. for i in range(0, len(text_pixels), step):
  66.     sampled_points.append(text_pixels[i])


  67. # 主循环
  68. clock = pygame.time.Clock()
  69. running = True
  70. particles_created = 0

  71. while running:
  72.     for event in pygame.event.get():
  73.         if event.type == pygame.QUIT:
  74.             running = False
  75.         elif event.type == pygame.KEYDOWN:
  76.             if event.key == pygame.K_ESCAPE:
  77.                 running = False

  78.     # 填充背景
  79.     screen.fill(BACKGROUND)

  80.     # 生成新粒子(按顺序)
  81.     if particles_created < len(sampled_points):
  82.         # 每次生成30个粒子
  83.         for _ in range(30):
  84.             if particles_created < len(sampled_points):
  85.                 start_x = WIDTH // 2
  86.                 start_y = 500
  87.                 target_x, target_y = sampled_points[particles_created]
  88.                 particles.append(Particle(start_x, start_y, target_x, target_y))
  89.                 particles_created += 1

  90.     # 更新和绘制粒子
  91.     for particle in particles:
  92.         particle.move()
  93.         particle.draw(screen)

  94.     pygame.display.flip()
  95.     clock.tick(60)

  96. pygame.quit()
  97. sys.exit()
复制代码

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
player-none + 2 + 2

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 昨天 09:21 | 显示全部楼层
以下是修正后的Pygame粒子效果代码,实现了"I LOVE YOU!"文字粒子动画:

  1. import pygame
  2. import sys
  3. import math
  4. import random

  5. # 初始化Pygame
  6. pygame.init()

  7. # 屏幕设置
  8. WIDTH, HEIGHT = 800, 600
  9. screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
  10. pygame.display.set_caption("pygame粒子效果: I LOVE YOU!")

  11. # 颜色定义
  12. BACKGROUND = (10, 10, 20)
  13. RED = (255, 50, 50)
  14. PINK = (255, 150, 200)
  15. PARTICLE_COLORS = [RED, PINK, (255, 100, 100), (255, 200, 200)]

  16. # 字体设置
  17. font = pygame.font.SysFont(None, 180)
  18. text = font.render("I LOVE YOU!", True, RED)
  19. text_rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2))

  20. # 粒子类
  21. class Particle:
  22.     def __init__(self, x, y, target_x, target_y):
  23.         self.x = x
  24.         self.y = y
  25.         self.target_x = target_x
  26.         self.target_y = target_y
  27.         self.size = random.randint(1, 3)
  28.         self.color = random.choice(PARTICLE_COLORS)
  29.         self.speed = random.uniform(1.5, 3.5)
  30.         self.arrived = False
  31.         self.trail = []

  32.     def move(self):
  33.         if not self.arrived:
  34.             dx = self.target_x - self.x
  35.             dy = self.target_y - self.y
  36.             distance = math.sqrt(dx * dx + dy * dy)

  37.             if distance < 2:
  38.                 self.arrived = True
  39.                 return

  40.             dx /= distance
  41.             dy /= distance

  42.             self.x += dx * self.speed
  43.             self.y += dy * self.speed
  44.             
  45.             # 记录轨迹用于拖尾效果
  46.             if len(self.trail) > 5:
  47.                 self.trail.pop(0)
  48.             self.trail.append((int(self.x), int(self.y)))

  49.     def draw(self, surface):
  50.         # 绘制粒子轨迹
  51.         for i, pos in enumerate(self.trail):
  52.             alpha = i * 50
  53.             trail_color = (self.color[0], self.color[1], self.color[2])
  54.             pygame.draw.circle(surface, trail_color, pos, max(1, self.size * i//3))
  55.         
  56.         # 绘制粒子主体
  57.         pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), self.size)

  58. # 获取文字像素点
  59. def get_text_pixels():
  60.     pixels = []
  61.     mask = pygame.mask.from_surface(text)

  62.     for x in range(text_rect.left, text_rect.right):
  63.         for y in range(text_rect.top, text_rect.bottom):
  64.             if mask.get_at((x - text_rect.left, y - text_rect.top)):
  65.                 pixels.append((x, y))
  66.     return pixels

  67. # 创建粒子系统
  68. particles = []
  69. text_pixels = get_text_pixels()

  70. # 减少粒子数量:只取部分像素点
  71. sampled_points = []
  72. step = 5  # 采样步长,控制粒子数量
  73. for i in range(0, len(text_pixels), step):
  74.     sampled_points.append(text_pixels[i])

  75. # 主循环
  76. clock = pygame.time.Clock()
  77. running = True
  78. particles_created = 0

  79. while running:
  80.     for event in pygame.event.get():
  81.         if event.type == pygame.QUIT:
  82.             running = False
  83.         elif event.type == pygame.KEYDOWN:
  84.             if event.key == pygame.K_ESCAPE:
  85.                 running = False
  86.         elif event.type == pygame.VIDEORESIZE:
  87.             WIDTH, HEIGHT = event.w, event.h
  88.             screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
  89.             text_rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2))

  90.     # 填充背景
  91.     screen.fill(BACKGROUND)

  92.     # 生成新粒子
  93.     if particles_created < len(sampled_points):
  94.         for _ in range(30):
  95.             if particles_created < len(sampled_points):
  96.                 start_x = random.randint(0, WIDTH)
  97.                 start_y = HEIGHT + 50
  98.                 target_x, target_y = sampled_points[particles_created]
  99.                 particles.append(Particle(start_x, start_y, target_x, target_y))
  100.                 particles_created += 1

  101.     # 更新和绘制粒子
  102.     for particle in particles[:]:
  103.         particle.move()
  104.         particle.draw(screen)
  105.         
  106.         # 移除到达目标且轨迹消失的粒子
  107.         if particle.arrived and len(particle.trail) == 0:
  108.             particles.remove(particle)

  109.     pygame.display.flip()
  110.     clock.tick(60)

  111. pygame.quit()
  112. sys.exit()
复制代码


代码主要修改点:
1. 补全了粒子颜色列表 PARTICLE_COLORS
2. 修正了采样点生成逻辑 sampled_points.append(text_pixels[i])
3. 添加了粒子拖尾效果和轨迹衰减
4. 增加了窗口大小调整支持
5. 优化了粒子初始位置随机分布
6. 添加了粒子回收机制(到达目标后消失)

运行效果说明:
- 粒子从屏幕底部随机位置飞向 "I LOVE YOU!" 文字轮廓
- 粒子呈现红色/粉色渐变效果
- 粒子移动时带有拖尾光效
- 支持窗口大小动态调整
- 按ESC键或关闭窗口退出程序

提示:可通过调整 step 值控制粒子密度(值越大粒子越少)

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 10:20 | 显示全部楼层
膜拜大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 13:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表