import pygame
import sys
import random

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 = (200, 200, 200)
RED = (220, 30, 30)
GREEN = (20, 160, 20)
BLUE = (30, 80, 200)

# 直接读取系统宋体绝对路径，不调用SysFont，不会报错且支持中文
try:
    font_big = pygame.font.Font(r"C:\Windows\Fonts\simsun.ttc", 36)
    font_mid = pygame.font.Font(r"C:\Windows\Fonts\simsun.ttc", 28)
    font_small = pygame.font.Font(r"C:\Windows\Fonts\simsun.ttc", 22)
except:
    # 万一路径失效兜底
    font_big = pygame.font.Font(None, 36)
    font_mid = pygame.font.Font(None, 28)
    font_small = pygame.font.Font(None, 22)

# 成语库
idiom_list = [
    "一帆风顺", "二龙戏珠", "三心二意", "四面楚歌", "五湖四海",
    "六神无主", "七上八下", "八面玲珑", "九牛一毛", "十全十美",
    "春暖花开", "暗度陈仓", "卧薪尝胆", "画蛇添足", "指鹿为马",
    "杯弓蛇影", "虎头蛇尾", "走马观花", "花前月下", "下里巴人",
    "人定胜天", "天罗地网", "网开一面", "面如土色", "色胆包天",
    "天经地义", "义薄云天", "天长地久", "久别重逢", "逢凶化吉",
    "吉人天相", "相辅相成", "成家立业", "业精于勤", "勤能补拙"
]

start_idiom = random.choice(idiom_list)
last_char = start_idiom[-1]
input_text = ""
tip_msg = f"游戏开始！请接：{last_char} 开头的成语"
tip_color = BLACK
history = [start_idiom]
clock = pygame.time.Clock()
FPS = 60

input_box = pygame.Rect(150, 420, 500, 50)

def check_idiom(idiom, head_char):
    if len(idiom) != 4:
        return False, "必须输入4字成语"
    if idiom[0] != head_char:
        return False, f"必须以【{head_char}】开头"
    if idiom not in idiom_list:
        return False, "词库暂无该成语"
    if idiom in history:
        return False, "该成语已经用过了"
    return True, "接龙成功！"

running = True
while running:
    screen.fill(WHITE)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                ok, msg = check_idiom(input_text.strip(), last_char)
                tip_msg = msg
                if ok:
                    history.append(input_text.strip())
                    last_char = input_text.strip()[-1]
                    tip_msg = f"正确！下一个接【{last_char}】开头"
                    tip_color = GREEN
                    input_text = ""
                else:
                    tip_color = RED
            elif event.key == pygame.K_BACKSPACE:
                input_text = input_text[:-1]
            elif event.key == pygame.K_ESCAPE:
                input_text = ""
            else:
                if len(input_text) < 4:
                    input_text += event.unicode

    # 绘制所有文字
    title = font_big.render("成语接龙", True, BLUE)
    screen.blit(title, (WIDTH//2 - title.get_width()//2, 40))

    tip_surface = font_mid.render(tip_msg, True, tip_color)
    screen.blit(tip_surface, (80, 120))

    last_text = font_mid.render(f"上一成语：{history[-1]}", True, BLACK)
    screen.blit(last_text, (80, 180))

    his_title = font_small.render("接龙记录：", True, BLACK)
    screen.blit(his_title, (80, 240))
    for idx, item in enumerate(history[-8:]):
        his_txt = font_small.render(item, True, GRAY)
        screen.blit(his_txt, (80 + idx*90, 270))

    pygame.draw.rect(screen, GRAY, input_box, 2)
    input_surface = font_mid.render(input_text, True, BLACK)
    screen.blit(input_surface, (input_box.x + 10, input_box.y + 8))

    help_txt = font_small.render("回车确认 | ESC清空输入 | 关闭窗口退出", True, (80,80,80))
    screen.blit(help_txt, (160, 500))

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

pygame.quit()
sys.exit()