import pygame
import math
import datetime

# ================== 配置 ==================
WIDTH, HEIGHT = 500, 500
CENTER = (WIDTH // 2, HEIGHT // 2)
RADIUS = 200
FONT_SIZE = 28
BG_COLOR = (15, 20, 30)
CLOCK_COLOR = (25, 35, 55)
TEXT_COLOR = (220, 220, 220)

# ================== 初始化 ==================
pygame.init()

# ✅ 关键：只用 Font(None)，绝不碰 SysFont
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pygame Clock（零坑版）")
clock = pygame.time.Clock()

font = pygame.font.Font(None, FONT_SIZE)
big_font = pygame.font.Font(None, 48)

# ================== 工具函数 ==================
def draw_hand(angle_deg, length, width, color):
    angle_rad = math.radians(angle_deg - 90)
    x = CENTER[0] + math.cos(angle_rad) * length
    y = CENTER[1] + math.sin(angle_rad) * length
    pygame.draw.line(screen, color, CENTER, (x, y), width)

def draw_text(text, pos, big=False):
    f = big_font if big else font
    surf = f.render(text, True, TEXT_COLOR)
    rect = surf.get_rect(center=pos)
    screen.blit(surf, rect)

# ================== 主循环 ==================
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False

    now = datetime.datetime.now()
    sec = now.second + now.microsecond / 1e6
    minute = now.minute + sec / 60
    hour = (now.hour % 12) + minute / 60

    screen.fill(BG_COLOR)

    # 表盘
    pygame.draw.circle(screen, CLOCK_COLOR, CENTER, RADIUS)
    pygame.draw.circle(screen, TEXT_COLOR, CENTER, RADIUS, 2)

    # 刻度
    for i in range(60):
        angle = math.radians(i * 6 - 90)
        r1 = RADIUS - (6 if i % 5 == 0 else 3)
        r2 = RADIUS - 2
        x1 = CENTER[0] + math.cos(angle) * r1
        y1 = CENTER[1] + math.sin(angle) * r1
        x2 = CENTER[0] + math.cos(angle) * r2
        y2 = CENTER[1] + math.sin(angle) * r2
        pygame.draw.line(screen, TEXT_COLOR, (x1, y1), (x2, y2), 2 if i % 5 == 0 else 1)

    # 数字
    for i in range(1, 13):
        angle = math.radians(i * 30 - 90)
        x = CENTER[0] + math.cos(angle) * (RADIUS - 40)
        y = CENTER[1] + math.sin(angle) * (RADIUS - 40)
        draw_text(str(i), (x, y))

    # 指针
    draw_hand(hour * 30, RADIUS * 0.5, 8, (255, 80, 80))
    draw_hand(minute * 6, RADIUS * 0.7, 5, (80, 255, 80))
    draw_hand(sec * 6, RADIUS * 0.85, 2, (255, 220, 50))

    pygame.draw.circle(screen, TEXT_COLOR, CENTER, 6)

    # 日期 & 时间
    date_str = now.strftime("%Y-%m-%d  %a")
    time_str = now.strftime("%H:%M:%S")
    draw_text(date_str, (CENTER[0], CENTER[1] + RADIUS + 30))
    draw_text(time_str, (CENTER[0], CENTER[1] + RADIUS + 65), big=True)

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

pygame.quit()