import pygame
import random
import sys

pygame.init()
# 开启文字输入，支持中文输入法
pygame.key.start_text_input()

WIDTH, HEIGHT = 750, 620
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("脑筋急转弯｜中文输入修复版")

# 颜色
WHITE = (255, 255, 255)
BLACK = (30, 30, 30)
BLUE = (40, 120, 200)
GREEN = (30, 160, 80)
GRAY = (180, 180, 180)
LIGHT_GRAY = (240, 240, 240)
RED = (200, 40, 40)

# 字体优先微软雅黑，没有自动回退simhei
try:
    font_small = pygame.font.SysFont("Microsoft YaHei", 22)
    font_mid = pygame.font.SysFont("Microsoft YaHei", 26)
    font_big = pygame.font.SysFont("Microsoft YaHei", 34)
except:
    font_small = pygame.font.SysFont("simhei", 22)
    font_mid = pygame.font.SysFont("simhei", 26)
    font_big = pygame.font.SysFont("simhei", 34)

# 题库
question_list = [
    {"q": "什么门永远关不上？", "a": "球门"},
    {"q": "什么东西越洗越脏？", "a": "水"},
    {"q": "什么球离你最近？", "a": "眼球"},
    {"q": "什么动物最容易摔倒？", "a": "狐狸，因为它很狡猾（脚滑）"},
    {"q": "什么书里毛病最多？", "a": "医学书"},
    {"q": "打破了什么东西不用赔？", "a": "打破纪录"},
    {"q": "什么路人们最不敢走？", "a": "绝路"},
    {"q": "什么瓜不能吃？", "a": "傻瓜"},
    {"q": "一年四季都盛开的花是什么花？", "a": "塑料花"},
    {"q": "什么东西有五个头，但大家不觉得奇怪？", "a": "手和脚"},
    {"q": "什么动物你打死了它，却流了你的血？", "a": "蚊子"},
    {"q": "什么东西有四条腿却不会走路？", "a": "桌子"},
    {"q": "什么布剪不断？", "a": "瀑布"},
    {"q": "什么动物最喜欢贴在墙上？", "a": "海豹（海报）"},
    {"q": "什么水永远用不完？", "a": "泪水"},
    {"q": "什么东西当你说到它的名字，就把它打破了？", "a": "沉默"},
    {"q": "太平洋的中间是什么？", "a": "是平字"},
    {"q": "用什么可以解开所有的谜？", "a": "答案"},
    {"q": "什么人始终不敢洗澡？", "a": "泥人"},
    {"q": "什么车子寸步难行？", "a": "风车"},
    {"q": "什么酒不能喝？", "a": "碘酒"},
    {"q": "什么蛋打不烂，煮不熟，更不能吃？", "a": "考试得的零蛋"},
    {"q": "什么东西越生气，它就越大？", "a": "脾气"},
    {"q": "从来没见过的爷爷他是什么爷爷？", "a": "老天爷"},
    {"q": "什么东西没吃的时候是绿的，吃的时候是红的，吐出来的是黑的？", "a": "西瓜"}
]

current_item = random.choice(question_list)
show_answer = False
user_input = ""
input_active = False
result_tip = ""
clock = pygame.time.Clock()

class Button:
    def __init__(self, x, y, w, h, text, color):
        self.rect = pygame.Rect(x, y, w, h)
        self.text = text
        self.color = color
    def draw(self):
        pygame.draw.rect(screen, self.color, self.rect, border_radius=8)
        pygame.draw.rect(screen, BLACK, self.rect, 2, border_radius=8)
        ts = font_mid.render(self.text, True, WHITE)
        tr = ts.get_rect(center=self.rect.center)
        screen.blit(ts, tr)
    def check_click(self, pos):
        return self.rect.collidepoint(pos)

btn_show = Button(60, 520, 170, 50, "显示答案", BLUE)
btn_next = Button(520, 520, 170, 50, "下一题", GREEN)
input_box = pygame.Rect(60, 360, 630, 70)

running = True
while running:
    screen.fill(LIGHT_GRAY)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            if input_box.collidepoint(event.pos):
                input_active = True
            else:
                input_active = False
            if btn_show.check_click(event.pos):
                show_answer = True
            if btn_next.check_click(event.pos):
                current_item = random.choice(question_list)
                show_answer = False
                user_input = ""
                result_tip = ""
        # ✅ 重点：TEXTINPUT 专门接收中文
        if event.type == pygame.TEXTINPUT:
            if input_active:
                user_input += event.text
        # 退格、回车
        if event.type == pygame.KEYDOWN:
            if input_active:
                if event.key == pygame.K_BACKSPACE:
                    user_input = user_input[:-1]
                elif event.key == pygame.K_RETURN:
                    # 宽松判断：去掉空格，包含关键词就算对
                    my_ans = user_input.strip()
                    right_ans = current_item["a"].strip()
                    if my_ans in right_ans or right_ans in my_ans:
                        result_tip = "✅ 答对啦！真棒！"
                    else:
                        result_tip = "❌ 答错咯，看看标准答案吧"
                    show_answer = True

    # 标题
    title = font_big.render("🧠 脑筋急转弯", True, BLACK)
    screen.blit(title, title.get_rect(center=(WIDTH//2, 40)))

    # 题目框
    q_box = pygame.Rect(40, 90, 670, 130)
    pygame.draw.rect(screen, WHITE, q_box, border_radius=10)
    pygame.draw.rect(screen, GRAY, q_box, 2, border_radius=10)
    q_text = f"题目：{current_item['q']}"
    q_surf = font_mid.render(q_text, True, BLACK)
    screen.blit(q_surf, (60, 120))

    # 标准答案框
    ans_box = pygame.Rect(40, 250, 670, 80)
    pygame.draw.rect(screen, WHITE, ans_box, border_radius=10)
    pygame.draw.rect(screen, GRAY, ans_box, 2, border_radius=10)
    if show_answer:
        ans_text = f"标准答案：{current_item['a']}"
    else:
        ans_text = "标准答案：暂未显示"
    ans_surf = font_mid.render(ans_text, True, RED)
    screen.blit(ans_surf, (60,275))

    # 输入框
    line_color = (100,180,255) if input_active else GRAY
    pygame.draw.rect(screen, WHITE, input_box, border_radius=8)
    pygame.draw.rect(screen, line_color, input_box, 3, border_radius=8)
    display_text = "你的答案：" + user_input
    input_surf = font_mid.render(display_text, True, BLACK)
    screen.blit(input_surf, (input_box.x + 15, input_box.y + 18))

    # 对错提示
    if result_tip:
        tip_color = GREEN if "答对" in result_tip else RED
        res_surf = font_big.render(result_tip, True, tip_color)
        screen.blit(res_surf, res_surf.get_rect(center=(WIDTH//2, 460)))

    small_tip = font_small.render("点击输入框激活，拼音输入中文，输完回车判对错", True, (70,70,70))
    screen.blit(small_tip, small_tip.get_rect(center=(WIDTH//2, 495)))

    btn_show.draw()
    btn_next.draw()

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

pygame.key.stop_text_input()
pygame.quit()
sys.exit()
