import pygame
import sys
import math
import random
from pygame.locals import *

# 初始化pygame
pygame.init()

# 设置窗口
WIDTH, HEIGHT =     1200, 1000
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("绚丽多彩的动态爱心")

# 颜色定义
BACKGROUND = (10, 10, 30)
HEART_COLORS = [
    (255, 0, 100),    # 粉红
    (255, 100, 0),    # 橙色
    (255, 255, 0),    # 黄色
    (0, 255, 100),    # 绿色
    (0, 200, 255),    # 蓝色
    (200, 0, 255),    # 紫色
    (255, 50, 150),   # 亮粉红
    (255, 150, 50),   # 珊瑚色
    (180, 255, 100),  # 柠檬绿
    (100, 200, 255),  # 天蓝色
    (220, 100, 255),  # 淡紫色
    (255, 200, 0),    # 金色
]

# 粒子类
class Particle:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color
        self.size = random.randint(3, 8)  # 增大粒子大小范围
        self.speed_x = random.uniform(-2, 2)  # 增加速度范围
        self.speed_y = random.uniform(-2, 2)
        self.life = random.randint(60, 120)  # 增加粒子生命期
    
    def update(self):
        self.x += self.speed_x
        self.y += self.speed_y
        self.life -= 1
        self.size = max(0, self.size - 0.05)
        return self.life > 0
    
    def draw(self, surface):
        alpha = min(255, self.life * 3)
        color_with_alpha = (self.color[0], self.color[1], self.color[2], alpha)
        pygame.draw.circle(surface, color_with_alpha, (int(self.x), int(self.y)), int(self.size))

# 创建透明表面用于绘制粒子
particle_surface = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)

# 粒子列表
particles = []

# 心形函数
def heart_x(t, size=15):
    return size * 16 * (math.sin(t) ** 3)

def heart_y(t, size=15):
    return -size * (13 * math.cos(t) - 5 * math.cos(2*t) - 2 * math.cos(3*t) - math.cos(4*t))

# 主循环
clock = pygame.time.Clock()
angle = 0
pulse = 0
pulse_direction = 1

running = True
while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
    
    # 填充背景色
    screen.fill(BACKGROUND)
    
    # 更新脉动效果
    pulse += 0.05 * pulse_direction
    if pulse > 1:
        pulse_direction = -1
    elif pulse < 0:
        pulse_direction = 1
    
    # 计算心形大小
    heart_size = 15 + 4 * pulse
    
    # 清空粒子表面
    particle_surface.fill((0, 0, 0, 0))
    
    # 绘制心形并生成粒子
    points = []
    for i in range(100):
        t = i / 100 * 2 * math.pi
        x = heart_x(t, heart_size) + WIDTH // 2
        y = heart_y(t, heart_size) + HEIGHT // 2
        points.append((x, y))
        
        # 随机生成粒子
        if random.random() < 0.6:  # 增加粒子生成概率
            color_idx = random.randint(0, len(HEART_COLORS) - 1)  # 随机选择颜色
            color = HEART_COLORS[color_idx]
            particles.append(Particle(x, y, color))
    
    # 绘制心形轮廓
    if len(points) > 1:
        pygame.draw.lines(screen, (255, 255, 255), False, points, 2)
    
    # 更新和绘制粒子
    new_particles = []
    for particle in particles:
        if particle.update():
            new_particles.append(particle)
            particle.draw(particle_surface)
    particles = new_particles
    
    # 将粒子表面绘制到屏幕上
    screen.blit(particle_surface, (0, 0))
    
    # 绘制填充心形
    fill_points = []
    for i in range(200):
        t = i / 200 * 2 * math.pi
        x = heart_x(t, heart_size - 1) + WIDTH // 2
        y = heart_y(t, heart_size - 1) + HEIGHT // 2
        fill_points.append((x, y))
    
    # 创建渐变填充
    if len(fill_points) > 2:
        for i in range(len(fill_points) - 1):
            color_idx = int((i / len(fill_points)) * len(HEART_COLORS))
            color = HEART_COLORS[color_idx % len(HEART_COLORS)]
            pygame.draw.line(screen, color, fill_points[i], fill_points[i+1], 2)
    
    # 添加一些随机闪烁的星星
    for _ in range(5):
        x = random.randint(0, WIDTH)
        y = random.randint(0, HEIGHT)
        size = random.randint(1, 3)
        brightness = random.randint(100, 255)
        pygame.draw.circle(screen, (brightness, brightness, brightness), (x, y), size)
    
    # 更新显示
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
sys.exit()