酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

制作py3D爱心粒子全屏跳动代码完整版

以下是一个使用Python的Pygame和NumPy库实现的3D爱心粒子全屏跳动效果完整代码: ```python import pygame import numpy as np import math import random # 初始化pygame pygame.init() # 获取屏幕尺寸并设置全屏 info = pygame.display.Info() WIDTH, HEIGHT = info.current_w, info.current_h screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN) pygame.display.set_caption("3D爱心粒子跳动") # 颜色定义 RED = (255, 50, 50) PINK = (255, 100, 200) WHITE = (255, 255, 255) # 粒子类 class Particle: def __init__(self): # 3D爱心参数方程 t = random.uniform(0, 2 * math.pi) size = random.uniform(2, 5) # 爱心形状参数 x = 16 * (math.sin(t) 3) y = 13 * math.cos(t) - 5 * math.cos(2*t) - 2 * math.cos(3*t) - math.cos(4*t) z = random.uniform(-5, 5) # 初始位置(居中) self.pos = np.array([x * 10 + WIDTH // 2, -y * 10 + HEIGHT // 2, z]) self.vel = np.array([0, 0, 0]) self.acc = np.array([0, 0, 0]) self.size = size self.color = random.choice([RED, PINK, WHITE]) self.life = 255 def update(self): # 添加随机运动 self.acc = np.array([random.uniform(-0.2, 0.2), random.uniform(-0.2, 0.2), random.uniform(-0.1, 0.1)]) # 物理模拟 self.vel += self.acc self.pos += self.vel # 边界检查 if self.pos[0] < 0 or self.pos[0] > WIDTH: self.vel[0] *= -0.5 if self.pos[1] < 0 or self.pos[1] > HEIGHT: self.vel[1] *= -0.5 # 生命周期 self.life -= 0.5 if self.life <= 0: self.__init__() def draw(self): # 3D透视效果(简单的z轴缩放) scale = 1 - self.pos[2] / 20 size = max(1, self.size * scale) # 根据z轴调整亮度 alpha = min(255, self.life) color = (*self.color[:3], alpha) # 绘制粒子 s = pygame.Surface((size*2, size*2), pygame.SRCALPHA) pygame.draw.circle(s, color, (size, size), size) screen.blit(s, (self.pos[0] - size, self.pos[1] - size)) # 创建粒子群 particles = [Particle() for _ in range(300)] # 主循环 clock = pygame.time.Clock() running = True while running: screen.fill((0, 0, 0)) 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 # 更新和绘制所有粒子 for p in particiles: p.update() p.draw() pygame.display.flip() clock.tick(60) pygame.quit() ``` 代码说明: 1. 使用参数方程生成3D爱心形状的粒子分布 2. 每个粒子具有3D位置和简单的物理模拟 3. 添加了z轴透视效果和生命周期变化 4. 粒子会随机运动并在边界反弹 5. 按ESC键或关闭窗口可退出程序 运行要求: - 需要安装Pygame和NumPy库 - 可通过`pip install pygame numpy`安装 三条额外需要注意的逻辑点: 1. 爱心形状是通过参数方程生成的,调整方程参数可以改变爱心形状 2. 粒子的z轴位置影响其大小和透视效果,但不影响实际绘制位置(伪3D效果) 3. 粒子的生命周期结束后会重新初始化,形成持续动画效果 [2025-03-27 10:54:52 | AI写代码神器 | 1025点数解答]

相关提问