python视频教程栏目介绍使用tkinter实现剪刀石头布
相关免费学习推荐:python视频教程
编写剪刀石头布游戏
让我们使用python 3和tkinter开发相同的游戏。我们可以将游戏命名为rock-paper-scissors-lizard-spock。
规则和玩法
rock crushes scissors
rock crushes lizard
paper covers rock
paper disproves spock
scissors cuts paper
scissors decapitates lizard
lizard poisons spock
lizard eats paper
spock smashes scissors
spock vaporizes rock
two same objects is a draw
程序演练
当用户运行程序时,他们必须单击五个可用对象之一:
rock
paper
scissors
lizard
spock
如果大家在学习中遇到困难,想找一个python学习交流环境,可以加入我们的python圈,裙号930900780,可领取python学习资料,会节约很多时间,减少很多遇到的难题。
当用户选择一个对象时,我们的程序将随机选择一个对象。然后,它将通过一组规则来声明用户是赢,输还是画游戏。结果将显示在应用程序的第二行。
当用户按下任何按钮时,游戏将重新开始。如果用户想要关闭游戏,则可以按关闭按钮。在游戏开始时,我们具有用于特定对象的手形符号。现在,当用户选择一个对象时,它将转换为图形图像。我们的程序还选择了一个对象,它将显示所选对象的图形图像。
用python实现(10个步骤)
现在我们已经有了剪刀石头布游戏的意义,让我们逐步介绍python的过程。
1.导入所需的库
#import the required libraries :from tkinter import *import randomimport simpleaudio as sa
tkinter:在我们的应用程序中添加小部件
random:生成一个随机数
simpleaudio:播放声音文件
2.创建tkinter主窗口
root = tk()root.configure(bg="#000000")root.geometry('+0+0')root.iconbitmap("game.ico")root.title("rock-paper-scissor-lizard-spock")root.resizable(width=false,height=false)
root = tk( ):用于初始化我们的tkinter模块。
root.configure( ):我们使用它来指定应用程序的背景色。在我们的情况下,背景颜色为黑色。
root.geometry( ):我们使用它来指定我们的应用程序窗口将在哪个位置打开。它将在左上角打开。
root.iconbitmap( ):我们使用它来设置应用程序窗口标题栏中的图标。此功能仅接受.ico文件。
root.title( ):我们使用它来设置应用程序的标题。
root.resizable( ):在这里我们使用它来防止用户调整主窗口的大小。
3.导入声音文件
#to play sound files :start = sa.waveobject.from_wave_file("start.wav")win = sa.waveobject.from_wave_file("win.wav")lose = sa.waveobject.from_wave_file("lose.wav")draw = sa.waveobject.from_wave_file("draw.wav")start.play()
现在,我们将使用一些将在各种事件中播放的声音文件。当我们的程序启动时,它将播放开始文件。当用户赢得游戏,输掉游戏或绘制游戏时,我们将播放其他三个文件。
需要注意的一件事是它仅接受.wav文件。首先,我们需要将声音文件加载到对象中。然后我们可以.play( )在需要时使用方法播放它。
4.为我们的应用程序加载图像
我们将在应用程序中使用各种图像。要首先使用这些图像,我们需要加载这些图像。在这里,我们将使用photoimage类加载图像。
#hand images :rockhandphoto = photoimage(file="rock_1.png")paperhandphoto = photoimage(file="paper_1.png")scissorhandphoto = photoimage(file="scissor_1.png")lizardhandphoto = photoimage(file="lizard_1.png")spockhandphoto = photoimage(file="spock_1.png")#graphical images :rockphoto = photoimage(file="rock_p.png")paperphoto = photoimage(file="paper_p.png")scissorphoto = photoimage(file="scissor_p.png")lizardphoto = photoimage(file="lizard_p.png")spockphoto = photoimage(file="spock_p.png")#decision image :decisionphoto = photoimage(file="decision_final.png")#result images :winphoto = photoimage(file="g_win.png")losephoto = photoimage(file="g_lost.png")tiephoto = photoimage(file="g_draw.png")
首先,我们为物体准备了手部图像。游戏开始时将向用户显示所有五个图像。用户必须从那些图像中选择一个对象。
用户单击图像后,我们的程序将向我们显示该对象的图形图像。必须选择一个对象,我们的程序也将选择一个对象。我们的程序将仅显示这两个图形图像,然后其余图像将消失。
现在,我们显示一个简单的决策图像,当结果可用时,它将更改其图像。我们的结果有不同的图像。
如果用户获胜
如果用户输了
如果有平局
5.添加tkinter小部件
#initialize the button variables :rockhandbutton = " "paperhandbutton = " "scissorhandbutton = " "lizardhandbutton= " "spockhandbutton = " "#create the result button :resultbutton = button(root,image=decisionphoto)#set the variable to trueclick = true
初始化五个按钮的变量。
在这里,我们创建了结果按钮,它将向我们显示最终结果。
我们将click变量设置为true,以便我们的程序继续运行直到将其设置为false。在接下来的几点中,我们将看到更多有关此的内容。
6. play( )功能
def play(): global rockhandbutton,paperhandbutton,scissorhandbutton,lizardhandbutton,spockhandbutton #set images and commands for buttons : rockhandbutton = button(root,image = rockhandphoto, command=lambda:youpick("rock")) paperhandbutton = button(root,image = paperhandphoto, command=lambda:youpick("paper")) scissorhandbutton = button(root,image = scissorhandphoto, command=lambda:youpick("scissor")) lizardhandbutton = button(root,image= lizardhandphoto,command=lambda:youpick("lizard")) spockhandbutton = button(root,image= spockhandphoto,command=lambda:youpick("spock")) #place the buttons on window : rockhandbutton.grid(row=0,column=0) paperhandbutton.grid(row=0,column=1) scissorhandbutton.grid(row=0,column=2) lizardhandbutton.grid(row=0,column=3) spockhandbutton.grid(row=0,column=4) #add space : root.grid_rowconfigure(1, minsize=50) #place result button on window : resultbutton.grid(row=2,column=0,columnspan=5)
在这里,我们为对象创建按钮。我们将为按钮设置图像,当按下按钮时,它将youpick( )与单击的对象的字符串名称一起起作用。
然后,使用该.grid( )方法将按钮排列在主窗口上。在这里,我们在的第一行添加一个空格.grid_rowconfigure( )。然后,将结果按钮放在第二行。我们正在使用columnspan结果按钮居中。
7.轮到计算机了
我们的计算机将随机选择五个可用对象之一,并为此返回一个字符串值。
def computerpick(): choice = random.choice(["rock","paper","scissor","lizard","spock"]) return choice
8.主要功能: youpick( )
在此功能中,我们的程序将显示所选对象的图形图像。它将删除其余的对象。它还将应用一组规则来生成结果。
def youpick(yourchoice): global click comppick = computerpick() if click==true:
我们将计算机的选择存储在comppick变量中。我们将使用它来确定结果。
用户选择rock:
如果用户选择rock,则使用此代码块。play( )函数中的命令沿字符串发送,该字符串代表用户选择的对象。我们将其存储在yourchoice变量中。现在,计算机有五种可能性。
现在我们必须为每个规则制定规则。现在注意,当用户和计算机选择一个对象时,不允许他们对其进行更改。因此,我们将click变量更改为false。
现在,由于用户已选择,rock我们希望我们的第一张图像变成岩石的图形图像。现在,如果计算机选择rock,那么我们希望我们的第二张图像变成图形图像。要更改按钮的图像,我们使用.configure( )方法。
我们希望其余三个图像消失。为了使它们消失,我们使用.grid_forget( )。它还将播放绘图音频。现在,我们为其余对象开发类似的规则。
def computerpick():choice = random.choice(["rock","paper","scissor","lizard","spock"])return choice
用户选择纸张:
请参阅上面的规则,以了解用户选择“纸张”时的规则。查看下面的代码,该代码遵循与rock相同的规则。
elif yourchoice == "paper":rockhandbutton.configure(image=paperphoto)if comppick == "rock":paperhandbutton.configure(image=rockphoto)resultbutton.configure(image=losephoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()lose.play()click = falseelif comppick == "paper":paperhandbutton.configure(image=paperphoto)resultbutton.configure(image=tiephoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()draw.play()click = falseelif comppick == "scissor":paperhandbutton.configure(image=scissorphoto)resultbutton.configure(image=losephoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()lose.play()click = falseelif comppick =="lizard":paperhandbutton.configure(image=lizardphoto)resultbutton.configure(image=losephoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()lose.play()click = falseelse :paperhandbutton.configure(image=spockphoto)resultbutton.configure(image=winphoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()win.play()click = false
用户选择剪刀:
请从上方查看规则,以了解用户选择剪刀时的规则。查看下面的代码,该代码遵循与rock and paper相同的规则。
elif yourchoice=="scissor":rockhandbutton.configure(image=scissorphoto)if comppick == "rock":paperhandbutton.configure(image=rockphoto)resultbutton.configure(image=losephoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()lose.play()click = falseelif comppick == "paper":paperhandbutton.configure(image=paperphoto)resultbutton.configure(image=winphoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()win.play()click = falseelif comppick=="scissor":paperhandbutton.configure(image=scissorphoto)resultbutton.configure(image=tiephoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()draw.play()click = falseelif comppick == "lizard":paperhandbutton.configure(image=lizardphoto)resultbutton.configure(image=winphoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()win.play()click = falseelse:paperhandbutton.configure(image=spockphoto)resultbutton.configure(image=losephoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()lose.play()click = false
用户选择"lizard"
请从上方查看规则,以了解用户选择蜥蜴的规则。查看下面的代码,该代码遵循与其他代码相同的规则。
elif yourchoice=="lizard":rockhandbutton.configure(image=lizardphoto)if comppick == "rock":paperhandbutton.configure(image=rockphoto)resultbutton.configure(image=losephoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()lose.play()click = falseelif comppick == "paper":paperhandbutton.configure(image=paperphoto)resultbutton.configure(image=winphoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()win.play()click = falseelif comppick=="scissor":paperhandbutton.configure(image=scissorphoto)resultbutton.configure(image=losephoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()lose.play()click = falseelif comppick == "lizard":paperhandbutton.configure(image=lizardphoto)resultbutton.configure(image=tiephoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()draw.play()click = falseelse:paperhandbutton.configure(image=spockphoto)resultbutton.configure(image=winphoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()win.play()click = false
用户选择spock:
请从上方查看规则,以了解用户选择spock的规则。查看下面的代码,该代码遵循与其他代码相同的规则。
elif yourchoice=="spock":rockhandbutton.configure(image=spockphoto)if comppick == "rock":paperhandbutton.configure(image=rockphoto)resultbutton.configure(image=winphoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()win.play()click = falseelif comppick == "paper":paperhandbutton.configure(image=paperphoto)resultbutton.configure(image=losephoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()lose.play()click = falseelif comppick=="scissor":paperhandbutton.configure(image=scissorphoto)resultbutton.configure(image=winphoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()win.play()click = falseelif comppick == "lizard":paperhandbutton.configure(image=lizardphoto)resultbutton.configure(image=losephoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()lose.play()click = falseelse:paperhandbutton.configure(image=spockphoto)resultbutton.configure(image=tiephoto)scissorhandbutton.grid_forget()lizardhandbutton.grid_forget()spockhandbutton.grid_forget()draw.play()click = false
9.再玩一次
得到结果后,如果要再次播放,只需单击任何按钮。它将转换为原始的手部图像。现在,我们必须取回那些消失的图像。我们将click变量的值设置为true。然后,我们将播放开始声音文件,以便在用户进入新游戏时将播放音频。
else: #to reset the game : if yourchoice=="rock" or yourchoice=="paper" or yourchoice=="scissor" or yourchoice=="lizard" or yourchoice=="spock": rockhandbutton.configure(image=rockhandphoto) paperhandbutton.configure(image=paperhandphoto) scissorhandbutton.configure(image=scissorhandphoto) lizardhandbutton.configure(image=lizardhandphoto) spockhandbutton.configure(image=spockhandphoto) resultbutton.configure(image=decisionphoto) #get back the deleted buttons : scissorhandbutton.grid(row=0,column=2) lizardhandbutton.grid(row=0,column=3) spockhandbutton.grid(row=0,column=4) #set click = true : click=true #play the sound file : start.play()
10.调用函数
现在我们调用play函数,它将在内部处理其余函数。要关闭该应用程序,请按标题栏上的关闭按钮。
#calling the play function :play()#enter the main loop :root.mainloop()
放在一起
查看此python tkinter游戏的完整代码。
#import the required libraries :from tkinter import *import randomimport simpleaudio as saroot = tk()root.configure(bg="#000000")root.geometry('+0+0')root.iconbitmap("game.ico")root.title("rock-paper-scissor-lizard-spock")root.resizable(width=false,height=false)#to play sound files :start = sa.waveobject.from_wave_file("start.wav")win = sa.waveobject.from_wave_file("win.wav")lose = sa.waveobject.from_wave_file("lose.wav")draw = sa.waveobject.from_wave_file("draw.wav")start.play()#hand images :rockhandphoto = photoimage(file="rock_1.png")paperhandphoto = photoimage(file="paper_1.png")scissorhandphoto = photoimage(file="scissor_1.png")lizardhandphoto = photoimage(file="lizard_1.png")spockhandphoto = photoimage(file="spock_1.png")#graphical images :rockphoto = photoimage(file="rock_p.png")paperphoto = photoimage(file="paper_p.png")scissorphoto = photoimage(file="scissor_p.png")lizardphoto = photoimage(file="lizard_p.png")spockphoto = photoimage(file="spock_p.png")#decision image :decisionphoto = photoimage(file="decision_final.png")#result images :winphoto = photoimage(file="g_win.png")losephoto = photoimage(file="g_lost.png")tiephoto = photoimage(file="g_draw.png")#initialize the button variables :rockhandbutton = " "paperhandbutton = " "scissorhandbutton = " "lizardhandbutton= " "spockhandbutton = " "#create the result button :resultbutton = button(root,image=decisionphoto)#set the variable to trueclick = truedef play(): global rockhandbutton,paperhandbutton,scissorhandbutton,lizardhandbutton,spockhandbutton #set images and commands for buttons : rockhandbutton = button(root,image = rockhandphoto, command=lambda:youpick("rock")) paperhandbutton = button(root,image = paperhandphoto, command=lambda:youpick("paper")) scissorhandbutton = button(root,image = scissorhandphoto, command=lambda:youpick("scissor")) lizardhandbutton = button(root,image= lizardhandphoto,command=lambda:youpick("lizard")) spockhandbutton = button(root,image= spockhandphoto,command=lambda:youpick("spock")) #place the buttons on window : rockhandbutton.grid(row=0,column=0) paperhandbutton.grid(row=0,column=1) scissorhandbutton.grid(row=0,column=2) lizardhandbutton.grid(row=0,column=3) spockhandbutton.grid(row=0,column=4) #add space : root.grid_rowconfigure(1, minsize=50) #place result button on window : resultbutton.grid(row=2,column=0,columnspan=5)def computerpick(): choice = random.choice(["rock","paper","scissor","lizard","spock"]) return choicedef youpick(yourchoice): global click comppick = computerpick() if click==true: if yourchoice == "rock": rockhandbutton.configure(image=rockphoto) if comppick == "rock": paperhandbutton.configure(image=rockphoto) resultbutton.configure(image=tiephoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() draw.play() click = false elif comppick == "paper": paperhandbutton.configure(image=paperphoto) scissorhandbutton.grid_forget() resultbutton.configure(image=losephoto) lizardhandbutton.grid_forget() spockhandbutton.grid_forget() lose.play() click = false elif comppick == "scissor": paperhandbutton.configure(image=scissorphoto) scissorhandbutton.grid_forget() resultbutton.configure(image=winphoto) lizardhandbutton.grid_forget() spockhandbutton.grid_forget() win.play() click = false elif comppick =="lizard": paperhandbutton.configure(image=lizardphoto) scissorhandbutton.grid_forget() resultbutton.configure(image=winphoto) lizardhandbutton.grid_forget() spockhandbutton.grid_forget() win.play() click = false else : paperhandbutton.configure(image=spockphoto) scissorhandbutton.grid_forget() resultbutton.configure(image=losephoto) lizardhandbutton.grid_forget() spockhandbutton.grid_forget() lose.play() click = false elif yourchoice == "paper": rockhandbutton.configure(image=paperphoto) if comppick == "rock": paperhandbutton.configure(image=rockphoto) resultbutton.configure(image=losephoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() lose.play() click = false elif comppick == "paper": paperhandbutton.configure(image=paperphoto) resultbutton.configure(image=tiephoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() draw.play() click = false elif comppick == "scissor": paperhandbutton.configure(image=scissorphoto) resultbutton.configure(image=losephoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() lose.play() click = false elif comppick =="lizard": paperhandbutton.configure(image=lizardphoto) resultbutton.configure(image=losephoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() lose.play() click = false else : paperhandbutton.configure(image=spockphoto) resultbutton.configure(image=winphoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() win.play() click = false elif yourchoice=="scissor": rockhandbutton.configure(image=scissorphoto) if comppick == "rock": paperhandbutton.configure(image=rockphoto) resultbutton.configure(image=losephoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() lose.play() click = false elif comppick == "paper": paperhandbutton.configure(image=paperphoto) resultbutton.configure(image=winphoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() win.play() click = false elif comppick=="scissor": paperhandbutton.configure(image=scissorphoto) resultbutton.configure(image=tiephoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() draw.play() click = false elif comppick == "lizard": paperhandbutton.configure(image=lizardphoto) resultbutton.configure(image=winphoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() win.play() click = false else: paperhandbutton.configure(image=spockphoto) resultbutton.configure(image=losephoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() lose.play() click = false elif yourchoice=="lizard": rockhandbutton.configure(image=lizardphoto) if comppick == "rock": paperhandbutton.configure(image=rockphoto) resultbutton.configure(image=losephoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() lose.play() click = false elif comppick == "paper": paperhandbutton.configure(image=paperphoto) resultbutton.configure(image=winphoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() win.play() click = false elif comppick=="scissor": paperhandbutton.configure(image=scissorphoto) resultbutton.configure(image=losephoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() lose.play() click = false elif comppick == "lizard": paperhandbutton.configure(image=lizardphoto) resultbutton.configure(image=tiephoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() draw.play() click = false else: paperhandbutton.configure(image=spockphoto) resultbutton.configure(image=winphoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() win.play() click = false elif yourchoice=="spock": rockhandbutton.configure(image=spockphoto) if comppick == "rock": paperhandbutton.configure(image=rockphoto) resultbutton.configure(image=winphoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() win.play() click = false elif comppick == "paper": paperhandbutton.configure(image=paperphoto) resultbutton.configure(image=losephoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() lose.play() click = false elif comppick=="scissor": paperhandbutton.configure(image=scissorphoto) resultbutton.configure(image=winphoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() win.play() click = false elif comppick == "lizard": paperhandbutton.configure(image=lizardphoto) resultbutton.configure(image=losephoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() lose.play() click = false else: paperhandbutton.configure(image=spockphoto) resultbutton.configure(image=tiephoto) scissorhandbutton.grid_forget() lizardhandbutton.grid_forget() spockhandbutton.grid_forget() draw.play() click = false else: #to reset the game : if yourchoice=="rock" or yourchoice=="paper" or yourchoice=="scissor" or yourchoice=="lizard" or yourchoice=="spock": rockhandbutton.configure(image=rockhandphoto) paperhandbutton.configure(image=paperhandphoto) scissorhandbutton.configure(image=scissorhandphoto) lizardhandbutton.configure(image=lizardhandphoto) spockhandbutton.configure(image=spockhandphoto) resultbutton.configure(image=decisionphoto) #get back the deleted buttons : scissorhandbutton.grid(row=0,column=2) lizardhandbutton.grid(row=0,column=3) spockhandbutton.grid(row=0,column=4) #set click = true : click=true #play the sound file : start.play()#calling the play function :play()#enter the main loop :root.mainloop()
想了解更多编程学习,敬请关注php培训栏目!
以上就是用python tkinter实现剪刀石头布小游戏的详细内容。