import pygame
import sys

# 初始化Pygame
pygame.init()

# 屏幕设置
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("课程表")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
LIGHT_BLUE = (173, 216, 230)
LIGHT_GREEN = (144, 238, 144)
LIGHT_YELLOW = (255, 255, 224)
LIGHT_PINK = (255, 192, 203)
GRAY = (200, 200, 200)

# 修复中文显示：使用系统中文字体
def get_chinese_font(size):
    font_names = [
        "SimHei", "Microsoft YaHei", "SimSun",
        "PingFang SC", "Heiti SC",
        "WenQuanYi Micro Hei", "Noto Sans CJK SC"
    ]
    
    for font_name in font_names:
        try:
            font = pygame.font.SysFont(font_name, size)
            test_text = font.render("测试", True, BLACK)
            if test_text.get_width() > 10:
                return font
        except:
            continue
    
    print("警告：未找到支持中文的字体，中文可能显示为方块")
    return pygame.font.Font(None, size)

# 字体设置
font_large = get_chinese_font(48)
font_medium = get_chinese_font(32)
font_small = get_chinese_font(24)

# 课程表数据
days = ["周一", "周二", "周三", "周四", "周五"]
periods = ["第1节\n8:00-8:45", "第2节\n8:55-9:40", "第3节\n10:00-10:45", 
           "第4节\n10:55-11:40", "第5节\n14:00-14:45", "第6节\n14:55-15:40",
           "第7节\n16:00-16:45", "第8节\n16:55-17:40"]

# 示例课程数据
schedule = [
    ["语文", "数学", "英语", "物理", "化学"],
    ["数学", "英语", "语文", "化学", "物理"],
    ["英语", "物理", "数学", "语文", "生物"],
    ["物理", "化学", "生物", "数学", "语文"],
    ["化学", "生物", "物理", "英语", "数学"],
    ["体育", "音乐", "美术", "信息技术", "历史"],
    ["地理", "政治", "班会", "", ""],
    ["", "", "", "", ""]
]

# 课程颜色
subject_colors = {
    "语文": LIGHT_YELLOW,
    "数学": LIGHT_BLUE,
    "英语": LIGHT_GREEN,
    "物理": LIGHT_PINK,
    "化学": (255, 218, 185),
    "生物": (220, 220, 220),
    "历史": (240, 230, 140),
    "地理": (176, 224, 230),
    "政治": (255, 228, 196),
    "体育": (152, 251, 152),
    "音乐": (255, 218, 233),
    "美术": (230, 230, 250),
    "信息技术": (211, 211, 211),
    "班会": (255, 240, 245),
    "": WHITE
}

# 主程序
def main():
    cell_width = (WIDTH - 100) // 5
    cell_height = (HEIGHT - 100) // 8
    
    while True:
        screen.fill(WHITE)
        
        # 绘制标题
        title = font_large.render("课程表", True, BLACK)
        screen.blit(title, (WIDTH//2 - title.get_width()//2, 20))
        
        # 绘制星期标题
        for i, day in enumerate(days):
            x = 100 + i * cell_width
            y = 80
            pygame.draw.rect(screen, GRAY, (x, y, cell_width, 30))
            pygame.draw.rect(screen, BLACK, (x, y, cell_width, 30), 1)
            text = font_medium.render(day, True, BLACK)
            screen.blit(text, (x + cell_width//2 - text.get_width()//2, y + 5))
        
        # 绘制节次和课程
        for i, period in enumerate(periods):
            # 绘制节次
            y = 110 + i * cell_height
            pygame.draw.rect(screen, GRAY, (20, y, 80, cell_height))
            pygame.draw.rect(screen, BLACK, (20, y, 80, cell_height), 1)
            lines = period.split('\n')
            for j, line in enumerate(lines):
                text = font_small.render(line, True, BLACK)
                screen.blit(text, (20 + 40 - text.get_width()//2, y + 10 + j*20))
            
            # 绘制课程
            for j in range(5):
                x = 100 + j * cell_width
                subject = schedule[i][j]
                color = subject_colors.get(subject, WHITE)
                pygame.draw.rect(screen, color, (x, y, cell_width, cell_height))
                pygame.draw.rect(screen, BLACK, (x, y, cell_width, cell_height), 1)
                if subject:
                    text = font_medium.render(subject, True, BLACK)
                    screen.blit(text, (x + cell_width//2 - text.get_width()//2, 
                                      y + cell_height//2 - text.get_height()//2))
        
        # 事件处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        
        pygame.display.flip()

if __name__ == "__main__":
    main()