import pygame
import math
import random
import sys
import os

# --- 1. 初始化与字体设置 ---
pygame.init()
pygame.mixer.init()

# 自动寻找系统中文字体 (以 Windows 微软雅黑为例)
def get_chinese_font(size):
    # 常见的中文字体路径
    font_paths = [
        "C:/Windows/Fonts/msyh.ttc",      # 微软雅黑
        "C:/Windows/Fonts/simhei.ttf",     # 黑体
        "C:/Windows/Fonts/simsun.ttc",     # 宋体
        "/System/Library/Fonts/PingFang.ttc", # Mac 苹方
        "/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc" # Linux 文泉驿
    ]
    for path in font_paths:
        if os.path.exists(path):
            return pygame.font.Font(path, size)
    # 如果都没找到，回退到默认字体
    return pygame.font.Font(None, size)

# 准备不同大小的字体
font_title = get_chinese_font(48)
font_prize = get_chinese_font(32)
font_hint = get_chinese_font(28)

# --- 2. 窗口与颜色设置 ---
WIDTH, HEIGHT = 600, 650  # 稍微加高一点，用来放标题
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(" 中文幸运大转盘")

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 50, 50)
COLORS = [(255, 100, 100), (255, 200, 100), (100, 255, 100), (100, 200, 255), (200, 100, 255)]

# --- 3. 奖项配置 ---
PRIZES = [
    ("一等奖 ", 5),
    ("二等奖 ", 15),
    ("三等奖 ", 30),
    ("谢谢参与 ", 30),
    ("再来一次 ", 20),
]

center = (WIDTH // 2, HEIGHT // 2 + 25)  # 转盘中心稍微下移，给标题留位置
radius = 200

# --- 4. 游戏状态 ---
angle = 0
speed = 0
spinning = False
target_prize = ""
last_tick_angle = 0

# --- 5. 核心绘制函数 ---
def draw_turntable(current_angle):
    num_prizes = len(PRIZES)
    sector_angle = 360 / num_prizes
    
    for i, (prize, _) in enumerate(PRIZES):
        start_angle = math.radians(current_angle + i * sector_angle)
        end_angle = math.radians(current_angle + (i + 1) * sector_angle)
        
        points = [center]
        for j in range(10):
            a = start_angle + (end_angle - start_angle) * j / 9
            points.append((center[0] + radius * math.cos(a), center[1] + radius * math.sin(a)))
        pygame.draw.polygon(screen, COLORS[i % len(COLORS)], points)
        
        # 绘制中文奖项文字
        text_angle = start_angle + (end_angle - start_angle) / 2
        text_x = center[0] + (radius * 0.6) * math.cos(text_angle)
        text_y = center[1] + (radius * 0.6) * math.sin(text_angle)
        text_surface = font_prize.render(prize, True, BLACK)
        text_rect = text_surface.get_rect(center=(text_x, text_y))
        screen.blit(text_surface, text_rect)

    pygame.draw.circle(screen, WHITE, center, 30)
    pygame.draw.circle(screen, BLACK, center, 30, 2)

def draw_pointer():
    pointer_pos = [
        (center[0], center[1] - radius - 20),
        (center[0] - 15, center[1] - radius + 10),
        (center[0] + 15, center[1] - radius + 10),
    ]
    pygame.draw.polygon(screen, RED, pointer_pos)

# --- 6. 主循环 ---
clock = pygame.time.Clock()
running = True

while running:
    screen.fill(WHITE)
    
    # 绘制标题
    title_surface = font_title.render(" 幸运大转盘 ", True, BLACK)
    title_rect = title_surface.get_rect(center=(WIDTH // 2, 40))
    screen.blit(title_surface, title_rect)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN and not spinning:
            spinning = True
            target_prize = random.choices([p[0] for p in PRIZES], weights=[p[1] for p in PRIZES])[0]
            speed = random.uniform(20, 30)
            last_tick_angle = angle

    if spinning:
        angle += speed
        speed *= 0.97
        
        sector_angle = 360 / len(PRIZES)
        if abs(angle - last_tick_angle) >= sector_angle:
            last_tick_angle = angle
            
        if speed < 0.1:
            spinning = False
            speed = 0

    draw_turntable(angle)
    draw_pointer()
    
    # 绘制提示文字或结果
    if not spinning and speed == 0 and target_prize:
        result_surface = font_hint.render(f" 恭喜获得: {target_prize}", True, RED)
    else:
        result_surface = font_hint.render(" 点击屏幕开始抽奖", True, BLACK)
        
    result_rect = result_surface.get_rect(center=(WIDTH // 2, HEIGHT - 40))
    screen.blit(result_surface, result_rect)
        
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
sys.exit()