import pygame
import random

# 初始化
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 = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 180, 0)
GRAY = (180, 180, 180)

# 字体
font_large = pygame.font.SysFont("SimHei", 40)
font_mid = pygame.font.SysFont("SimHei", 30)
font_small = pygame.font.SysFont("SimHei", 24)

# 选项映射
choices = ["石头", "剪刀", "布"]
player_score = 0
ai_score = 0

# 判断胜负
def get_result(player, ai):
    if player == ai:
        return "平局"
    # 赢的规则
    if (player=="石头" and ai=="剪刀") or \
       (player=="剪刀" and ai=="布") or \
       (player=="布" and ai=="石头"):
        return "你赢了"
    else:
        return "你输了"

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

# 主循环
clock = pygame.time.Clock()
running = True
result_text = "请选择：石头 / 剪刀 / 布"
player_choice = ""
ai_choice = ""

while running:
    screen.fill(WHITE)

    # 顶部分数
    draw_text(f"你的分数：{player_score}", 50, 30, BLUE)
    draw_text(f"电脑分数：{ai_score}", 400, 30, RED)

    # 中间结果
    draw_text(result_text, 180, 120, GREEN, font_large)

    # 显示双方选择
    if player_choice:
        draw_text(f"你出：{player_choice}", 150, 200, BLUE)
        draw_text(f"电脑出：{ai_choice}", 350, 200, RED)

    # 底部操作提示
    draw_text("按 1=石头  2=剪刀  3=布  Q=退出", 120, 320, GRAY, font_small)

    # 事件监听
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            # 按键选择
            if event.key == pygame.K_1:
                player_choice = "石头"
            elif event.key == pygame.K_2:
                player_choice = "剪刀"
            elif event.key == pygame.K_3:
                player_choice = "布"
            elif event.key == pygame.K_q:
                running = False

            # 判定对局
            if player_choice in choices:
                ai_choice = random.choice(choices)
                res = get_result(player_choice, ai_choice)
                result_text = res
                # 计分
                if res == "你赢了":
                    player_score += 1
                elif res == "你输了":
                    ai_score += 1

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

pygame.quit()