#相關免費學習推薦: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
Spock smashes Scissors##Spock vaporizes Rock
#Spock smashes Scissors#Two same objects is a draw
程式演練
當使用者執行程式時,他們必須點擊五個可用物件之一:
Rock
Paper
Scissors
Lizard
Spock如果大家在學習中遇到困難,想找一個python學習交流環境,可以加入我們的python圈,裙號930900780,可領取python學習資料,會節約很多時間,減少很多遇到的難題。
當使用者選擇一個物件時,我們的程式將隨機選擇一個物件。然後,它將透過一組規則來聲明用戶是贏,輸還是畫遊戲。結果將顯示在應用程式的第二行。
當使用者按下任何按鈕時,遊戲將重新開始。如果使用者想要關閉遊戲,則可以按下關閉按鈕。在遊戲開始時,我們具有用於特定物件的手形符號。現在,當使用者選擇一個物件時,它將轉換為圖形圖像。我們的程式還選擇了一個對象,它將顯示所選對象的圖形圖像。
用Python實現(10個步驟)
1.導入所需的函式庫
#Import the required libraries : from tkinter import * import random import simpleaudio as sa
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)
#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 True click = 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 random import simpleaudio as sa 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) #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 True click = True 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) def computerPick(): choice = random.choice(["Rock","Paper","Scissor","Lizard","Spock"]) return choice def 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實現剪刀石頭布小遊戲的詳細內容。更多資訊請關注PHP中文網其他相關文章!