编写剪刀石头布游戏
让咱们应用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 Tkinter教程系列01:编写简略版剪刀石头布游戏
用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游戏的残缺代码(IT小站)。
#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()