import pygame
import sys
import math

pygame.init()

WIDTH, HEIGHT = 1200, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("多次考试成绩对比 - 展示王炸版")

clock = pygame.time.Clock()

font = pygame.font.SysFont("simhei", 20)
big_font = pygame.font.SysFont("simhei", 44)

# ✅ 科目
subjects = ["语文", "数学", "英语", "物理", "化学", "生物"]

# ✅ 三次考试数据
exam_data = [
    [80, 85, 78, 82, 90, 76],
    [85, 88, 80, 86, 92, 79],
    [90, 92, 84, 89, 95, 83]
]

exam_names = ["第一次", "第二次", "第三次"]
colors = [(255, 90, 90), (80, 170, 255), (100, 255, 150)]

chart_left = 150
chart_right = 1050
chart_top = 150
chart_bottom = 600

max_score = 100
min_score = 0

# ✅ 坐标转换


def get_points(scores):
    pts = []
    for i, score in enumerate(scores):
        x = chart_left + i * ((chart_right - chart_left)/(len(scores)-1))
        y = chart_bottom - (score - min_score) / \
            (max_score-min_score)*(chart_bottom-chart_top)
        pts.append((x, y))
    return pts


all_points = [get_points(data) for data in exam_data]

progress = 0
speed = 0.01

# ✅ 背景渐变


def draw_background():
    for y in range(HEIGHT):
        ratio = y/HEIGHT
        color = (int(220-40*ratio), int(220-40*ratio), 255)
        pygame.draw.line(screen, color, (0, y), (WIDTH, y))


# ✅ 平均分
averages = [sum(data)/len(data) for data in exam_data]

running = True
while running:
    clock.tick(60)
    draw_background()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    if progress < 1:
        progress += speed

    visible_index = int(progress*(len(subjects)-1))

    # 标题
    title = big_font.render("多次考试成绩对比 - 王炸版", True, (20, 20, 120))
    screen.blit(title, (WIDTH//2-title.get_width()//2, 50))

    # 坐标轴
    pygame.draw.line(screen, (0, 0, 0), (chart_left, chart_top),
                     (chart_left, chart_bottom), 2)
    pygame.draw.line(screen, (0, 0, 0), (chart_left,
                     chart_bottom), (chart_right, chart_bottom), 2)

    # 网格线
    for i in range(0, 101, 10):
        y = chart_bottom-(i-min_score)/(max_score-min_score) * \
            (chart_bottom-chart_top)
        pygame.draw.line(screen, (210, 210, 230),
                         (chart_left, y), (chart_right, y))
        label = font.render(str(i), True, (50, 50, 50))
        screen.blit(label, (chart_left-40, y-10))

    mouse_pos = pygame.mouse.get_pos()

    # ✅ 绘制每次考试
    for exam_i, points in enumerate(all_points):
        color = colors[exam_i]

        # 半透明面积
        surface = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
        if visible_index > 0:
            fill_points = points[:visible_index+1] + [(points[visible_index][0], chart_bottom),
                                                      (points[0][0], chart_bottom)]
            pygame.draw.polygon(surface, color + (60,), fill_points)
            screen.blit(surface, (0, 0))

        # 折线
        for i in range(visible_index):
            pygame.draw.line(screen, color, points[i], points[i+1], 3)

        # 数据点 + 悬停
        for i in range(visible_index+1):
            pygame.draw.circle(
                screen, color, (int(points[i][0]), int(points[i][1])), 6)

            # 鼠标悬停检测
            if math.hypot(mouse_pos[0]-points[i][0], mouse_pos[1]-points[i][1]) < 10:
                score = exam_data[exam_i][i]
                tooltip = font.render(
                    f"{exam_names[exam_i]} {subjects[i]}: {score}", True, (0, 0, 0))
                pygame.draw.rect(screen, (255, 255, 255),
                                 (mouse_pos[0], mouse_pos[1]-30, tooltip.get_width()+10, 30))
                pygame.draw.rect(screen, (0, 0, 0),
                                 (mouse_pos[0], mouse_pos[1]-30, tooltip.get_width()+10, 30), 1)
                screen.blit(tooltip, (mouse_pos[0]+5, mouse_pos[1]-25))

        # ✅ 标记最高分
        max_score_exam = max(exam_data[exam_i])
        max_index = exam_data[exam_i].index(max_score_exam)
        pygame.draw.circle(screen, (255, 215, 0),
                           (int(points[max_index][0]), int(points[max_index][1])), 10, 2)

    # 科目名称
    for i, subject in enumerate(subjects):
        label = font.render(subject, True, (0, 0, 0))
        screen.blit(label, (all_points[0][i][0]-20, chart_bottom+20))

    # ✅ 图例 + 平均分
    for i, name in enumerate(exam_names):
        pygame.draw.rect(screen, colors[i], (900, 180+i*50, 30, 30))
        label = font.render(f"{name}  平均分:{averages[i]:.1f}", True, (0, 0, 0))
        screen.blit(label, (940, 185+i*50))

    pygame.display.update()

pygame.quit()
sys.exit()
