import pygame
import random

# 初始化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 = (220, 30, 30)
BLUE = (30, 80, 220)
GREEN = (20, 160, 60)
GRAY = (180, 180, 180)

# 字体
font_big = pygame.font.SysFont("simhei", 36)
font_mid = pygame.font.SysFont("simhei", 28)
font_small = pygame.font.SysFont("simhei", 22)

# 按钮信息
btn_info = [
    {"text": "石头", "rect": pygame.Rect(50, 280, 140, 70)},
    {"text": "剪刀", "rect": pygame.Rect(230, 280, 140, 70)},
    {"text": "布", "rect": pygame.Rect(410, 280, 140, 70)}
]

# 映射选择数字
choice_map = {"石头": 0, "剪刀": 1, "布": 2}
rev_map = {0: "石头", 1: "剪刀", 2: "布"}

# 计分
player_score = 0
ai_score = 0

# 对局信息
player_choose = ""
ai_choose = ""
result_text = "请点击下方按钮出拳"

clock = pygame.time.Clock()
running = True

def get_result(p, a):
    """判定胜负：0平 1玩家赢 -1电脑赢"""
    if p == a:
        return 0
    # 石头赢剪刀，剪刀赢布，布赢石头
    if (p == 0 and a == 1) or (p == 1 and a == 2) or (p == 2 and a == 0):
        return 1
    else:
        return -1

while running:
    screen.fill(WHITE)

    # 事件循环
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            mx, my = pygame.mouse.get_pos()
            for btn in btn_info:
                if btn["rect"].collidepoint(mx, my):
                    # 玩家选择
                    player_choose = btn["text"]
                    p_num = choice_map[player_choose]
                    # AI随机
                    a_num = random.randint(0, 2)
                    ai_choose = rev_map[a_num]
                    # 判定
                    res = get_result(p_num, a_num)
                    if res == 0:
                        result_text = f"平局！你：{player_choose} 电脑：{ai_choose}"
                    elif res == 1:
                        player_score += 1
                        result_text = f"你赢了！你：{player_choose} 电脑：{ai_choose}"
                    else:
                        ai_score += 1
                        result_text = f"电脑赢！你：{player_choose} 电脑：{ai_choose}"

    # 绘制标题
    title = font_big.render("石头剪刀布 人机对战", True, BLACK)
    screen.blit(title, ((WIDTH - title.get_width()) // 2, 30))

    # 绘制比分
    score_txt = font_mid.render(f"玩家得分：{player_score}    电脑得分：{ai_score}", True, BLUE)
    screen.blit(score_txt, (40, 100))

    # 绘制对局结果
    res_surf = font_mid.render(result_text, True, RED)
    screen.blit(res_surf, ((WIDTH - res_surf.get_width()) // 2, 160))

    # 绘制三个按钮
    for btn in btn_info:
        pygame.draw.rect(screen, GRAY, btn["rect"])
        pygame.draw.rect(screen, BLACK, btn["rect"], 2)
        text_surf = font_small.render(btn["text"], True, BLACK)
        tx = btn["rect"].x + (btn["rect"].width - text_surf.get_width()) // 2
        ty = btn["rect"].y + (btn["rect"].height - text_surf.get_height()) // 2
        screen.blit(text_surf, (tx, ty))

    pygame.display.flip()
    clock.tick(60)

pygame.quit()
