import pygame
import math
import random
import sys

# ================== 初始化 ==================
pygame.init()

WIDTH, HEIGHT = 1000, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pygame 最复杂图形演示")

clock = pygame.time.Clock()

# ✅ 字体安全写法（防止第11行报错）
try:
    font = pygame.font.SysFont("arial", 24)
except Exception:
    font = pygame.font.Font(None, 24)

# ================== 颜色工具 ==================


def hsv_to_rgb(h, s, v):
    i = int(h * 6)
    f = h * 6 - i
    p = v * (1 - s)
    q = v * (1 - f * s)
    t = v * (1 - (1 - f) * s)
    i = i % 6
    if i == 0:
        return int(v*255), int(t*255), int(p*255)
    if i == 1:
        return int(q*255), int(v*255), int(p*255)
    if i == 2:
        return int(p*255), int(v*255), int(t*255)
    if i == 3:
        return int(p*255), int(q*255), int(v*255)
    if i == 4:
        return int(t*255), int(p*255), int(v*255)
    return int(v*255), int(p*255), int(q*255)

# ================== 粒子系统 ==================


class Particle:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        angle = random.uniform(0, math.pi * 2)
        speed = random.uniform(1, 4)
        self.vx = math.cos(angle) * speed
        self.vy = math.sin(angle) * speed
        self.life = 60

    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.life -= 1

    def draw(self, surf):
        alpha = max(0, min(255, self.life * 4))
        color = (255, 200, 100, alpha)
        s = pygame.Surface((6, 6), pygame.SRCALPHA)
        pygame.draw.circle(s, color, (3, 3), 3)
        surf.blit(s, (int(self.x), int(self.y)))


particles = []

# ================== 主循环 ==================
running = True
time_counter = 0.0

while running:
    dt = clock.tick(60)
    time_counter += 0.02

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            for _ in range(30):
                particles.append(Particle(*pygame.mouse.get_pos()))

    # 背景
    screen.fill((10, 10, 30))

    # 星空
    for i in range(200):
        x = (i * 12345) % WIDTH
        y = (i * 54321) % HEIGHT
        brightness = int(100 + 50 * math.sin(time_counter + i))
        pygame.draw.circle(
            screen, (brightness, brightness, brightness), (x, y), 1)

    # 中心几何
    cx, cy = WIDTH // 2, HEIGHT // 2
    for i in range(12):
        angle = time_counter + i / 6
        size = 120 + 40 * math.sin(time_counter * 2 + i)
        points = []
        for j in range(6):
            a = angle + j * math.pi / 3
            r = size * (0.7 + 0.3 * math.sin(time_counter + j))
            px = cx + r * math.cos(a)
            py = cy + r * math.sin(a)
            points.append((px, py))

        hue = (time_counter / 5 + i / 12) % 1
        color = hsv_to_rgb(hue, 0.8, 1)
        pygame.draw.polygon(screen, color, points, width=3)

    # 粒子
    for p in particles[:]:
        p.update()
        p.draw(screen)
        if p.life <= 0:
            particles.remove(p)

    # FPS
    fps = clock.get_fps()
    screen.blit(font.render(f"FPS: {fps:.1f}",
                True, (255, 255, 255)), (10, 10))

    pygame.display.flip()

pygame.quit()
sys.exit()
