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

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

# Python 3D爱心粒子全屏跳动代码 以下是一个使用Python和Pygame创建3D爱心粒子全屏跳动效果的完整代码: ```python import pygame import math import random import numpy as np from pygame.locals import * # 初始化pygame pygame.init() # 设置全屏显示 info = pygame.display.Info() WIDTH, HEIGHT = info.current_w, info.current_h screen = pygame.display.set_mode((WIDTH, HEIGHT), FULLSCREEN) pygame.display.set_caption("3D爱心粒子") # 颜色定义 RED = (255, 50, 50) PINK = (255, 100, 150) WHITE = (255, 255, 255) BLACK = (0, 0, 0) # 粒子类 class Particle: def __init__(self): self.reset() self.z = random.uniform(0, 20) def reset(self): self.x = random.uniform(-2, 2) self.y = random.uniform(-2, 2) self.z = random.uniform(0, 20) self.size = random.uniform(1, 3) self.color = random.choice([RED, PINK, WHITE]) self.speed_z = random.uniform(0.02, 0.1) def update(self): self.z -= self.speed_z if self.z <= 0: self.reset() def get_2d_pos(self): # 3D到2D投影 factor = 10 / (10 + self.z) x = self.x * factor + WIDTH // 2 y = -self.y * factor + HEIGHT // 2 return x, y, self.size * factor # 创建爱心形状的函数 def heart_shape(t, scale=1.0): 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) return x * scale, y * scale # 创建粒子系统 particles = [Particle() for _ in range(500)] # 主循环 clock = pygame.time.Clock() running = True time = 0 while running: screen.fill(BLACK) time += 0.01 # 处理退出事件 for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): running = False # 更新并绘制粒子 for particle in particles: # 计算爱心形状 t = random.uniform(0, 2 * math.pi) hx, hy = heart_shape(t, 0.1) # 添加一些随机性使粒子跳动 particle.x = hx + math.sin(time * 2 + particle.z) * 0.2 particle.y = hy + math.cos(time * 1.5 + particle.z) * 0.2 particle.update() x, y, size = particle.get_2d_pos() pygame.draw.circle(screen, particle.color, (int(x), int(y)), int(size)) # 显示FPS fps = int(clock.get_fps()) font = pygame.font.SysFont('Arial', 20) fps_text = font.render(f'FPS: {fps}', True, WHITE) screen.blit(fps_text, (10, 10)) pygame.display.flip() clock.tick(60) pygame.quit() ``` ## 代码说明: 1. 这段代码创建了一个全屏的3D爱心粒子效果,粒子会从远处向屏幕前方移动,形成跳动的爱心形状。 2. 主要组成部分: - `Particle`类:管理每个粒子的位置、大小、颜色和移动逻辑 - `heart_shape`函数:数学公式生成爱心形状的坐标 - 主循环:更新粒子位置并绘制到屏幕上 3. 效果特点: - 粒子在3D空间中形成爱心形状 - 添加了正弦波使爱心有跳动效果 - 粒子从远处向近处移动,形成深度感 - 按ESC键可退出程序 ## 运行要求: - 需要安装pygame库:`pip install pygame numpy` - 建议在性能较好的电脑上运行,因为粒子数量较多 ## 注意事项: 1. 粒子数量可以根据电脑性能调整,增加粒子会使效果更密集但可能降低帧率 2. 爱心的大小和跳动幅度可以通过修改`heart_shape`函数中的参数和正弦波幅度来调整 3. 颜色方案可以自定义,修改`RED`、`PINK`和`WHITE`的值或添加更多颜色选项 [2025-06-02 09:20:45 | AI写代码神器 | 1077点数解答]

相关提问