import pygame
import sys
import math

# 初始化pygame
pygame.init()

# 窗口设置
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("成绩走势图 - 元宝")
clock = pygame.time.Clock()

# 颜色定义
BACKGROUND = (240, 245, 250)
GRID_COLOR = (200, 210, 220)
AXIS_COLOR = (50, 50, 50)
LINE_COLOR = (0, 120, 215)
POINT_COLOR = (215, 60, 0)
TEXT_COLOR = (30, 30, 30)
GRID_COLOR = (180, 195, 210)

# 模拟成绩数据（你可以替换成你的实际成绩）
# 格式：[考试名称, 成绩]
exam_data = [
    ["期中1", 85],
    ["期中2", 78],
    ["期中3", 82],
    ["期中4", 90],
    ["期中5", 88],
    ["期中6", 92],
    ["期末", 95]
]

# 获取最大最小值
scores = [data[1] for data in exam_data]
max_score = max(scores)
min_score = min(scores)

# 图表边距
MARGIN = 80
CHART_WIDTH = WIDTH - 2 * MARGIN
CHART_HEIGHT = HEIGHT - 2 * MARGIN

# 字体
font = pygame.font.SysFont('microsoftyahei', 18)
title_font = pygame.font.SysFont('microsoftyahei', 24, bold=True)

def draw_grid():
    """绘制网格"""
    # 水平网格线
    for i in range(11):  # 0-100分，间隔10
        y = MARGIN + CHART_HEIGHT - (i * CHART_HEIGHT / 10)
        pygame.draw.line(screen, GRID_COLOR, 
                        (MARGIN, y), 
                        (WIDTH - MARGIN, y), 1)
        
        # 分数标签
        score_text = font.render(str(i * 10), True, TEXT_COLOR)
        screen.blit(score_text, (MARGIN - 40, y - 10))

def draw_axes():
    """绘制坐标轴"""
    # Y轴
    pygame.draw.line(screen, AXIS_COLOR, 
                    (MARGIN, MARGIN), 
                    (MARGIN, HEIGHT - MARGIN), 2)
    # X轴
    pygame.draw.line(screen, AXIS_COLOR, 
                    (MARGIN, HEIGHT - MARGIN), 
                    (WIDTH - MARGIN, HEIGHT - MARGIN), 2)

def draw_chart():
    """绘制图表"""
    if len(exam_data) < 2:
        return
    
    points = []
    
    # 计算每个数据点的位置
    for i, (exam_name, score) in enumerate(exam_data):
        x = MARGIN + (i * CHART_WIDTH / (len(exam_data) - 1))
        y = HEIGHT - MARGIN - ((score - min_score) * CHART_HEIGHT / (max_score - min_score))
        points.append((x, y))
        
        # 绘制数据点
        pygame.draw.circle(screen, POINT_COLOR, (int(x), int(y)), 6)
        pygame.draw.circle(screen, (255, 255, 255), (int(x), int(y)), 3)
        
        # 显示考试名称
        exam_text = font.render(exam_name, True, TEXT_COLOR)
        text_rect = exam_text.get_rect(center=(x, HEIGHT - MARGIN + 30))
        screen.blit(exam_text, text_rect)
        
        # 显示具体分数
        score_text = font.render(str(score), True, POINT_COLOR)
        score_rect = score_text.get_rect(center=(x, y - 20))
        screen.blit(score_text, score_rect)
    
    # 连接数据点
    for i in range(len(points) - 1):
        pygame.draw.line(screen, LINE_COLOR, points[i], points[i + 1], 3)

def draw_title():
    """绘制标题"""
    title = title_font.render("成绩走势图", True, (0, 90, 170))
    screen.blit(title, (WIDTH // 2 - title.get_width() // 2, 20))
    
    # 显示平均分
    avg_score = sum(scores) / len(scores)
    avg_text = font.render(f"平均分: {avg_score:.1f}", True, (0, 120, 60))
    screen.blit(avg_text, (WIDTH - 150, 30))
    
    # 显示最高分和最低分
    max_min_text = font.render(f"最高: {max_score}  最低: {min_score}", True, (120, 60, 0))
    screen.blit(max_min_text, (WIDTH - 200, 60))

def main():
    """主循环"""
    running = True
    
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False
        
        # 填充背景
        screen.fill(BACKGROUND)
        
        # 绘制各元素
        draw_grid()
        draw_axes()
        draw_chart()
        draw_title()
        
        # 显示操作提示
        tip_text = font.render("按ESC键退出", True, (100, 100, 100))
        screen.blit(tip_text, (WIDTH - 100, HEIGHT - 30))
        
        pygame.display.flip()
        clock.tick(60)
    
    pygame.quit()
    sys.exit()

if __name__ == "__main__":
    main()