import tkinter as tk
from tkinter import messagebox
import random

# 定义手势常量
ROCK = 0
SCISSORS = 1
PAPER = 2
# 手势文字与图标显示
gesture_text = {ROCK: "石头", SCISSORS: "剪刀", PAPER: "布"}
gesture_emoji = {ROCK: "🪨", SCISSORS: "✂️", PAPER: "🧻"}

class RockPaperScissors:
    def __init__(self, root):
        self.root = root
        self.root.title("石头剪刀布小游戏")
        self.root.geometry("480x360")
        self.root.resizable(False, False)

        # 计分变量
        self.player_score = 0
        self.computer_score = 0

        # 顶部分数栏
        frame_score = tk.Frame(root)
        frame_score.pack(pady=15)
        tk.Label(frame_score, text="我方分数：", font=("微软雅黑", 14)).grid(row=0, column=0, padx=10)
        self.label_player_score = tk.Label(frame_score, text="0", font=("微软雅黑", 14, "bold"), fg="#2277cc")
        self.label_player_score.grid(row=0, column=1, padx=10)

        tk.Label(frame_score, text="电脑分数：", font=("微软雅黑", 14)).grid(row=0, column=2, padx=10)
        self.label_computer_score = tk.Label(frame_score, text="0", font=("微软雅黑", 14, "bold"), fg="#dd3333")
        self.label_computer_score.grid(row=0, column=3, padx=10)

        # 对局展示区（双方出拳）
        frame_show = tk.Frame(root)
        frame_show.pack(pady=10)
        # 玩家展示
        tk.Label(frame_show, text="你的出拳", font=("微软雅黑", 12)).grid(row=0, column=0, padx=30)
        self.label_player_hand = tk.Label(frame_show, text="❓", font=("微软雅黑", 40))
        self.label_player_hand.grid(row=1, column=0, padx=30)

        # 对战vs
        tk.Label(frame_show, text="VS", font=("微软雅黑", 20, "bold"), fg="orange").grid(row=1, column=1)

        # 电脑展示
        tk.Label(frame_show, text="电脑出拳", font=("微软雅黑", 12)).grid(row=0, column=2, padx=30)
        self.label_computer_hand = tk.Label(frame_show, text="❓", font=("微软雅黑", 40))
        self.label_computer_hand.grid(row=1, column=2, padx=30)

        # 对局结果提示
        self.label_result = tk.Label(root, text="请选择下方手势开始游戏", font=("微软雅黑", 13))
        self.label_result.pack(pady=10)

        # 出拳按钮区域
        frame_btn = tk.Frame(root)
        frame_btn.pack(pady=10)
        btn_rock = tk.Button(frame_btn, text="石头 🪨", width=8, height=2, font=("微软雅黑", 11),
                             command=lambda: self.play(ROCK))
        btn_rock.grid(row=0, column=0, padx=8)

        btn_scissor = tk.Button(frame_btn, text="剪刀 ✂️", width=8, height=2, font=("微软雅黑", 11),
                                command=lambda: self.play(SCISSORS))
        btn_scissor.grid(row=0, column=1, padx=8)

        btn_paper = tk.Button(frame_btn, text="布 🧻", width=8, height=2, font=("微软雅黑", 11),
                              command=lambda: self.play(PAPER))
        btn_paper.grid(row=0, column=2, padx=8)

        # 重置按钮
        btn_reset = tk.Button(root, text="重置分数", font=("微软雅黑", 10), bg="#eeeeee",
                              command=self.reset_score)
        btn_reset.pack(pady=8)

    def play(self, player_choice):
        # 电脑随机选择
        computer_choice = random.randint(0, 2)
        # 更新界面显示手势
        self.label_player_hand.config(text=f"{gesture_emoji[player_choice]}\n{gesture_text[player_choice]}")
        self.label_computer_hand.config(text=f"{gesture_emoji[computer_choice]}\n{gesture_text[computer_choice]}")

        # 判断胜负逻辑
        if player_choice == computer_choice:
            res_text = "本局平局！"
        elif (player_choice == ROCK and computer_choice == SCISSORS) or \
             (player_choice == SCISSORS and computer_choice == PAPER) or \
             (player_choice == PAPER and computer_choice == ROCK):
            res_text = "恭喜，你赢了本局！"
            self.player_score += 1
            self.label_player_score.config(text=str(self.player_score))
        else:
            res_text = "很遗憾，电脑获胜"
            self.computer_score += 1
            self.label_computer_score.config(text=str(self.computer_score))

        self.label_result.config(text=res_text)

    def reset_score(self):
        # 清空分数和显示
        self.player_score = 0
        self.computer_score = 0
        self.label_player_score.config(text="0")
        self.label_computer_score.config(text="0")
        self.label_player_hand.config(text="❓")
        self.label_computer_hand.config(text="❓")
        self.label_result.config(text="分数已重置，请重新开始对局")

if __name__ == "__main__":
    main_window = tk.Tk()
    app = RockPaperScissors(main_window)
    main_window.mainloop()