import pygame
import sys

# 初始化Pygame
pygame.init()

# 窗口设置
WIDTH, HEIGHT = 450, 300
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("温度单位转换器")

# 颜色定义
WHITE = (255, 255, 255)
GRAY = (230, 230, 230)
BLUE = (50, 150, 255)
BLACK = (0, 0, 0)
RED = (255, 60, 60)

# =============== 修复中文核心代码 ===============
# 加载支持中文的系统字体（Windows/mac/Linux 通用）
def get_chinese_font(size):
    try:
        # Windows 常用中文字体
        return pygame.font.SysFont("Microsoft YaHei", size)
    except:
        try:
            # macOS 常用中文字体
            return pygame.font.SysFont("PingFang SC", size)
        except:
            try:
                # Linux 常用中文字体
                return pygame.font.SysFont("WenQuanYi Micro Hei", size)
            except:
                # 兜底方案
                return pygame.font.SysFont(None, size)

font = get_chinese_font(36)       # 主字体
small_font = get_chinese_font(28) # 小字体
# ================================================

# 输入框与按钮区域
input_rect = pygame.Rect(100, 80, 250, 40)
btn_c_to_f = pygame.Rect(80, 150, 130, 45)
btn_f_to_c = pygame.Rect(240, 150, 130, 45)

# 状态变量
user_input = ""
result = ""
active = True  # 输入框激活

# 主循环
clock = pygame.time.Clock()
while True:
    screen.fill(WHITE)

    # 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        # 鼠标点击
        if event.type == pygame.MOUSEBUTTONDOWN:
            if input_rect.collidepoint(event.pos):
                active = True
            else:
                active = False

            # 摄氏度 → 华氏度
            if btn_c_to_f.collidepoint(event.pos):
                try:
                    c = float(user_input)
                    f = c * 9 / 5 + 32
                    result = f"{c:.1f}℃ = {f:.1f}℉"
                except:
                    result = "请输入有效数字"

            # 华氏度 → 摄氏度
            if btn_f_to_c.collidepoint(event.pos):
                try:
                    f = float(user_input)
                    c = (f - 32) * 5 / 9
                    result = f"{f:.1f}℉ = {c:.1f}℃"
                except:
                    result = "请输入有效数字"

        # 键盘输入
        if event.type == pygame.KEYDOWN:
            if active:
                if event.key == pygame.K_BACKSPACE:
                    user_input = user_input[:-1]
                else:
                    if event.unicode in "-0123456789.":
                        user_input += event.unicode

    # ———— 绘制界面 ————
    title = font.render("温度单位转换器", True, BLACK)
    screen.blit(title, (WIDTH//2 - title.get_width()//2, 20))

    # 输入框
    pygame.draw.rect(screen, GRAY if active else WHITE, input_rect, 2, 5)
    text_surf = font.render(user_input, True, BLACK)
    screen.blit(text_surf, (input_rect.x + 8, input_rect.y + 5))

    # 按钮
    pygame.draw.rect(screen, BLUE, btn_c_to_f, border_radius=8)
    pygame.draw.rect(screen, RED, btn_f_to_c, border_radius=8)

    btn1_text = small_font.render("℃ → ℉", True, WHITE)
    btn2_text = small_font.render("℉ → ℃", True, WHITE)
    screen.blit(btn1_text, (btn_c_to_f.centerx - btn1_text.get_width()//2, btn_c_to_f.centery - 10))
    screen.blit(btn2_text, (btn_f_to_c.centerx - btn2_text.get_width()//2, btn_f_to_c.centery - 10))

    # 结果显示
    result_surf = font.render(result, True, BLACK)
    screen.blit(result_surf, (WIDTH//2 - result_surf.get_width()//2, 220))

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