import pygame
import random

# 初始化pygame
pygame.init()

# 窗口设置
WIDTH, HEIGHT = 600, 450
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Rock Paper Scissors")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (180, 180, 180)
BLUE = (30, 144, 255)
RED = (220, 50, 50)
GREEN = (34, 139, 34)

# 使用内置默认字体，避开SysFont的bug
font_big = pygame.font.Font(None, 40)
font_mid = pygame.font.Font(None, 28)
font_small = pygame.font.Font(None, 22)

# 选项
choices = ["Rock", "Scissors", "Paper"]
player_choice = ""
ai_choice = ""
result_text = "Please select: Rock / Scissors / Paper"
score_player = 0
score_ai = 0

# 按钮区域
btn_stone = pygame.Rect(50, 320, 140, 70)
btn_scissors = pygame.Rect(230, 320, 140, 70)
btn_paper = pygame.Rect(410, 320, 140, 70)

def get_result(player, ai):
    """判断胜负逻辑"""
    if player == ai:
        return "Draw"
    if (player == "Rock" and ai == "Scissors") or \
       (player == "Scissors" and ai == "Paper") or \
       (player == "Paper" and ai == "Rock"):
        return "You Win!"
    else:
        return "AI Win!"

def draw_ui():
    screen.fill(WHITE)

    # 分数显示
    score_surface = font_mid.render(f"Your Score: {score_player}   AI Score: {score_ai}", True, BLACK)
    screen.blit(score_surface, (30, 20))

    # 玩家和AI选择
    p_text = font_big.render(f"You: {player_choice}", True, BLUE)
    screen.blit(p_text, (80, 100))

    ai_text = font_big.render(f"AI: {ai_choice}", True, RED)
    screen.blit(ai_text, (320, 100))

    # 对局结果
    res_text = font_mid.render(result_text, True, GREEN)
    screen.blit(res_text, (160, 180))

    # 绘制三个按钮
    pygame.draw.rect(screen, GRAY, btn_stone, border_radius=8)
    pygame.draw.rect(screen, GRAY, btn_scissors, border_radius=8)
    pygame.draw.rect(screen, GRAY, btn_paper, border_radius=8)

    t1 = font_mid.render("Rock", True, BLACK)
    t2 = font_mid.render("Scissors", True, BLACK)
    t3 = font_mid.render("Paper", True, BLACK)
    screen.blit(t1, (btn_stone.x + 45, btn_stone.y + 15))
    screen.blit(t2, (btn_scissors.x + 35, btn_scissors.y + 15))
    screen.blit(t3, (btn_paper.x + 45, btn_paper.y + 15))

    pygame.display.update()

# 主循环
running = True
while running:
    draw_ui()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # 鼠标点击事件
        if event.type == pygame.MOUSEBUTTONDOWN:
            mx, my = pygame.mouse.get_pos()
            select = None
            if btn_stone.collidepoint(mx, my):
                select = "Rock"
            elif btn_scissors.collidepoint(mx, my):
                select = "Scissors"
            elif btn_paper.collidepoint(mx, my):
                select = "Paper"

            if select:
                player_choice = select
                ai_choice = random.choice(choices)
                res = get_result(player_choice, ai_choice)
                result_text = res
                if res == "You Win!":
                    score_player += 1
                elif res == "AI Win!":
                    score_ai += 1

pygame.quit()
