import tkinter as tk
import random

# 主窗口
root = tk.Tk()
root.title("石头剪刀布小游戏")
root.geometry("450x350")

# 计分
user_score = 0
com_score = 0

# 出拳列表
arr = ["石头", "剪刀", "布"]

def play(you):
    global user_score, com_score
    com = random.choice(arr)
    # 显示出拳
    lab_you.config(text=f"你出：{you}")
    lab_com.config(text=f"电脑出：{com}")
    
    # 判断胜负
    if you == com:
        res = "平局"
    elif (you=="石头" and com=="剪刀") or (you=="剪刀" and com=="布") or (you=="布" and com=="石头"):
        res = "你赢啦！"
        user_score += 1
    else:
        res = "电脑赢了"
        com_score += 1
    
    lab_res.config(text=res)
    lab_score.config(text=f"比分 你:{user_score} 电脑:{com_score}")

# 标题
tk.Label(root,text="石头剪刀布",font=("黑体",20)).pack(pady=10)

# 显示区域
lab_you = tk.Label(root,text="你出：",font=("黑体",14))
lab_you.pack()
lab_com = tk.Label(root,text="电脑出：",font=("黑体",14))
lab_com.pack()
lab_res = tk.Label(root,text="等待出拳",font=("黑体",16),fg="red")
lab_res.pack(pady=5)
lab_score = tk.Label(root,text="比分 你:0 电脑:0",font=("黑体",13))
lab_score.pack(pady=5)

# 按钮区
frame = tk.Frame(root)
frame.pack(pady=20)

tk.Button(frame,text="石头",width=8,height=2,command=lambda:play("石头"),bg="#ffcccc").grid(row=0,column=0,padx=10)
tk.Button(frame,text="剪刀",width=8,height=2,command=lambda:play("剪刀"),bg="#ccffcc").grid(row=0,column=1,padx=10)
tk.Button(frame,text="布",width=8,height=2,command=lambda:play("布"),bg="#ccccff").grid(row=0,column=2,padx=10)

root.mainloop()