import pygame
import sys
import math
import random
import os

# 初始化Pygame
pygame.init()

# 窗口设置
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 800
CENTER = (WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2)
RADIUS = 300

# 颜色定义
BACKGROUND_COLOR = (30, 30, 40)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
RED = (255, 50, 50)
GREEN = (50, 255, 50)
BLUE = (50, 150, 255)
ORANGE = (255, 165, 0)
PURPLE = (200, 50, 200)
CYAN = (50, 255, 200)
PINK = (255, 105, 180)
BUTTON_COLOR = (100, 100, 150)
BUTTON_HOVER = (150, 150, 200)

# 奖品配置
PRIZES = ["谢谢参与", "一等奖", "二等奖", "三等奖", "参与奖", "特等奖", "幸运奖", "纪念奖"]
PRIZES_COLORS = [RED, BLUE, GREEN, ORANGE, PURPLE, CYAN, PINK, YELLOW]
ANGLE_PER_PRIZE = 360 / len(PRIZES)


def get_chinese_font(size):
    """获取支持中文的字体"""
    # 尝试多个常见的中文字体路径
    font_paths = [
        "C:/Windows/Fonts/simhei.ttf",  # Windows 黑体
        "C:/Windows/Fonts/msyh.ttc",    # Windows 微软雅黑
        "C:/Windows/Fonts/simsun.ttc",  # Windows 宋体
        "/System/Library/Fonts/PingFang.ttc",  # macOS
        "/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf",  # Linux
    ]

    for path in font_paths:
        if os.path.exists(path):
            try:
                return pygame.font.Font(path, size)
            except:
                continue

    # 如果没有找到中文字体，使用默认字体（可能无法显示中文）
    return pygame.font.Font(None, size)


class Wheel:
    def __init__(self):
        self.rotation_angle = 0
        self.spinning = False
        self.spin_speed = 0
        self.spin_decay = 0.98
        self.result = None
        self.font = get_chinese_font(18)
        self.center_font = get_chinese_font(24)

    def draw(self, screen):
        """绘制转盘"""
        for i, prize in enumerate(PRIZES):
            start_angle = math.radians(
                self.rotation_angle + i * ANGLE_PER_PRIZE)
            end_angle = math.radians(
                self.rotation_angle + (i + 1) * ANGLE_PER_PRIZE)

            # 绘制扇形
            points = [CENTER]
            num_segments = 30
            for j in range(num_segments + 1):
                angle = start_angle + \
                    (end_angle - start_angle) * j / num_segments
                x = CENTER[0] + RADIUS * math.cos(angle)
                y = CENTER[1] + RADIUS * math.sin(angle)
                points.append((x, y))

            pygame.draw.polygon(screen, PRIZES_COLORS[i], points)
            pygame.draw.polygon(screen, BLACK, points, 2)

            # 绘制文字
            mid_angle = start_angle + ANGLE_PER_PRIZE / 2
            text_radius = RADIUS * 0.7
            text_x = CENTER[0] + text_radius * math.cos(mid_angle)
            text_y = CENTER[1] + text_radius * math.sin(mid_angle)

            # 根据文字长度调整显示
            if len(prize) == 4:  # 四个字的奖品
                text_surface1 = self.font.render(prize[:2], True, BLACK)
                text_surface2 = self.font.render(prize[2:], True, BLACK)
                rect1 = text_surface1.get_rect(center=(text_x, text_y - 8))
                rect2 = text_surface2.get_rect(center=(text_x, text_y + 8))
                screen.blit(text_surface1, rect1)
                screen.blit(text_surface2, rect2)
            elif len(prize) == 3:  # 三个字的奖品
                text_surface = self.font.render(prize, True, BLACK)
                text_rect = text_surface.get_rect(center=(text_x, text_y))
                screen.blit(text_surface, text_rect)
            else:  # 其他长度
                text_surface = self.font.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, 60)
        pygame.draw.circle(screen, BLACK, CENTER, 60, 3)

        # 中心文字
        text1 = self.center_font.render("点我", True, BLACK)
        text2 = self.center_font.render("抽奖", True, BLACK)
        rect1 = text1.get_rect(center=(CENTER[0], CENTER[1] - 10))
        rect2 = text2.get_rect(center=(CENTER[0], CENTER[1] + 15))
        screen.blit(text1, rect1)
        screen.blit(text2, rect2)

        # 绘制指针
        pointer_points = [
            (CENTER[0], CENTER[1] - RADIUS - 10),
            (CENTER[0] - 15, CENTER[1] - RADIUS + 25),
            (CENTER[0] + 15, CENTER[1] - RADIUS + 25)
        ]
        pygame.draw.polygon(screen, YELLOW, pointer_points)
        pygame.draw.polygon(screen, BLACK, pointer_points, 2)

    def update(self):
        """更新旋转动画"""
        if self.spinning:
            self.rotation_angle += self.spin_speed
            self.rotation_angle %= 360
            self.spin_speed *= self.spin_decay

            if abs(self.spin_speed) < 0.5:
                self.spinning = False
                self.spin_speed = 0
                self.determine_prize()
                return True
        return False

    def start_spin(self):
        if not self.spinning:
            self.spin_speed = random.uniform(12, 20)
            self.spinning = True
            return True
        return False

    def determine_prize(self):
        # 计算指针指向的奖品
        pointer_angle = 90
        effective_angle = (360 - (self.rotation_angle + pointer_angle)) % 360
        prize_index = int(effective_angle // ANGLE_PER_PRIZE)
        self.result = PRIZES[prize_index % len(PRIZES)]
        return self.result


def main():
    screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    pygame.display.set_caption("转盘抽奖小游戏")
    clock = pygame.time.Clock()

    wheel = Wheel()
    font = get_chinese_font(32)  # 用于显示结果的大字体
    small_font = get_chinese_font(24)  # 用于提示的小字体

    result_text = ""
    result_timer = 0

    running = True
    while running:
        current_time = pygame.time.get_ticks()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                # 检查是否点击了开始抽奖按钮
                button_rect = pygame.Rect(
                    WINDOW_WIDTH // 2 - 80, WINDOW_HEIGHT - 100, 160, 50)
                # 也可以点击中心圆抽奖
                center_circle = pygame.Rect(
                    CENTER[0] - 60, CENTER[1] - 60, 120, 120)

                if (button_rect.collidepoint(mouse_pos) or center_circle.collidepoint(mouse_pos)) and not wheel.spinning:
                    wheel.start_spin()
                    result_text = ""

        # 更新转盘
        spin_complete = wheel.update()
        if spin_complete and wheel.result:
            result_text = f"恭喜获得：{wheel.result}"
            result_timer = current_time

        # 绘制
        screen.fill(BACKGROUND_COLOR)
        wheel.draw(screen)

        # 绘制按钮
        button_rect = pygame.Rect(
            WINDOW_WIDTH // 2 - 80, WINDOW_HEIGHT - 100, 160, 50)
        mouse_pos = pygame.mouse.get_pos()
        if button_rect.collidepoint(mouse_pos):
            pygame.draw.rect(screen, BUTTON_HOVER, button_rect)
        else:
            pygame.draw.rect(screen, BUTTON_COLOR, button_rect)
        pygame.draw.rect(screen, BLACK, button_rect, 3)

        button_text = font.render("开始抽奖", True, BLACK)
        button_text_rect = button_text.get_rect(center=button_rect.center)
        screen.blit(button_text, button_text_rect)

        # 显示结果
        if result_text and current_time - result_timer < 3000:  # 显示3秒
            # 添加半透明背景
            result_surface = font.render(result_text, True, YELLOW)
            result_rect = result_surface.get_rect(
                center=(WINDOW_WIDTH // 2, 100))

            # 添加背景框
            bg_rect = result_rect.inflate(20, 10)
            pygame.draw.rect(screen, (0, 0, 0, 128), bg_rect)
            pygame.draw.rect(screen, BLACK, bg_rect, 2)
            screen.blit(result_surface, result_rect)

        # 显示提示
        if not wheel.spinning and not result_text:
            tip_text1 = small_font.render("点击「开始抽奖」或中心圆盘旋转转盘", True, WHITE)
            tip_text2 = small_font.render("等待转盘停止即可获得奖品", True, WHITE)
            rect1 = tip_text1.get_rect(
                center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT - 70))
            rect2 = tip_text2.get_rect(
                center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT - 40))
            screen.blit(tip_text1, rect1)
            screen.blit(tip_text2, rect2)

        pygame.display.flip()
        clock.tick(60)

    pygame.quit()
    sys.exit()


if __name__ == "__main__":
    main()
