使用 Tkinter 的石头纸和剪刀游戏
先决条件: Tkinter ,随机
Python为开发 GUI(图形用户界面)提供了多种选择。在所有 GUI 方法中,Tkinter 是最常用的方法。它是Python附带的 Tk GUI 工具包的标准Python接口。 Python with Tkinter 是创建 GUI 应用程序的最快、最简单的方法。
在本文中,我们将看到如何使用 Tkinter 创建石头剪刀布游戏。石头剪刀布是一种通常在两个人之间玩的手游,其中每个玩家同时用伸出的手形成三种形状中的一种。这些形状是“石头”、“纸”和“剪刀”。
游戏获胜条件:
- 纸和剪刀=>剪刀赢
- 摇滚和剪刀=>摇滚赢
- 纸和摇滚=>纸赢
下面是 GUI 的样子:-
GUI实现步骤(添加按钮、标签、框架)
- 创建一个显示游戏标题的头部标签,设置其字体和属性
- 在头部下方,label 创建一个用户标签,将显示用户选择的手势
- 创建一个计算机标签,将显示计算机选择的手势
- 在用户和计算机标签之间创建一个标签以显示文本“vs”
- 创建结果标签以向其显示结果集字体和其他属性
- 分别为石头、纸和剪刀创建三个按钮
- 创建一个重置按钮来重置游戏
后端实现步骤:
- 我们将创建五个函数;一个用于重置游戏,第二个用于禁用按钮,另一个用于游戏获胜者
以下是实现:-
Python3
# Import Required Library
from tkinter import *
import random
# Create Object
root = Tk()
# Set geometry
root.geometry("300x300")
# Set title
root.title("Rock Paper Scissor Game")
# Computer Value
computer_value = {
"0":"Rock",
"1":"Paper",
"2":"Scissor"
}
# Reset The Game
def reset_game():
b1["state"] = "active"
b2["state"] = "active"
b3["state"] = "active"
l1.config(text = "Player ")
l3.config(text = "Computer")
l4.config(text = "")
# Disable the Button
def button_disable():
b1["state"] = "disable"
b2["state"] = "disable"
b3["state"] = "disable"
# If player selected rock
def isrock():
c_v = computer_value[str(random.randint(0,2))]
if c_v == "Rock":
match_result = "Match Draw"
elif c_v=="Scissor":
match_result = "Player Win"
else:
match_result = "Computer Win"
l4.config(text = match_result)
l1.config(text = "Rock ")
l3.config(text = c_v)
button_disable()
# If player selected paper
def ispaper():
c_v = computer_value[str(random.randint(0, 2))]
if c_v == "Paper":
match_result = "Match Draw"
elif c_v=="Scissor":
match_result = "Computer Win"
else:
match_result = "Player Win"
l4.config(text = match_result)
l1.config(text = "Paper ")
l3.config(text = c_v)
button_disable()
# If player selected scissor
def isscissor():
c_v = computer_value[str(random.randint(0,2))]
if c_v == "Rock":
match_result = "Computer Win"
elif c_v == "Scissor":
match_result = "Match Draw"
else:
match_result = "Player Win"
l4.config(text = match_result)
l1.config(text = "Scissor ")
l3.config(text = c_v)
button_disable()
# Add Labels, Frames and Button
Label(root,
text = "Rock Paper Scissor",
font = "normal 20 bold",
fg = "blue").pack(pady = 20)
frame = Frame(root)
frame.pack()
l1 = Label(frame,
text = "Player ",
font = 10)
l2 = Label(frame,
text = "VS ",
font = "normal 10 bold")
l3 = Label(frame, text = "Computer", font = 10)
l1.pack(side = LEFT)
l2.pack(side = LEFT)
l3.pack()
l4 = Label(root,
text = "",
font = "normal 20 bold",
bg = "white",
width = 15 ,
borderwidth = 2,
relief = "solid")
l4.pack(pady = 20)
frame1 = Frame(root)
frame1.pack()
b1 = Button(frame1, text = "Rock",
font = 10, width = 7,
command = isrock)
b2 = Button(frame1, text = "Paper ",
font = 10, width = 7,
command = ispaper)
b3 = Button(frame1, text = "Scissor",
font = 10, width = 7,
command = isscissor)
b1.pack(side = LEFT, padx = 10)
b2.pack(side = LEFT,padx = 10)
b3.pack(padx = 10)
Button(root, text = "Reset Game",
font = 10, fg = "red",
bg = "black", command = reset_game).pack(pady = 20)
# Execute Tkinter
root.mainloop()
输出: