如何用Python Tkinter實現(xiàn)剪刀石頭布小游戲社牲?

編寫剪刀石頭布游戲

讓我們使用Python 3和Tkinter開發(fā)相同的游戲肝谭。我們可以將游戲命名為Rock-Paper-Scissors-Lizard-Spock

規(guī)則和玩法

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學習交流環(huán)境,可以加入我們的python圈评抚,裙號930900780豹缀,可領取python學習資料,會節(jié)約很多時間慨代,減少很多遇到的難題邢笙。

當用戶選擇一個對象時,我們的程序?qū)㈦S機選擇一個對象鱼响。然后鸣剪,它將通過一組規(guī)則來聲明用戶是贏,輸還是畫游戲。結(jié)果將顯示在應用程序的第二行筐骇。

當用戶按下任何按鈕時债鸡,游戲?qū)⒅匦麻_始。如果用戶想要關(guān)閉游戲铛纬,則可以按關(guān)閉按鈕厌均。在游戲開始時,我們具有用于特定對象的手形符號「嫠簦現(xiàn)在棺弊,當用戶選擇一個對象時,它將轉(zhuǎn)換為圖形圖像擒悬。我們的程序還選擇了一個對象模她,它將顯示所選對象的圖形圖像。

用Python實現(xiàn)(10個步驟)

現(xiàn)在我們已經(jīng)有了剪刀石頭布游戲的意義懂牧,讓我們逐步介紹Python的過程侈净。

1.導入所需的庫

#Import the required libraries :

from tkinter import *

import random

import simpleaudio as sa

復制代碼

tkinter:在我們的應用程序中添加小部件

random:生成一個隨機數(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( ):我們使用它來指定應用程序的背景色僧凤。在我們的情況下畜侦,背景顏色為黑色。

root.geometry( ):我們使用它來指定我們的應用程序窗口將在哪個位置打開躯保。它將在左上角打開旋膳。

root.iconbitmap( ):我們使用它來設置應用程序窗口標題欄中的圖標涮俄。此功能僅接受.ico文件润努。

root.title( ):我們使用它來設置應用程序的標題颠印。

root.resizable( ):在這里我們使用它來防止用戶調(diào)整主窗口的大小换淆。

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()

復制代碼

現(xiàn)在轴脐,我們將使用一些將在各種事件中播放的聲音文件咧擂。當我們的程序啟動時宫患,它將播放開始文件麦到。當用戶贏得游戲振惰,輸?shù)粲螒蚧蚶L制游戲時歌溉,我們將播放其他三個文件。

需要注意的一件事是它僅接受.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")

復制代碼

首先,我們?yōu)槲矬w準備了手部圖像电抚。游戲開始時將向用戶顯示所有五個圖像惕稻。用戶必須從那些圖像中選擇一個對象。

用戶單擊圖像后蝙叛,我們的程序?qū)⑾蛭覀冿@示該對象的圖形圖像俺祠。必須選擇一個對象,我們的程序也將選擇一個對象借帘。我們的程序?qū)H顯示這兩個圖形圖像蜘渣,然后其余圖像將消失。

現(xiàn)在肺然,我們顯示一個簡單的決策圖像蔫缸,當結(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變量設置為True它匕,以便我們的程序繼續(xù)運行直到將其設置為False。在接下來的幾點中窖认,我們將看到更多有關(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)建按鈕扑浸。我們將為按鈕設置圖像烧给,當按下按鈕時,它將youPick( )與單擊的對象的字符串名稱一起起作用喝噪。

然后础嫡,使用該.grid( )方法將按鈕排列在主窗口上。在這里酝惧,我們在的第一行添加一個空格.grid_rowconfigure( )榴鼎。然后,將結(jié)果按鈕放在第二行晚唇。我們正在使用columnspan結(jié)果按鈕居中巫财。

7.輪到計算機了

我們的計算機將隨機選擇五個可用對象之一,并為此返回一個字符串值哩陕。

def computerPick():

? ? choice = random.choice(["Rock","Paper","Scissor","Lizard","Spock"])

? ? return choice

復制代碼

8.主要功能: youPick( )

在此功能中平项,我們的程序?qū)@示所選對象的圖形圖像赫舒。它將刪除其余的對象。它還將應用一組規(guī)則來生成結(jié)果闽瓢。

def youPick(yourChoice):

? ? global click

? ? ? ? compPick = computerPick()

? ? ? ? if click==True:

復制代碼

我們將計算機的選擇存儲在compPick變量中接癌。我們將使用它來確定結(jié)果。

用戶選擇Rock:

如果用戶選擇Rock鸳粉,則使用此代碼塊扔涧。play( )函數(shù)中的命令沿字符串發(fā)送,該字符串代表用戶選擇的對象届谈。我們將其存儲在yourChoice變量中】菀梗現(xiàn)在,計算機有五種可能性艰山。

現(xiàn)在我們必須為每個規(guī)則制定規(guī)則『ⅲ現(xiàn)在注意,當用戶和計算機選擇一個對象時曙搬,不允許他們對其進行更改摔吏。因此,我們將click變量更改為False纵装。

現(xiàn)在征讲,由于用戶已選擇,Rock我們希望我們的第一張圖像變成巖石的圖形圖像∠鹇Γ現(xiàn)在诗箍,如果計算機選擇Rock,那么我們希望我們的第二張圖像變成圖形圖像挽唉。要更改按鈕的圖像滤祖,我們使用.configure( )方法。

我們希望其余三個圖像消失瓶籽。為了使它們消失匠童,我們使用.grid_forget( )。它還將播放繪圖音頻∷芩常現(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變量的值設置為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.調(diào)用函數(shù)

?

現(xiàn)在我們調(diào)用play函數(shù)计技,它將在內(nèi)部處理其余函數(shù)。要關(guān)閉該應用程序山橄,請按標題欄上的關(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()

最后多說一句驾胆,小編是一名python開發(fā)工程師,這里有我自己整理了一套最新的python系統(tǒng)學習教程贱呐。想要這些資料的可以進裙930900780領取丧诺。

本文章素材來源于網(wǎng)絡,如有侵權(quán)請聯(lián)系刪除奄薇。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末驳阎,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子馁蒂,更是在濱河造成了極大的恐慌呵晚,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件沫屡,死亡現(xiàn)場離奇詭異饵隙,居然都是意外死亡,警方通過查閱死者的電腦和手機沮脖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門金矛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來芯急,“玉大人,你說我怎么就攤上這事驶俊∪⑺#” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵饼酿,是天一觀的道長榕酒。 經(jīng)常有香客問我,道長故俐,這世上最難降的妖魔是什么想鹰? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮购披,結(jié)果婚禮上杖挣,老公的妹妹穿的比我還像新娘。我一直安慰自己刚陡,他們只是感情好惩妇,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著筐乳,像睡著了一般歌殃。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蝙云,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天氓皱,我揣著相機與錄音,去河邊找鬼勃刨。 笑死波材,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的身隐。 我是一名探鬼主播廷区,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼贾铝!你這毒婦竟也來了隙轻?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤垢揩,失蹤者是張志新(化名)和其女友劉穎玖绿,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體叁巨,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡斑匪,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了俘种。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片秤标。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡绝淡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出苍姜,到底是詐尸還是另有隱情牢酵,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布衙猪,位于F島的核電站馍乙,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏垫释。R本人自食惡果不足惜丝格,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望棵譬。 院中可真熱鬧显蝌,春花似錦、人聲如沸订咸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽脏嚷。三九已至骆撇,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間父叙,已是汗流浹背神郊。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留趾唱,地道東北人涌乳。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像甜癞,于是被迫代替她去往敵國和親爷怀。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354