import pygame
import sys

pygame.init()

WIDTH, HEIGHT = 1500, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("多次考试 - 三科分开趋势图")

clock = pygame.time.Clock()
font = pygame.font.SysFont("simhei", 22)

# ===== 数据 =====
exams = ["期中", "期末", "一模", "二模", "三模"]

data = {
    "语文": [85, 88, 90, 87, 92],
    "数学": [78, 82, 95, 91, 96],
    "英语": [80, 84, 86, 88, 90]
}

colors = {
    "语文": (255, 80, 80),
    "数学": (80, 255, 120),
    "英语": (80, 160, 255)
}


# ===== 画单个折线图函数 =====
def draw_chart(x_offset, subject):

    scores = data[subject]
    color = colors[subject]

    chart_width = 400
    chart_height = 400
    base_x = x_offset
    base_y = 550

    # 坐标轴
    pygame.draw.line(screen, (200, 200, 200),
                     (base_x, base_y),
                     (base_x + chart_width, base_y), 2)

    pygame.draw.line(screen, (200, 200, 200),
                     (base_x, base_y),
                     (base_x, base_y - chart_height), 2)

    points = []

    for i, score in enumerate(scores):
        x = base_x + 50 + i * 70
        y = base_y - score * 3
        points.append((x, y))

        pygame.draw.circle(screen, color, (x, y), 6)

        score_text = font.render(str(score), True, (255, 255, 255))
        screen.blit(score_text, (x - 10, y - 25))

    # 画折线
    for i in range(len(points) - 1):
        pygame.draw.line(screen, color, points[i], points[i + 1], 3)

    # 考试标签
    for i, exam in enumerate(exams):
        x = base_x + 50 + i * 70
        text = font.render(exam, True, (220, 220, 220))
        screen.blit(text, (x - 25, base_y + 10))

    # 科目标题
    title = font.render(subject, True, color)
    screen.blit(title, (base_x + 150, 100))


# ===== 主循环 =====
while True:

    screen.fill((25, 30, 45))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # 三个图横向排列
    draw_chart(150, "语文")
    draw_chart(550, "数学")
    draw_chart(950, "英语")

    pygame.display.flip()
    clock.tick(60)