以下是一个使用Python和Pygame库创建的3D跳动爱心粒子效果源代码: ```python import pygame import math import random from pygame.locals import * # 初始化pygame pygame.init() width, height = 800, 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("3D跳动爱心粒子") # 颜色定义 RED = (255, 0, 0) PINK = (255, 105, 180) WHITE = (255, 255, 255) # 粒子类 class Particle: def __init__(self, x, y, z): self.x = x self.y = y self.z = z self.size = random.uniform(1, 3) self.color = random.choice([RED, PINK]) self.speed = random.uniform(0.01, 0.05) def update(self, time): # 3D爱心公式 t = time * self.speed scale = 10 + 2 * math.sin(t * 5) # 跳动效果 # 3D爱心参数方程 self.x = 16 * (math.sin(t) 3) * scale self.y = -(13 * math.cos(t) - 5 * math.cos(2*t) - 2 * math.cos(3*t) - math.cos(4*t)) * scale self.z = random.uniform(0.5, 1.5) # 添加一些随机性使效果更自然 self.x += random.uniform(-1, 1) self.y += random.uniform(-1, 1) def draw(self): # 简单的3D透视效果 perspective = 300 / (300 + self.z * 100) screen_x = width // 2 + self.x * perspective screen_y = height // 2 + self.y * perspective size = self.size * perspective if size < 0.5: size = 0.5 pygame.draw.circle(screen, self.color, (int(screen_x), int(screen_y)), int(size)) # 创建粒子 particles = [Particle(0, 0, 0) for _ in range(500)] # 主循环 clock = pygame.time.Clock() running = True time = 0 while running: screen.fill((0, 0, 0)) for event in pygame.event.get(): if event.type == QUIT: running = False # 更新和绘制所有粒子 for p in particles: p.update(time) p.draw() pygame.display.flip() time += 0.01 clock.tick(60) pygame.quit() ``` 代码说明: 1. 使用3D爱心参数方程创建粒子运动轨迹 2. 添加透视效果模拟3D空间 3. 通过正弦函数实现跳动动画 4. 每个粒子有随机大小、颜色和速度 运行效果: - 红色和粉色粒子组成3D爱心形状 - 爱心会周期性跳动 - 粒子有远近透视效果 运行前需要安装Pygame库: ``` pip install pygame ``` 注意事项: 1. 粒子数量可以根据电脑性能调整,数量越多效果越细腻但消耗资源越多 2. 修改scale参数可以改变爱心大小 3. 调整time的增量可以改变动画速度 [2025-05-09 14:35:36 | AI写代码神器 | 759点数解答]