import pygame
from pygame.locals import *
# 简易翻译字典（内置词汇，联网版本我后面给你方案）
trans_dict = {
    "你好": "hello",
    "早上好": "good morning",
    "谢谢": "thank you",
    "再见": "goodbye",
    "苹果": "apple",
    "小狗": "dog",
    "hello": "你好",
    "good morning": "早上好",
    "thank you": "谢谢",
    "goodbye": "再见",
    "apple": "苹果",
    "dog": "小狗"
}

pygame.init()
WIDTH, HEIGHT = 700, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("简易翻译器")
clock = pygame.time.Clock()

# 字体配置，解决中文显示
try:
    font_input = pygame.font.Font("simhei.ttf", 26)
    font_text = pygame.font.Font("simhei.ttf", 28)
except:
    font_input = pygame.font.SysFont("SimHei", 26)
    font_text = pygame.font.SysFont("SimHei", 28)

# 颜色定义
WHITE = (255, 255, 255)
GRAY = (220, 220, 220)
BLACK = (0, 0, 0)
BLUE = (40, 120, 200)

# 变量
input_text = ""
result_text = "翻译结果在此处显示"
input_rect = pygame.Rect(40, 60, 520, 45)
button1 = pygame.Rect(40, 140, 130, 45)
button2 = pygame.Rect(190, 140, 130, 45)
active = True  # 输入框是否选中

def translate(word):
    """翻译逻辑"""
    if word in trans_dict:
        return trans_dict[word]
    else:
        return "暂无翻译内容"

running = True
while running:
    screen.fill(WHITE)
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
        if event.type == MOUSEBUTTONDOWN:
            if input_rect.collidepoint(event.pos):
                active = True
            else:
                active = False
            # 翻译按钮
            if button1.collidepoint(event.pos):
                result_text = translate(input_text.strip())
            # 清空按钮
            if button2.collidepoint(event.pos):
                input_text = ""
                result_text = "翻译结果在此处显示"

        if event.type == KEYDOWN and active:
            if event.key == K_RETURN:
                result_text = translate(input_text.strip())
            elif event.key == K_BACKSPACE:
                input_text = input_text[:-1]
            else:
                if len(input_text) <= 25:
                    input_text += event.unicode

    # 绘制输入框
    pygame.draw.rect(screen, GRAY if active else WHITE, input_rect, border_radius=8)
    pygame.draw.rect(screen, BLACK, input_rect, 2, border_radius=8)
    input_surface = font_input.render(input_text, True, BLACK)
    screen.blit(input_surface, (input_rect.x + 8, input_rect.y + 5))

    # 绘制按钮
    pygame.draw.rect(screen, BLUE, button1, border_radius=8)
    pygame.draw.rect(screen, (100,100,100), button2, border_radius=8)
    btn1_text = font_input.render("开始翻译", True, WHITE)
    btn2_text = font_input.render("清空", True, WHITE)
    screen.blit(btn1_text, (button1.x + 20, button1.y + 5))
    screen.blit(btn2_text, (button2.x + 40, button2.y + 5))

    # 输出翻译结果
    res_surface = font_text.render(result_text, True, (20,80,150))
    screen.blit(res_surface, (40, 230))

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

pygame.quit()