import pygame
import sys

# 初始化pygame
pygame.init()
WIDTH, HEIGHT = 500, 320
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("温度单位转换器")

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

# 字体
font_big = pygame.font.SysFont("simhei", 32)
font_text = pygame.font.SysFont("simhei", 24)

# 程序变量
input_text = ""
result_text = ""
error_msg = ""
active_input = True  # 输入框激活状态

# 按钮区域
btn_c2f = pygame.Rect(60, 180, 160, 50)
btn_f2c = pygame.Rect(280, 180, 160, 50)
input_box = pygame.Rect(60, 100, 380, 45)

clock = pygame.time.Clock()

def celsius_to_fahrenheit(c):
    return c * 1.8 + 32

def fahrenheit_to_celsius(f):
    return (f - 32) / 1.8

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_box.collidepoint(event.pos):
                active_input = True
            else:
                active_input = False

            # 摄氏转华氏
            if btn_c2f.collidepoint(event.pos):
                try:
                    val = float(input_text)
                    res = celsius_to_fahrenheit(val)
                    result_text = f"{val} ℃ = {res:.2f} ℉"
                    error_msg = ""
                except ValueError:
                    error_msg = "请输入有效的数字！"
                    result_text = ""

            # 华氏转摄氏
            if btn_f2c.collidepoint(event.pos):
                try:
                    val = float(input_text)
                    res = fahrenheit_to_celsius(val)
                    result_text = f"{val} ℉ = {res:.2f} ℃"
                    error_msg = ""
                except ValueError:
                    error_msg = "请输入有效的数字！"
                    result_text = ""

        # 键盘输入
        if event.type == pygame.KEYDOWN and active_input:
            if event.key == pygame.K_BACKSPACE:
                input_text = input_text[:-1]
            elif event.key == pygame.K_RETURN:
                pass
            else:
                # 只允许数字、小数点、负号
                char = event.unicode
                if char in "0123456789.-":
                    input_text += char

    # 绘制标题
    title = font_big.render("温度单位转换器", True, BLACK)
    screen.blit(title, (WIDTH//2 - title.get_width()//2, 25))

    # 绘制输入框
    pygame.draw.rect(screen, GRAY, input_box, 2)
    text_surface = font_text.render(input_text, True, BLACK)
    screen.blit(text_surface, (input_box.x + 8, input_box.y + 6))

    # 绘制按钮
    pygame.draw.rect(screen, BLUE, btn_c2f)
    pygame.draw.rect(screen, BLUE, btn_f2c)
    text_c2f = font_text.render("℃ → ℉", True, WHITE)
    text_f2c = font_text.render("℉ → ℃", True, WHITE)
    screen.blit(text_c2f, (btn_c2f.centerx - text_c2f.get_width()//2, btn_c2f.y + 10))
    screen.blit(text_f2c, (btn_f2c.centerx - text_f2c.get_width()//2, btn_f2c.y + 10))

    # 输出结果
    if result_text:
        res_surface = font_text.render(result_text, True, BLACK)
        screen.blit(res_surface, (60, 250))
    if error_msg:
        err_surface = font_text.render(error_msg, True, RED)
        screen.blit(err_surface, (60, 250))

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