import pygame
import random

# ====================
# 初始化
# ====================
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("石头剪刀布")

font_big = pygame.font.SysFont("simhei", 48)
font_small = pygame.font.SysFont("simhei", 28)

clock = pygame.time.Clock()

# ====================
# 数据
# ====================
choices = ["石头", "剪刀", "布"]
icons = {"石头": "✊", "剪刀": "✋", "布": "✌"}

player_score = 0
computer_score = 0

player_choice = None
computer_choice = None
result = ""

# ====================
# 按钮区域
# ====================
buttons = {
    "石头": pygame.Rect(80, 260, 120, 50),
    "剪刀": pygame.Rect(240, 260, 120, 50),
    "布": pygame.Rect(400, 260, 120, 50),
}

# ====================
# 主循环
# ====================
running = True
while running:
    screen.fill((30, 30, 40))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            x, y = event.pos
            for choice, rect in buttons.items():
                if rect.collidepoint(x, y):
                    player_choice = choice
                    computer_choice = random.choice(choices)

                    if player_choice == computer_choice:
                        result = "平局"
                    elif (
                        (player_choice == "石头" and computer_choice == "剪刀")
                        or (player_choice == "剪刀" and computer_choice == "布")
                        or (player_choice == "布" and computer_choice == "石头")
                    ):
                        result = "你赢了！"
                        player_score += 1
                    else:
                        result = "你输了！"
                        computer_score += 1

    # ===== 显示文字 =====
    title = font_big.render("石头 剪刀 布", True, (255, 255, 255))
    screen.blit(title, (180, 40))

    if player_choice:
        p = font_big.render(
            f"你：{icons[player_choice]}", True, (0, 200, 255)
        )
        c = font_big.render(
            f"电脑：{icons[computer_choice]}", True, (255, 100, 100)
        )
        screen.blit(p, (120, 140))
        screen.blit(c, (340, 140))

        r = font_big.render(result, True, (255, 215, 0))
        screen.blit(r, (220, 200))

    # ===== 按钮 =====
    for choice, rect in buttons.items():
        pygame.draw.rect(screen, (70, 70, 90), rect)
        text = font_small.render(choice, True, (255, 255, 255))
        screen.blit(text, (rect.x + 25, rect.y + 12))

    # ===== 比分 =====
    score = font_small.render(
        f"比分  你:{player_score}  : 电脑:{computer_score}",
        True,
        (200, 200, 200),
    )
    screen.blit(score, (180, 340))

    pygame.display.flip()
    clock.tick(30)

pygame.quit()
