import pygame
import math
import sys

# 奖品
PRIZES = [
    "皇家马车",
    "王权之心",
    "24Kg纯金蛋仔",
    "帝王翡翠",
    "食神",
    "国王权杖",
    "骑士团圣剑",
    "没中"
]

COLORS = [
    (255,50,50),
    (50,160,255),
    (255,210,50),
    (50,255,100),
    (180,100,255),
    (255,160,50),
    (50,255,210),
    (255,100,160)
]

WIDTH = 600
HEIGHT = 650
CX = WIDTH//2
CY = 270
R = 210

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("精准转盘 - 绝不跑偏")
clock = pygame.time.Clock()

font = pygame.font.SysFont("SimHei", 28)
font_s = pygame.font.SysFont("SimHei", 20)

btn_rect = pygame.Rect(CX-100, 560, 200, 60)

spinning = False
angle = 0
speed = 0
res = ""
num = len(PRIZES)
per_angle = 360 / num

while True:
    screen.fill((240,240,240))

    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if e.type == pygame.MOUSEBUTTONDOWN:
            if btn_rect.collidepoint(e.pos) and not spinning:
                spinning = True
                speed = -30
                res = ""

    if spinning:
        angle += speed
        speed *= 0.95

        if abs(speed) < 0.2:
            spinning = False
            a = angle % 360
            idx = int((270 - a) / per_angle) % num
            res = PRIZES[idx]

    for i in range(num):
        a1 = math.radians(i * per_angle + angle)
        a2 = math.radians((i+1)*per_angle + angle)
        pts = [(CX, CY)]
        for t in range(21):
            aa = a1 + (a2 - a1) * t / 20
            x = CX + R * math.cos(aa)
            y = CY + R * math.sin(aa)
            pts.append((x,y))
        pygame.draw.polygon(screen, COLORS[i], pts)

    pygame.draw.circle(screen, (0,0,0), (CX,CY), R, 3)
    pygame.draw.circle(screen, (255,255,255), (CX,CY), 15)

    for i in range(num):
        mid = math.radians(i * per_angle + per_angle/2 + angle)
        x = CX + (R-42) * math.cos(mid)
        y = CY + (R-42) * math.sin(mid)
        txt = font_s.render(PRIZES[i], True, (0,0,0))
        screen.blit(txt, txt.get_rect(center=(x,y)))

    pygame.draw.polygon(screen, (255,0,0), [
        (CX-12, CY - R - 5),
        (CX+12, CY - R - 5),
        (CX,    CY - R + 15)
    ])

    c = (80,180,255) if not spinning else (150,150,150)
    pygame.draw.rect(screen, c, btn_rect, border_radius=10)
    pygame.draw.rect(screen, (0,0,0), btn_rect, 2, border_radius=10)
    t = font.render("开始抽奖", True, (0,0,0))
    screen.blit(t, t.get_rect(center=btn_rect.center))

    if res:
        t = font.render("抽到：" + res, True, (255,0,0))
        screen.blit(t, t.get_rect(center=(CX, 510)))

    pygame.display.flip()
    clock.tick(60)
    "皇家马车",
    "王权之心",
    "24Kg纯金蛋仔",
    "帝王翡翠",
    "食神",
    "国王权杖",
    "骑士团圣剑",
    "没中"