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)
GRAY = (100, 100, 100)
BLUE = (50, 100, 200)
LIGHT_BLUE = (230, 240, 255)

# 字体（系统默认字体）
font_title = pygame.font.SysFont("simhei", 40, bold=True)  # 标题字体
font_text = pygame.font.SysFont("simhei", 24)               # 正文字体

# ====================== 历史人物数据（已扩充为10位） ======================
characters = [
    {
        "name": "孔子",
        "intro": "孔子（公元前551年－公元前479年），中国古代思想家、教育家，儒家学派创始人。他开创了私人讲学的风气，倡导仁、义、礼、智、信，其思想对中国和世界都有深远的影响。"
    },
    {
        "name": "秦始皇",
        "intro": "秦始皇（公元前259年－公元前210年），嬴姓，赵氏，名政。中国历史上第一个使用“皇帝”称号的君主，他统一六国、文字、度量衡，建立了中国历史上第一个中央集权制国家。"
    },
    {
        "name": "李白",
        "intro": "李白（701年－762年），字太白，号青莲居士，唐代伟大的浪漫主义诗人，被后人誉为“诗仙”。他的诗风豪放飘逸，充满浪漫主义色彩，代表作有《蜀道难》《将进酒》等。"
    },
    {
        "name": "杜甫",
        "intro": "杜甫（712年－770年），字子美，自号少陵野老，唐代伟大的现实主义诗人，被后人誉为“诗圣”。他的诗多反映社会动荡与人民疾苦，被称为“诗史”，代表作有《登高》《茅屋为秋风所破歌》。"
    },
    {
        "name": "苏轼",
        "intro": "苏轼（1037年－1101年），字子瞻，号东坡居士，北宋著名文学家、书画家。他在诗、词、文、书、画方面都有极高成就，是宋代文学最高成就的代表，代表作有《念奴娇·赤壁怀古》。"
    },
    {
        "name": "岳飞",
        "intro": "岳飞（1103年－1142年），字鹏举，南宋时期抗金名将、军事家。他率领岳家军抗击金兵，保卫南宋半壁江山，后被奸臣秦桧以“莫须有”的罪名杀害，代表作有《满江红·写怀》。"
    },
    {
        "name": "诸葛亮",
        "intro": "诸葛亮（181年－234年），字孔明，号卧龙，三国时期蜀汉丞相，杰出的政治家、军事家。他辅佐刘备建立蜀汉，鞠躬尽瘁，是中国传统文化中忠臣与智者的代表人物。"
    },
    {
        "name": "武则天",
        "intro": "武则天（624年－705年），名曌，中国历史上唯一的正统女皇帝。她在位期间，打击门阀、发展科举、重视农业生产，为开元盛世奠定了基础，展现了卓越的政治才能。"
    },
    {
        "name": "成吉思汗",
        "intro": "成吉思汗（1162年－1227年），孛儿只斤·铁木真，大蒙古国可汗，世界史上杰出的军事家、政治家。他统一蒙古各部，建立了横跨欧亚大陆的蒙古帝国，对世界历史产生了深远影响。"
    },
    {
        "name": "林则徐",
        "intro": "林则徐（1785年－1850年），字元抚，清代政治家、思想家。他是鸦片战争时期主张严禁鸦片、抵抗侵略的民族英雄，主持了虎门销烟，被誉为“近代中国开眼看世界的第一人”。"
    }
]

# 当前显示的人物索引
current_index = 0

# ====================== 文字换行函数 ======================
def draw_text_wrap(text, font, max_width):
    lines = []
    current_line = ""
    for char in text:
        # 先尝试加上当前字符
        test_line = current_line + char
        # 渲染测试行，看宽度是否超过限制
        if font.size(test_line)[0] <= max_width:
            current_line = test_line
        else:
            # 超过宽度，就把当前行加入列表，开始新行
            lines.append(current_line)
            current_line = char
    # 把最后一行加上
    if current_line:
        lines.append(current_line)
    return lines

# ====================== 主循环 ======================
clock = pygame.time.Clock()
running = True

while running:
    # 绘制浅蓝色背景
    screen.fill(LIGHT_BLUE)

    # 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # 按键切换人物
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:  # 右键 → 下一个
                current_index = (current_index + 1) % len(characters)
            if event.key == pygame.K_LEFT:   # 左键 → 上一个
                current_index = (current_index - 1) % len(characters)

    # 获取当前人物
    char = characters[current_index]

    # 1. 显示姓名（居中）
    name_surf = font_title.render(char["name"], True, BLUE)
    # 计算居中位置
    name_rect = name_surf.get_rect(center=(WIDTH//2, 100))
    screen.blit(name_surf, name_rect)

    # 2. 显示介绍（自动换行并居中）
    intro_lines = draw_text_wrap(char["intro"], font_text, 650)
    y = 200
    for line in intro_lines:
        line_surf = font_text.render(line, True, BLACK)
        line_rect = line_surf.get_rect(center=(WIDTH//2, y))
        screen.blit(line_surf, line_rect)
        y += 35

    # 3. 显示当前人物序号（比如：1/10）
    index_text = font_text.render(f"{current_index + 1}/{len(characters)}", True, GRAY)
    index_rect = index_text.get_rect(center=(WIDTH//2, 480))
    screen.blit(index_text, index_rect)

    # 4. 提示文字（底部居中）
    tip = font_text.render("← 左键  | 右键 → 切换人物", True, GRAY)
    tip_rect = tip.get_rect(center=(WIDTH//2, 530))
    screen.blit(tip, tip_rect)

    # 更新画面
    pygame.display.flip()
    clock.tick(30)

pygame.quit()
sys.exit()