import tkinter as tk
import random
from tkinter import messagebox

# 主窗口
root = tk.Tk()
root.title("石头剪刀布游戏")
root.geometry("450x320")
root.resizable(False, False)

# 分数变量
user_score = 0
computer_score = 0

# 游戏逻辑
def play(user_choice):
    global user_score, computer_score
    options = ["石头", "剪刀", "布"]
    computer_choice = random.choice(options)

    # 判断胜负
    if user_choice == computer_choice:
        result = "平局！"
    elif (user_choice == "石头" and computer_choice == "剪刀") or \
         (user_choice == "剪刀" and computer_choice == "布") or \
         (user_choice == "布" and computer_choice == "石头"):
        result = "🎉你赢了！"
        user_score += 1
    else:
        result = "💻电脑赢了！"
        computer_score += 1

    # 更新文字
    label_user.config(text=f"你的选择：{user_choice}")
    label_com.config(text=f"电脑选择：{computer_choice}")
    label_result.config(text=result)
    label_score.config(text=f"你的分数：{user_score}    电脑分数：{computer_score}")

# 重置游戏
def reset():
    global user_score, computer_score
    user_score = 0
    computer_score = 0
    label_user.config(text="你的选择：")
    label_com.config(text="电脑选择：")
    label_result.config(text="")
    label_score.config(text=f"你的分数：{user_score}    电脑分数：{computer_score}")


# 界面组件
title = tk.Label(root, text="石头 剪刀 布", font=("微软雅黑", 20))
title.pack(pady=10)

# 选择按钮区
frame_btn = tk.Frame(root)
frame_btn.pack(pady=5)

btn_rock = tk.Button(frame_btn, text="石头", width=10, height=2, font=("微软雅黑",12),command=lambda:play("石头"))
btn_rock.grid(row=0, column=0, padx=5)

btn_scissor = tk.Button(frame_btn, text="剪刀", width=10, height=2, font=("微软雅黑",12),command=lambda:play("剪刀"))
btn_scissor.grid(row=0, column=1, padx=5)

btn_paper = tk.Button(frame_btn, text="布", width=10, height=2, font=("微软雅黑",12),command=lambda:play("布"))
btn_paper.grid(row=0, column=2, padx=5)

# 结果展示
label_user = tk.Label(root, text="你的选择：", font=("微软雅黑",14))
label_user.pack(pady=4)

label_com = tk.Label(root, text="电脑选择：", font=("微软雅黑",14))
label_com.pack(pady=4)

label_result = tk.Label(root, text="", font=("微软雅黑",16),fg="red")
label_result.pack(pady=4)

label_score = tk.Label(root, text=f"你的分数：0    电脑分数：0", font=("微软雅黑",13))
label_score.pack(pady=8)

btn_reset = tk.Button(root, text="重置分数",font=("微软雅黑",12),command=reset)
btn_reset.pack()


root.mainloop()
