import pygame

# 初始化pygame
pygame.init()
WIDTH, HEIGHT = 700, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("英语单词背诵器")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (30, 90, 180)

# 加载字体，兼容中英文
try:
    word_font = pygame.font.Font("simhei.ttf", 45)
    mean_font = pygame.font.Font("simhei.ttf", 35)
except:
    word_font = pygame.font.SysFont("Arial", 45)
    mean_font = pygame.font.SysFont("Arial", 35)

# 单词库，可以自由添加
word_list = [
    {"en": "apple", "cn": "苹果"},
    {"en": "banana", "cn": "香蕉"},
    {"en": "beautiful", "cn": "美丽的"},
    {"en": "happiness", "cn": "幸福"},
    {"en": "journey", "cn": "旅行"},
    {"en": "opportunity", "cn": "机会"}
]

index = 0
show_chinese = False  # 默认隐藏中文释义
clock = pygame.time.Clock()
running = True

while running:
    screen.fill(WHITE)
    current_word = word_list[index]

    # 绘制英文单词
    en_text = word_font.render(current_word["en"], True, BLUE)
    screen.blit(en_text, ((WIDTH - en_text.get_width()) // 2, 100))

    # 判断是否展示中文
    if show_chinese:
        cn_text = mean_font.render(current_word["cn"], True, BLACK)
        screen.blit(cn_text, ((WIDTH - cn_text.get_width()) // 2, 180))
    else:
        tip_text = mean_font.render("按下空格查看释义", True, (120, 120, 120))
        screen.blit(tip_text, ((WIDTH - tip_text.get_width()) // 2, 180))

    # 底部操作提示
    info_font = pygame.font.SysFont("Arial", 22)
    info = info_font.render("← 上一个单词    → 下一个单词", True, (80, 80, 80))
    screen.blit(info, ((WIDTH - info.get_width()) // 2, 300))
    page_text = info_font.render(f"{index + 1}/{len(word_list)}", True, BLACK)
    screen.blit(page_text, (30, 350))

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

        if event.type == pygame.KEYDOWN:
            # 上一个单词
            if event.key == pygame.K_LEFT:
                index -= 1
                if index < 0:
                    index = len(word_list) - 1
                show_chinese = False
            # 下一个单词
            elif event.key == pygame.K_RIGHT:
                index += 1
                if index >= len(word_list):
                    index = 0
                show_chinese = False
            # 空格显示释义
            elif event.key == pygame.K_SPACE:
                show_chinese = True

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

pygame.quit()