import pygame
import random
import sys

# 初始化pygame
pygame.init()

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

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (220, 30, 30)
BLUE = (30, 100, 220)
GREEN = (20, 180, 60)
GRAY = (180, 180, 180)

# 字体
font_big = pygame.font.SysFont("simhei", 45)
font_mid = pygame.font.SysFont("simhei", 30)
font_small = pygame.font.SysFont("simhei", 24)

# 游戏数据
choices = ["石头", "剪刀", "布"]
player_choose = ""
pc_choose = ""
result_text = "请点击下方按钮出拳！"
score_player = 0
score_pc = 0

# 按钮坐标与尺寸
btn_width = 140
btn_height = 60
btn_x_start = 40
btn_y = 280
btn_gap = 20

# 按钮矩形
btn_stone = pygame.Rect(btn_x_start, btn_y, btn_width, btn_height)
btn_scissor = pygame.Rect(btn_x_start + btn_width + btn_gap, btn_y, btn_width, btn_height)
btn_paper = pygame.Rect(btn_x_start + (btn_width + btn_gap)*2, btn_y, btn_width, btn_height)
btn_reset = pygame.Rect(420, 30, 120, 45)

# 判断胜负逻辑
def get_result(player, pc):
    global score_player, score_pc
    if player == pc:
        return "平局！"
    # 赢的规则：石头砸剪刀，剪刀剪布，布包石头
    win_case = (
        (player == "石头" and pc == "剪刀"),
        (player == "剪刀" and pc == "布"),
        (player == "布" and pc == "石头")
    )
    if any(win_case):
        score_player += 1
        return "你赢了！"
    else:
        score_pc += 1
        return "电脑赢了！"

# 主循环
clock = pygame.time.Clock()
running = True

while running:
    # 帧率控制
    clock.tick(60)
    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:
            mouse_pos = pygame.mouse.get_pos()
            # 出拳按钮
            if btn_stone.collidepoint(mouse_pos):
                player_choose = "石头"
            elif btn_scissor.collidepoint(mouse_pos):
                player_choose = "剪刀"
            elif btn_paper.collidepoint(mouse_pos):
                player_choose = "布"
            # 重置按钮
            elif btn_reset.collidepoint(mouse_pos):
                player_choose = ""
                pc_choose = ""
                result_text = "请点击下方按钮出拳！"
                score_player = 0
                score_pc = 0

            # 玩家出拳后电脑随机选择，计算结果
            if player_choose != "":
                pc_choose = random.choice(choices)
                result_text = get_result(player_choose, pc_choose)

    # 绘制文字：分数
    score_text = font_mid.render(f"你:{score_player}  电脑:{score_pc}", True, BLACK)
    screen.blit(score_text, (30, 30))

    # 重置按钮
    pygame.draw.rect(screen, GRAY, btn_reset)
    reset_text = font_small.render("重置游戏", True, BLACK)
    screen.blit(reset_text, (btn_reset.x + 15, btn_reset.y + 10))

    # 绘制选择展示区
    show_y = 100
    if player_choose:
        p_text = font_big.render(f"你的选择：{player_choose}", True, BLUE)
        screen.blit(p_text, (120, show_y))
    if pc_choose:
        pc_text = font_big.render(f"电脑选择：{pc_choose}", True, RED)
        screen.blit(pc_text, (120, show_y + 50))

    # 胜负结果
    res_text = font_big.render(result_text, True, GREEN)
    screen.blit(res_text, (180, 180))

    # 绘制三个出拳按钮
    pygame.draw.rect(screen, BLUE, btn_stone)
    pygame.draw.rect(screen, RED, btn_scissor)
    pygame.draw.rect(screen, GREEN, btn_paper)

    # 按钮文字
    stone_txt = font_mid.render("石头", True, WHITE)
    scissor_txt = font_mid.render("剪刀", True, WHITE)
    paper_txt = font_mid.render("布", True, WHITE)
    screen.blit(stone_txt, (btn_stone.x + 45, btn_stone.y + 12))
    screen.blit(scissor_txt, (btn_scissor.x + 40, btn_scissor.y + 12))
    screen.blit(paper_txt, (btn_paper.x + 50, btn_paper.y + 12))

    pygame.display.flip()

pygame.quit()
sys.exit()