import tkinter as tk
from tkinter import messagebox
import random

class RPSGame:
    def __init__(self, root):
        self.root = root
        self.root.title("Python 石头剪子布小游戏")
        self.root.geometry("400x450")
        self.root.resizable(False, False)

        # 游戏状态变量
        self.player_score = 0
        self.computer_score = 0

        # 1. 顶部标题
        tk.Label(root, text="🎮 石头剪子布", font=("Arial", 20, "bold")).pack(pady=20)

        # 2. 分数显示区
        score_frame = tk.Frame(root)
        score_frame.pack(pady=10)
        self.score_label = tk.Label(score_frame, text="玩家: 0  |  电脑: 0", font=("Arial", 14))
        self.score_label.pack()

        # 3. 玩家选择按钮区
        btn_frame = tk.Frame(root)
        btn_frame.pack(pady=20)
        
        choices = ["✊ 石头", "✌️ 剪子", "🖐️ 布"]
        for choice in choices:
            tk.Button(btn_frame, text=choice, font=("Arial", 12), width=8, height=2,
                      command=lambda c=choice: self.play_game(c)).pack(side="left", padx=10)

        # 4. 结果展示区
        self.result_label = tk.Label(root, text="请做出你的选择！", font=("Arial", 16, "bold"), fg="#333333")
        self.result_label.pack(pady=20)

        # 5. 重置按钮
        tk.Button(root, text="🔄 重新开始", font=("Arial", 10), command=self.reset_game).pack(pady=10)

    def play_game(self, player_choice):
        """处理玩家的选择并判定胜负"""
        # 电脑随机选择
        computer_choice = random.choice(["✊ 石头", "✌️ 剪子", "🖐️ 布"])
        
        # 提取纯文本用于逻辑判断
        p = player_choice.split(" ")[1]
        c = computer_choice.split(" ")[1]

        # 判定胜负逻辑
        result = ""
        if p == c:
            result = "🤝 平局！"
        elif (p == "石头" and c == "剪子") or \
             (p == "剪子" and c == "布") or \
             (p == "布" and c == "石头"):
            result = "🎉 你赢了！"
            self.player_score += 1
        else:
            result = "💻 电脑赢了！"
            self.computer_score += 1

        # 更新界面显示
        self.result_label.config(text=f"你出了 {player_choice}\n电脑出了 {computer_choice}\n{result}")
        self.score_label.config(text=f"玩家: {self.player_score}  |  电脑: {self.computer_score}")

    def reset_game(self):
        """重置游戏分数和界面"""
        self.player_score = 0
        self.computer_score = 0
        self.score_label.config(text="玩家: 0  |  电脑: 0")
        self.result_label.config(text="请做出你的选择！")

if __name__ == "__main__":
    root = tk.Tk()
    app = RPSGame(root)
    root.mainloop()