import pygame
import random
import sys

# 初始化Pygame
pygame.init()

# 窗口设置
WIDTH, HEIGHT = 600, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("石头剪子布 - Pygame版")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (200, 0, 0)
GREEN = (0, 180, 0)
BLUE = (0, 0, 180)
GRAY = (100, 100, 100)
LIGHT_GRAY = (180, 180, 180)

# 解决中文乱码：使用系统自带的中文字体
# Windows用微软雅黑，macOS用苹方，Linux用文泉驿，这里写通用兼容方式
try:
    # 优先使用支持中文的字体
    font_large = pygame.font.SysFont(["SimHei", "Microsoft YaHei", "PingFang SC", "WenQuanYi Zen Hei"], 40)
    font_medium = pygame.font.SysFont(["SimHei", "Microsoft YaHei", "PingFang SC", "WenQuanYi Zen Hei"], 30)
except:
    # 万一找不到，就用默认字体（但中文还是会乱码，所以优先上面的）
    font_large = pygame.font.SysFont(None, 40)
    font_medium = pygame.font.SysFont(None, 30)

# 游戏选项
options = ["石头", "剪刀", "布"]
player_choice = None
computer_choice = None
result = ""
player_score = 0
computer_score = 0

# 按钮区域
rock_btn = pygame.Rect(50, 300, 150, 60)
scissors_btn = pygame.Rect(225, 300, 150, 60)
paper_btn = pygame.Rect(400, 300, 150, 60)

# 判断胜负
def judge(player, computer):
    global player_score, computer_score
    if player == computer:
        return "平局！"
    elif (player == "石头" and computer == "剪刀") or \
         (player == "剪刀" and computer == "布") or \
         (player == "布" and computer == "石头"):
        player_score += 1
        return "你赢啦！"
    else:
        computer_score += 1
        return "电脑赢了！"

# 游戏主循环
running = True
while running:
    screen.fill(WHITE)  # 背景白色

    # 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # 鼠标点击按钮
        if event.type == pygame.MOUSEBUTTONDOWN:
            if rock_btn.collidepoint(event.pos):
                player_choice = "石头"
            elif scissors_btn.collidepoint(event.pos):
                player_choice = "剪刀"
            elif paper_btn.collidepoint(event.pos):
                player_choice = "布"

            # 电脑随机出拳
            if player_choice:
                computer_choice = random.choice(options)
                result = judge(player_choice, computer_choice)

    # ———— 绘制文字 ————
    # 标题
    title = font_large.render("石头 剪子 布", True, BLACK)
    screen.blit(title, (WIDTH//2 - title.get_width()//2, 30))

    # 显示玩家分数
    player_score_text = font_medium.render(f"你的分数: {player_score}", True, BLUE)
    screen.blit(player_score_text, (50, 100))

    # 显示电脑分数
    computer_score_text = font_medium.render(f"电脑分数: {computer_score}", True, RED)
    screen.blit(computer_score_text, (50, 150))

    # 显示玩家选择
    if player_choice:
        player_text = font_medium.render(f"你出了: {player_choice}", True, BLUE)
        screen.blit(player_text, (50, 200))

    # 显示电脑选择
    if computer_choice:
        computer_text = font_medium.render(f"电脑出了: {computer_choice}", True, RED)
        screen.blit(computer_text, (50, 240))

    # 显示结果
    if result:
        result_text = font_large.render(result, True, GREEN)
        screen.blit(result_text, (WIDTH//2 - result_text.get_width()//2, 240))

    # ———— 绘制按钮（加hover效果） ————
    mouse_pos = pygame.mouse.get_pos()

    # 石头按钮
    btn_color = LIGHT_GRAY if rock_btn.collidepoint(mouse_pos) else GRAY
    pygame.draw.rect(screen, btn_color, rock_btn)
    rock_text = font_medium.render("石头", True, WHITE)
    screen.blit(rock_text, (rock_btn.centerx - rock_text.get_width()//2, rock_btn.centery - rock_text.get_height()//2))

    # 剪刀按钮
    btn_color = LIGHT_GRAY if scissors_btn.collidepoint(mouse_pos) else GRAY
    pygame.draw.rect(screen, btn_color, scissors_btn)
    scissors_text = font_medium.render("剪刀", True, WHITE)
    screen.blit(scissors_text, (scissors_btn.centerx - scissors_text.get_width()//2, scissors_btn.centery - scissors_text.get_height()//2))

    # 布按钮
    btn_color = LIGHT_GRAY if paper_btn.collidepoint(mouse_pos) else GRAY
    pygame.draw.rect(screen, btn_color, paper_btn)
    paper_text = font_medium.render("布", True, WHITE)
    screen.blit(paper_text, (paper_btn.centerx - paper_text.get_width()//2, paper_btn.centery - paper_text.get_height()//2))

    pygame.display.update()

# 退出
pygame.quit()
sys.exit()