使用Python Tkinter實(shí)現(xiàn)剪刀石頭布小游戲功能
編寫剪刀石頭布游戲
讓我們使用Python 3和Tkinter開發(fā)相同的游戲。我們可以將游戲命名為Rock-Paper-Scissors-Lizard-Spock。
規(guī)則和玩法
- ock 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
程序演練
當(dāng)用戶運(yùn)行程序時,他們必須單擊五個可用對象之一:
- Rock
- Paper
- Scissors
- Lizard
- Spock

當(dāng)用戶選擇一個對象時,我們的程序?qū)㈦S機(jī)選擇一個對象。然后,它將通過一組規(guī)則來聲明用戶是贏,輸還是畫游戲。結(jié)果將顯示在應(yīng)用程序的第二行。
當(dāng)用戶按下任何按鈕時,游戲?qū)⒅匦麻_始。如果用戶想要關(guān)閉游戲,則可以按關(guān)閉按鈕。在游戲開始時,我們具有用于特定對象的手形符號?,F(xiàn)在,當(dāng)用戶選擇一個對象時,它將轉(zhuǎn)換為圖形圖像。我們的程序還選擇了一個對象,它將顯示所選對象的圖形圖像。
用Python實(shí)現(xiàn)(10個步驟)
現(xiàn)在我們已經(jīng)有了剪刀石頭布游戲的意義,讓我們逐步介紹Python的過程。
1.導(dǎo)入所需的庫
#Import the required libraries : from tkinter import * import random import simpleaudio as sa
- tkinter:在我們的應(yīng)用程序中添加小部件
- random:生成一個隨機(jī)數(shù)
- simpleaudio:播放聲音文件
2.創(chuàng)建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( ):我們使用它來指定應(yīng)用程序的背景色。在我們的情況下,背景顏色為黑色。
- root.geometry( ):我們使用它來指定我們的應(yīng)用程序窗口將在哪個位置打開。它將在左上角打開。
- root.iconbitmap( ):我們使用它來設(shè)置應(yīng)用程序窗口標(biāo)題欄中的圖標(biāo)。此功能僅接受.ico文件。
- root.title( ):我們使用它來設(shè)置應(yīng)用程序的標(biāo)題。
- root.resizable( ):在這里我們使用它來防止用戶調(diào)整主窗口的大小。
3.導(dǎo)入聲音文件
#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()
現(xiàn)在,我們將使用一些將在各種事件中播放的聲音文件。當(dāng)我們的程序啟動時,它將播放開始文件。當(dāng)用戶贏得游戲,輸?shù)粲螒蚧蚶L制游戲時,我們將播放其他三個文件。
需要注意的一件事是它僅接受.wav文件。首先,我們需要將聲音文件加載到對象中。然后我們可以.play( )在需要時使用方法播放它。

4.為我們的應(yīng)用程序加載圖像
我們將在應(yīng)用程序中使用各種圖像。要首先使用這些圖像,我們需要加載這些圖像。在這里,我們將使用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")
首先,我們?yōu)槲矬w準(zhǔn)備了手部圖像。游戲開始時將向用戶顯示所有五個圖像。用戶必須從那些圖像中選擇一個對象。
用戶單擊圖像后,我們的程序?qū)⑾蛭覀冿@示該對象的圖形圖像。必須選擇一個對象,我們的程序也將選擇一個對象。我們的程序?qū)H顯示這兩個圖形圖像,然后其余圖像將消失。
現(xiàn)在,我們顯示一個簡單的決策圖像,當(dāng)結(jié)果可用時,它將更改其圖像。我們的結(jié)果有不同的圖像。
- 如果用戶獲勝
- 如果用戶輸了
- 如果有平局
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
- 初始化五個按鈕的變量。
- 在這里,我們創(chuàng)建了結(jié)果按鈕,它將向我們顯示最終結(jié)果。
- 我們將click變量設(shè)置為True,以便我們的程序繼續(xù)運(yùn)行直到將其設(shè)置為False。在接下來的幾點(diǎn)中,我們將看到更多有關(guān)此的內(nèi)容。
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)
在這里,我們?yōu)閷ο髣?chuàng)建按鈕。我們將為按鈕設(shè)置圖像,當(dāng)按下按鈕時,它將youPick( )與單擊的對象的字符串名稱一起起作用。
然后,使用該.grid( )方法將按鈕排列在主窗口上。在這里,我們在的第一行添加一個空格.grid_rowconfigure( )。然后,將結(jié)果按鈕放在第二行。我們正在使用columnspan結(jié)果按鈕居中。
7.輪到計(jì)算機(jī)了
我們的計(jì)算機(jī)將隨機(jī)選擇五個可用對象之一,并為此返回一個字符串值。
def computerPick(): choice = random.choice(["Rock","Paper","Scissor","Lizard","Spock"]) return choice
8.主要功能: youPick( )
在此功能中,我們的程序?qū)@示所選對象的圖形圖像。它將刪除其余的對象。它還將應(yīng)用一組規(guī)則來生成結(jié)果。
def youPick(yourChoice): global click compPick = computerPick() if click==True:
我們將計(jì)算機(jī)的選擇存儲在compPick變量中。我們將使用它來確定結(jié)果。
用戶選擇Rock:
如果用戶選擇Rock,則使用此代碼塊。play( )函數(shù)中的命令沿字符串發(fā)送,該字符串代表用戶選擇的對象。我們將其存儲在yourChoice變量中。現(xiàn)在,計(jì)算機(jī)有五種可能性。
現(xiàn)在我們必須為每個規(guī)則制定規(guī)則?,F(xiàn)在注意,當(dāng)用戶和計(jì)算機(jī)選擇一個對象時,不允許他們對其進(jìn)行更改。因此,我們將click變量更改為False。
現(xiàn)在,由于用戶已選擇,Rock我們希望我們的第一張圖像變成巖石的圖形圖像?,F(xiàn)在,如果計(jì)算機(jī)選擇Rock,那么我們希望我們的第二張圖像變成圖形圖像。要更改按鈕的圖像,我們使用.configure( )方法。
我們希望其余三個圖像消失。為了使它們消失,我們使用.grid_forget( )。它還將播放繪圖音頻?,F(xiàn)在,我們?yōu)槠溆鄬ο箝_發(fā)類似的規(guī)則。
def computerPick():choice = random.choice(["Rock","Paper","Scissor","Lizard","Spock"])return choice
用戶選擇紙張:
請參閱上面的規(guī)則,以了解用戶選擇“紙張”時的規(guī)則。查看下面的代碼,該代碼遵循與Rock相同的規(guī)則。
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
用戶選擇剪刀:
請從上方查看規(guī)則,以了解用戶選擇剪刀時的規(guī)則。查看下面的代碼,該代碼遵循與Rock and Paper相同的規(guī)則。
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"
請從上方查看規(guī)則,以了解用戶選擇蜥蜴的規(guī)則。查看下面的代碼,該代碼遵循與其他代碼相同的規(guī)則。
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:
請從上方查看規(guī)則,以了解用戶選擇Spock的規(guī)則。查看下面的代碼,該代碼遵循與其他代碼相同的規(guī)則。
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.再玩一次
得到結(jié)果后,如果要再次播放,只需單擊任何按鈕。它將轉(zhuǎn)換為原始的手部圖像。現(xiàn)在,我們必須取回那些消失的圖像。我們將click變量的值設(shè)置為True。然后,我們將播放開始聲音文件,以便在用戶進(jìn)入新游戲時將播放音頻。
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.調(diào)用函數(shù)

現(xiàn)在我們調(diào)用play函數(shù),它將在內(nèi)部處理其余函數(shù)。要關(guān)閉該應(yīng)用程序,請按標(biāo)題欄上的關(guān)閉按鈕。
#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()
到此這篇關(guān)于使用Python Tkinter實(shí)現(xiàn)剪刀石頭布小游戲功能的文章就介紹到這了,更多相關(guān)Python Tkinter剪刀石頭布小游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python 使用 raise 語句拋出異常的流程分析
- Python tkinter之ComboBox(下拉框)的使用簡介
- Python tkinter之Bind(綁定事件)的使用示例
- python 使用tkinter+you-get實(shí)現(xiàn)視頻下載器
- python tkinter實(shí)現(xiàn)連連看游戲
- python 實(shí)現(xiàn)圍棋游戲(純tkinter gui)
- python tkinter的消息框模塊(messagebox,simpledialog)
- Python GUI之tkinter窗口視窗教程大集合(推薦)
- 關(guān)于python tushare Tkinter構(gòu)建的簡單股票可視化查詢系統(tǒng)(Beta v0.13)
- 使用Python中tkinter庫簡單gui界面制作及打包成exe的操作方法(二)
- PHP語言對接抖音快手小紅書視頻/圖片去水印API接口源碼
- python tkinter實(shí)現(xiàn)下載進(jìn)度條及抖音視頻去水印原理
相關(guān)文章
Python趣味實(shí)戰(zhàn)之手把手教你實(shí)現(xiàn)舉牌小人生成器
前幾天寫了一個嬰兒級別的爬蟲圖文教程,大家很喜歡.恰好周末看到有人咨詢這個 “舉牌小人” 怎么做?基于此,我想借此為大家再寫一篇 “爬蟲應(yīng)用” 的文章,教你制作一個好玩兒的 “舉牌小人” ,需要的朋友可以參考下2021-06-06
python基礎(chǔ)教程項(xiàng)目五之虛擬茶話會
這篇文章主要為大家詳細(xì)介紹了python基礎(chǔ)教程項(xiàng)目五之虛擬茶話會,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
關(guān)于Python中幾個有趣的函數(shù)和推導(dǎo)式解析
這篇文章主要介紹了關(guān)于Python中幾個有趣的函數(shù)和推導(dǎo)式解析,推導(dǎo)式comprehensions,又稱解析式,是Python的一種獨(dú)有特性,推導(dǎo)式是可以從一個數(shù)據(jù)序列構(gòu)建另一個新的數(shù)據(jù)序列的結(jié)構(gòu)體,需要的朋友可以參考下2023-08-08
python列表排序用?sort()和sorted()的區(qū)別
這篇文章主要介紹了python列表排序用?sort()和sorted()的區(qū)別,主要比較?Python?中用于列表排序的兩種函數(shù)?sort()?和?sorted(),選擇合適的排序函數(shù),下文詳細(xì)內(nèi)容需要的小伙伴可以參考一下2022-03-03
python3用PIL把圖片轉(zhuǎn)換為RGB圖片的實(shí)例
今天小編就為大家分享一篇python3用PIL把圖片轉(zhuǎn)換為RGB圖片的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
python實(shí)現(xiàn)兩張圖片拼接為一張圖片并保存
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)兩張圖片拼接為一張圖片并保存,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
Django 全局的static和templates的使用詳解
這篇文章主要介紹了Django 全局的static和templates的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07

