import pygame
import sys
import random
import math

pygame.init()
WIDTH, HEIGHT = 1000, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("迪斯科模拟器 Disco‑Party")
clock = pygame.time.Clock()
FPS = 60

# 颜色库
COLOR_LIST = [
    (255, 30, 30),
    (30, 255, 30),
    (30, 80, 255),
    (255, 220, 20),
    (255, 30, 200),
    (80, 255, 255),
    (180, 80, 255)
]

# 跳舞者类
class Dancer:
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.anim_timer = 0
        self.pose = 0
        self.speed_x = random.uniform(-1.2,1.2)
        self.speed_y = random.uniform(-1.2,1.2)

    def update(self):
        self.anim_timer += 1
        if self.anim_timer >= 8:
            self.pose = (self.pose + 1) % 4
            self.anim_timer = 0
        self.x += self.speed_x
        self.y += self.speed_y
        if self.x<80 or self.x>WIDTH-80:
            self.speed_x *= -1
        if self.y<120 or self.y>HEIGHT-60:
            self.speed_y *= -1

    def draw(self):
        px,py = int(self.x),int(self.y)
        # 头部
        pygame.draw.circle(screen,(255,230,210),(px,py-35),16)
        # 身体
        pygame.draw.line(screen,(40,40,40),(px,py-20),(px,py+25),4)
        # 四肢 4种跳舞姿势
        if self.pose == 0:
            pygame.draw.line(screen,(40,40,40),(px,py-8),(px-28,py+12),3)
            pygame.draw.line(screen,(40,40,40),(px,py-8),(px+28,py+12),3)
            pygame.draw.line(screen,(40,40,40),(px,py+25),(px-22,py+60),3)
            pygame.draw.line(screen,(40,40,40),(px,py+25),(px+22,py+60),3)
        elif self.pose ==1:
            pygame.draw.line(screen,(40,40,40),(px,py-8),(px-32,py-5),3)
            pygame.draw.line(screen,(40,40,40),(px,py-8),(px+32,py-5),3)
            pygame.draw.line(screen,(40,40,40),(px,py+25),(px-18,py+62),3)
            pygame.draw.line(screen,(40,40,40),(px,py+25),(px+18,py+62),3)
        elif self.pose ==2:
            pygame.draw.line(screen,(40,40,40),(px,py-8),(px-25,py+18),3)
            pygame.draw.line(screen,(40,40,40),(px,py-8),(px+25,py-18),3)
            pygame.draw.line(screen,(40,40,40),(px,py+25),(px+25,py+58),3)
            pygame.draw.line(screen,(40,40,40),(px,py+25),(px-25,py+58),3)
        else:
            pygame.draw.line(screen,(40,40,40),(px,py-8),(px-30,py-10),3)
            pygame.draw.line(screen,(40,40,40),(px,py-8),(px+30,py+15),3)
            pygame.draw.line(screen,(40,40,40),(px,py+25),(px-20,py+60),3)
            pygame.draw.line(screen,(40,40,40),(px,py+25),(px+20,py+60),3)

# 创建多名舞者
dancers = []
for i in range(8):
    dancers.append(Dancer(random.randint(150,WIDTH-150),random.randint(180,HEIGHT-120)))

light_timer = 0
laser_angle = 0
bg_color = random.choice(COLOR_LIST)   # 初始化灯光颜色
running = True

while running:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_SPACE]:
        for d in dancers:
            d.speed_x = random.uniform(-2.5,2.5)
            d.speed_y = random.uniform(-2.5,2.5)

    # 更新灯光
    light_timer += 1
    if light_timer>7:
        bg_color = random.choice(COLOR_LIST)
        light_timer = 0
    screen.fill((int(bg_color[0]*0.32),int(bg_color[1]*0.32),int(bg_color[2]*0.32)))

    # 迪斯科方格舞池地板
    tile_size = 60
    for tx in range(0,WIDTH,tile_size):
        for ty in range(100,HEIGHT,tile_size):
            c = random.choice(COLOR_LIST)
            pygame.draw.rect(screen,(c[0]//3,c[1]//3,c[2]//3),[tx,ty,tile_size-2,tile_size-2])

    # 顶部旋转镭射灯
    laser_angle += 0.045
    center_x,center_y = WIDTH//2,60
    for i in range(6):
        ang = laser_angle + i*(math.pi/3)
        end_x = center_x + math.cos(ang)*950
        end_y = center_y + math.sin(ang)*950
        pygame.draw.line(screen,random.choice(COLOR_LIST),(center_x,center_y),(end_x,end_y),3)
    pygame.draw.circle(screen,(255,255,255),(center_x,center_y),22)

    # 更新并绘制所有跳舞小人
    for human in dancers:
        human.update()
        human.draw()

    pygame.display.flip()

pygame.quit()
sys.exit()