import pygame
import random
import sys

# --- 初始化 Pygame ---
pygame.init()

# --- 常量定义 ---
WIDTH, HEIGHT = 800, 600
WHITE = (255, 255, 255)
BLACK = (30, 30, 30)
GRAY = (200, 200, 200)
HOVER_COLOR = (180, 180, 180)
BLUE = (0, 120, 215) # 类似系统按钮蓝

# 字体设置 (尝试加载系统默认字体，防止中文乱码)
try:
    # Windows通常有微软雅黑，Mac有PingFang，Linux这里回退到freesansbold
    FONT_PATH = "msyh.ttc" 
    font_large = pygame.font.Font(FONT_PATH, 60)
    font_medium = pygame.font.Font(FONT_PATH, 30)
    font_small = pygame.font.Font(FONT_PATH, 20)
except:
    # 如果找不到中文字体，回退到默认字体（中文可能显示为方块，建议放入一个.ttf文件）
    font_large = pygame.font.Font(None, 74)
    font_medium = pygame.font.Font(None, 36)
    font_small = pygame.font.Font(None, 24)

# 设置屏幕
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pygame 石头剪刀布")
clock = pygame.time.Clock()

# --- 游戏状态 ---
choices = ["石头", "剪刀", "布"]
player_choice = None
computer_choice = None
result_text = "请出拳！"
result_color = BLACK
game_active = True # 是否处于等待出拳状态

# --- 按钮类 ---
class Button:
    def __init__(self, x, y, width, height, text, color, text_color):
        self.rect = pygame.Rect(x, y, width, height)
        self.text = text
        self.color = color
        self.text_color = text_color
        self.is_hovered = False

    def draw(self, surface):
        # 鼠标悬停效果
        mouse_pos = pygame.mouse.get_pos()
        if self.rect.collidepoint(mouse_pos):
            current_color = HOVER_COLOR
            self.is_hovered = True
        else:
            current_color = self.color
            self.is_hovered = False

        # 绘制按钮背景
        pygame.draw.rect(surface, current_color, self.rect, border_radius=10)
        pygame.draw.rect(surface, BLACK, self.rect, 2, border_radius=10) # 边框

        # 绘制文字
        text_surf = font_medium.render(self.text, True, self.text_color)
        text_rect = text_surf.get_rect(center=self.rect.center)
        surface.blit(text_surf, text_rect)

    def is_clicked(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            if self.rect.collidepoint(event.pos):
                return True
        return False

# --- 创建按钮实例 ---
# 居中排列按钮的计算
btn_width, btn_height = 120, 50
start_x = (WIDTH - (btn_width * 3 + 40)) / 2 # 40是两个间距的总和
btn_y = 450

btn_rock = Button(start_x, btn_y, btn_width, btn_height, "石头", GRAY, BLACK)
btn_scissors = Button(start_x + btn_width + 20, btn_y, btn_width, btn_height, "剪刀", GRAY, BLACK)
btn_paper = Button(start_x + (btn_width + 20) * 2, btn_y, btn_width, btn_height, "布", GRAY, BLACK)
btn_restart = Button(WIDTH//2 - 60, HEIGHT - 80, 120, 40, "再来一局", BLUE, WHITE)

# --- 主循环 ---
running = True
while running:
    # 1. 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        
        # 游戏逻辑处理
        if game_active:
            if btn_rock.is_clicked(event):
                player_choice = "石头"
                game_active = False
            elif btn_scissors.is_clicked(event):
                player_choice = "剪刀"
                game_active = False
            elif btn_paper.is_clicked(event):
                player_choice = "布"
                game_active = False
        else:
            # 游戏结束后，点击“再来一局”重置
            if btn_restart.is_clicked(event):
                player_choice = None
                computer_choice = None
                result_text = "请出拳！"
                result_color = BLACK
                game_active = True

    # 2. 游戏逻辑运算 (当玩家出拳后)
    if not game_active and player_choice and not computer_choice:
        # 模拟电脑思考时间（简单的延迟效果，这里直接计算）
        computer_choice = random.choice(choices)
        
        # 判定胜负
        if player_choice == computer_choice:
            result_text = "平局！"
            result_color = BLACK
        elif (player_choice == "石头" and computer_choice == "剪刀") or \
             (player_choice == "剪刀" and computer_choice == "布") or \
             (player_choice == "布" and computer_choice == "石头"):
            result_text = "你赢了！"
            result_color = BLUE
        else:
            result_text = "电脑赢了！"
            result_color = (200, 50, 50) # 深红色

    # 3. 绘图渲染
    screen.fill(WHITE) # 背景色

    # 标题
    title_text = font_large.render("石头 剪刀 布", True, BLACK)
    screen.blit(title_text, (WIDTH//2 - title_text.get_width()//2, 50))

    # 玩家选择显示
    p_text = f"玩家: {player_choice if player_choice else '...'}"
    p_surf = font_medium.render(p_text, True, BLACK)
    screen.blit(p_surf, (WIDTH//4 - p_surf.get_width()//2, 250))

    # 电脑选择显示
    c_text = f"电脑: {computer_choice if computer_choice else '...'}"
    c_surf = font_medium.render(c_text, True, BLACK)
    screen.blit(c_surf, (3*WIDTH//4 - c_surf.get_width()//2, 250))

    # 结果显示
    res_surf = font_large.render(result_text, True, result_color)
    screen.blit(res_surf, (WIDTH//2 - res_surf.get_width()//2, 350))

    # 绘制按钮
    if game_active:
        btn_rock.draw(screen)
        btn_scissors.draw(screen)
        btn_paper.draw(screen)
    else:
        btn_restart.draw(screen)

    # 更新屏幕
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
sys.exit()