import pygame
import math
import random

# 初始化pygame
pygame.init()

# 窗口设置
WIDTH, HEIGHT = 600, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("抽奖转盘")
clock = pygame.time.Clock()

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 50, 50)
GREEN = (50, 200, 50)
BLUE = (50, 100, 255)
YELLOW = (255, 220, 0)
PURPLE = (180, 80, 220)
ORANGE = (255, 140, 0)

# 转盘奖品配置
prizes = [
    {"name": "一等奖", "color": RED},
    {"name": "谢谢参与", "color": YELLOW},
    {"name": "二等奖", "color": GREEN},
    {"name": "优惠券", "color": BLUE},
    {"name": "三等奖", "color": PURPLE},
    {"name": "再来一次", "color": ORANGE},
]
sector_num = len(prizes)
angle_per_sector = 360 / sector_num

# 转盘参数
center_x, center_y = WIDTH // 2, 300
radius = 240
rotate_angle = 0  # 当前旋转角度
speed = 0         # 旋转速度
is_spinning = False  # 是否正在转动
slowdown_start = 120  # 低于这个速度开始减速

# 字体
font = pygame.font.SysFont("simhei", 24)
result_font = pygame.font.SysFont("simhei", 36)

# 绘制扇形
def draw_sector(start_angle, end_angle, color):
    points = [(center_x, center_y)]
    for angle in range(int(start_angle), int(end_angle) + 1):
        rad = math.radians(angle)
        x = center_x + radius * math.cos(rad)
        y = center_y + radius * math.sin(rad)
        points.append((x, y))
    pygame.draw.polygon(screen, color, points)
    pygame.draw.polygon(screen, BLACK, points, 2)

# 获取当前指针指向的奖品
def get_prize(angle):
    # 指针固定向上，角度换算
    fixed_angle = (360 - angle) % 360
    index = int(fixed_angle // angle_per_sector)
    return prizes[index]["name"]

# 主循环
running = True
prize_result = ""

while running:
    screen.fill(WHITE)

    # 事件监听
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        # 鼠标点击
        if event.type == pygame.MOUSEBUTTONDOWN and not is_spinning:
            mx, my = pygame.mouse.get_pos()
            # 点击开始按钮区域
            if 220 < mx < 380 and 580 < my < 640:
                is_spinning = True
                speed = random.uniform(18, 26)  # 初始随机速度
                prize_result = ""

    # 转盘旋转逻辑
    if is_spinning:
        rotate_angle += speed
        # 减速逻辑
        if speed > 0.3:
            speed -= 0.06
        else:
            speed = 0
            is_spinning = False
            prize_result = get_prize(rotate_angle)

    # 绘制所有扇形区块
    for i in range(sector_num):
        start = rotate_angle + i * angle_per_sector
        end = rotate_angle + (i + 1) * angle_per_sector
        draw_sector(start, end, prizes[i]["color"])
        # 绘制奖品文字
        mid_angle = math.radians(start + angle_per_sector / 2)
        text_r = radius * 0.65
        tx = center_x + text_r * math.cos(mid_angle)
        ty = center_y + text_r * math.sin(mid_angle)
        text_surf = font.render(prizes[i]["name"], True, WHITE)
        rect = text_surf.get_rect(center=(tx, ty))
        screen.blit(text_surf, rect)

    # 绘制转盘外圈
    pygame.draw.circle(screen, BLACK, (center_x, center_y), radius, 4)
    # 指针（顶部箭头）
    pygame.draw.polygon(screen, BLACK, [
        (center_x - 15, center_y - radius + 10),
        (center_x + 15, center_y - radius + 10),
        (center_x, center_y - radius - 10)
    ])

    # 绘制开始按钮
    pygame.draw.rect(screen, RED, (220, 580, 160, 60), border_radius=12)
    btn_text = font.render("点击抽奖", True, WHITE)
    screen.blit(btn_text, btn_text.get_rect(center=(300, 610)))

    # 显示中奖结果
    if prize_result:
        res_text = result_font.render(f"恭喜：{prize_result}", True, RED)
        screen.blit(res_text, res_text.get_rect(center=(WIDTH//2, 670)))

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

pygame.quit()