import pygame
import random
import sys

# 初始化pygame
pygame.init()
# 窗口设置
WIDTH, HEIGHT = 600, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("石头剪刀布")
clock = pygame.time.Clock()
FPS = 60

# 颜色常量
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (180, 180, 180)
LIGHT_BLUE = (100, 180, 255)
RED = (255, 80, 80)
GREEN = (60, 200, 80)
ORANGE = (255, 160, 60)

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

# 游戏数据
player_score = 0
cpu_score = 0
result_text = "请选择：石头 / 剪刀 / 布"
player_choose = ""
cpu_choose = ""

# 按钮矩形 (x,y,w,h)
btn_stone = pygame.Rect(50, 220, 130, 70)
btn_scissor = pygame.Rect(235, 220, 130, 70)
btn_paper = pygame.Rect(420, 220, 130, 70)
btn_reset = pygame.Rect(220, 310, 160, 60)

# 出拳逻辑判断
def get_win(p, c):
    global player_score, cpu_score, result_text
    if p == c:
        result_text = f"平局！你出{p}，电脑出{c}"
    elif (p == "石头" and c == "剪刀") or (p == "剪刀" and c == "布") or (p == "布" and c == "石头"):
        player_score += 1
        result_text = f"你赢了！你出{p}，电脑出{c}"
    else:
        cpu_score += 1
        result_text = f"你输了！你出{p}，电脑出{c}"

# 绘制文字工具
def draw_text(text, x, y, color=BLACK, font=font_mid):
    surf = font.render(text, True, color)
    screen.blit(surf, (x, y))

# 主循环
def game_loop():
    global player_choose, cpu_choose, result_text, player_score, cpu_score
    running = True
    while running:
        screen.fill(WHITE)
        mouse_pos = pygame.mouse.get_pos()

        # 事件监听
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    # 点击石头
                    if btn_stone.collidepoint(mouse_pos):
                        player_choose = "石头"
                        cpu_choose = random.choice(["石头", "剪刀", "布"])
                        get_win(player_choose, cpu_choose)
                    # 点击剪刀
                    elif btn_scissor.collidepoint(mouse_pos):
                        player_choose = "剪刀"
                        cpu_choose = random.choice(["石头", "剪刀", "布"])
                        get_win(player_choose, cpu_choose)
                    # 点击布
                    elif btn_paper.collidepoint(mouse_pos):
                        player_choose = "布"
                        cpu_choose = random.choice(["石头", "剪刀", "布"])
                        get_win(player_choose, cpu_choose)
                    # 重置按钮
                    elif btn_reset.collidepoint(mouse_pos):
                        player_score = 0
                        cpu_score = 0
                        result_text = "分数已重置，请重新选择！"
                        player_choose = ""
                        cpu_choose = ""

        # 绘制标题
        draw_text("石头剪刀布对战", 180, 30, ORANGE, font_big)

        # 绘制分数面板
        pygame.draw.rect(screen, LIGHT_BLUE, (40, 80, 520, 60), border_radius=8)
        draw_text(f"你的分数：{player_score}        电脑分数：{cpu_score}", 120, 95, WHITE, font_mid)

        # 显示对局结果
        draw_text(result_text, 100, 160, RED, font_mid)

        # 绘制选择按钮
        # 石头
        pygame.draw.rect(screen, GRAY, btn_stone, border_radius=10)
        draw_text("石头", 90, 242)
        # 剪刀
        pygame.draw.rect(screen, GRAY, btn_scissor, border_radius=10)
        draw_text("剪刀", 275, 242)
        # 布
        pygame.draw.rect(screen, GRAY, btn_paper, border_radius=10)
        draw_text("布", 470, 242)

        # 重置按钮
        pygame.draw.rect(screen, GREEN, btn_reset, border_radius=10)
        draw_text("重置游戏", 260, 325, WHITE, font_small)

        pygame.display.update()
        clock.tick(FPS)

    pygame.quit()
    sys.exit()

if __name__ == "__main__":
    game_loop()