import tkinter as tk
import random
from tkinter import font

# ================= 主窗口 =================
root = tk.Tk()
root.title("石头剪子布")
root.geometry("400x520")
root.resizable(False, False)
root.configure(bg="#f5f5f5")

# ================= 状态变量 =================
player_score = tk.IntVar(value=0)
computer_score = tk.IntVar(value=0)
round_result = tk.StringVar(value="请出拳！")
player_choice = tk.StringVar(value="")
computer_choice = tk.StringVar(value="")

choices = {
    "rock":     ("✊", "石头"),
    "scissors": ("✌️", "剪刀"),
    "paper":    ("🖐️", "布"),
}

# ================= 核心逻辑 =================
def play(player_hand):
    computer_hand = random.choice(list(choices.keys()))

    player_choice.set(f"{choices[player_hand][0]}  {choices[player_hand][1]}")
    computer_choice.set(f"{choices[computer_hand][0]}  {choices[computer_hand][1]}")

    # 判断胜负
    if player_hand == computer_hand:
        result = "平局！"
    elif (player_hand == "rock"     and computer_hand == "scissors") or \
         (player_hand == "scissors" and computer_hand == "paper")    or \
         (player_hand == "paper"    and computer_hand == "rock"):
        result = "你赢了！🎉"
        player_score.set(player_score.get() + 1)
    else:
        result = "你输了！😢"
        computer_score.set(computer_score.get() + 1)

    round_result.set(result)

def reset_game():
    player_score.set(0)
    computer_score.set(0)
    round_result.set("请出拳！")
    player_choice.set("")
    computer_choice.set("")

# ================= 标题 =================
title_font = font.Font(family="Microsoft YaHei", size=26, weight="bold")
tk.Label(root, text="石头 ✊ 剪子 ✌️ 布 🖐️",
         font=title_font, bg="#f5f5f5", fg="#333333").pack(pady=(25, 5))

# ================= 比分板 =================
score_frame = tk.Frame(root, bg="#ffffff", highlightbackground="#dddddd", highlightthickness=1)
score_frame.pack(pady=10, padx=40, fill="x")

score_font = font.Font(family="Microsoft YaHei", size=18, weight="bold")
tk.Label(score_frame, text="玩家", font=score_font, bg="#ffffff", fg="#2196F3").grid(row=0, column=0, padx=30, pady=8)
tk.Label(score_frame, text=":", font=score_font, bg="#ffffff").grid(row=0, column=1)
tk.Label(score_frame, text="电脑", font=score_font, bg="#ffffff", fg="#FF5722").grid(row=0, column=2, padx=30, pady=8)

score_num_font = font.Font(family="Arial", size=28, weight="bold")
tk.Label(score_frame, textvariable=player_score, font=score_num_font, bg="#ffffff", fg="#2196F3").grid(row=1, column=0, padx=30, pady=(0, 10))
tk.Label(score_frame, text=":", font=score_num_font, bg="#ffffff").grid(row=1, column=1)
tk.Label(score_frame, textvariable=computer_score, font=score_num_font, bg="#ffffff", fg="#FF5722").grid(row=1, column=2, padx=30, pady=(0, 10))

# ================= 出拳显示区 =================
battle_frame = tk.Frame(root, bg="#f5f5f5")
battle_frame.pack(pady=15)

hand_font = font.Font(family="Arial", size=36)
tk.Label(battle_frame, textvariable=player_choice, font=hand_font, bg="#f5f5f5", fg="#333333").grid(row=0, column=0, padx=30)
tk.Label(battle_frame, text="VS", font=font.Font(family="Arial", size=20, weight="bold"), bg="#f5f5f5", fg="#999999").grid(row=0, column=1)
tk.Label(battle_frame, textvariable=computer_choice, font=hand_font, bg="#f5f5f5", fg="#333333").grid(row=0, column=2, padx=30)

# ================= 结果提示 =================
result_font = font.Font(family="Microsoft YaHei", size=18, weight="bold")
result_label = tk.Label(root, textvariable=round_result, font=result_font, bg="#f5f5f5", fg="#333333")
result_label.pack(pady=5)

# ================= 按钮区 =================
btn_frame = tk.Frame(root, bg="#f5f5f5")
btn_frame.pack(pady=15)

btn_font = font.Font(family="Microsoft YaHei", size=14, weight="bold")
colors = {"rock": "#4CAF50", "scissors": "#FF9800", "paper": "#9C27B0"}

for i, (key, (emoji, name)) in enumerate(choices.items()):
    btn = tk.Button(btn_frame, text=f"{emoji}  {name}", font=btn_font,
                    width=10, height=2, bg=colors[key], fg="white",
                    activebackground=colors[key], relief="flat",
                    command=lambda k=key: play(k))
    btn.grid(row=0, column=i, padx=10)

# 重置按钮
reset_btn = tk.Button(root, text="重新开始", font=btn_font,
                      width=15, height=1, bg="#607D8B", fg="white",
                      relief="flat", command=reset_game)
reset_btn.pack(pady=10)

# ================= 启动 =================
root.mainloop()