Python Tkinter教程系列01:剪刀石頭布遊戲

編寫剪刀石頭布遊戲

讓咱們使用Python 3和Tkinter開發相同的遊戲。咱們能夠將遊戲命名爲Rock-Paper-Scissors-Lizard-Spockhtml

規則和玩法

  1. Rock crushes Scissors
  2. Rock crushes Lizard
  3. Paper covers Rock
  4. Paper disproves Spock
  5. Scissors cuts Paper
  6. Scissors decapitates Lizard
  7. Lizard poisons Spock
  8. Lizard eats paper
  9. Spock smashes Scissors
  10. Spock vaporizes Rock
  11. Two same objects is a draw

程序演練

當用戶運行程序時,他們必須單擊五個可用對象之一:api

  • Rock
  • Paper
  • Scissors
  • Lizard
  • Spock

當用戶選擇一個對象時,咱們的程序將隨機選擇一個對象。而後,它將經過一組規則來聲明用戶是贏,輸仍是畫遊戲。結果將顯示在應用程序的第二行。markdown

當用戶按下任何按鈕時,遊戲將從新開始。若是用戶想要關閉遊戲,則能夠按關閉按鈕。在遊戲開始時,咱們具備用於特定對象的手形符號。如今,當用戶選擇一個對象時,它將轉換爲圖形圖像。咱們的程序還選擇了一個對象,它將顯示所選對象的圖形圖像。Python Tkinter教程系列01:編寫簡單版剪刀石頭布遊戲插圖(4)dom

Python Tkinter教程系列01:編寫簡單版剪刀石頭布遊戲函數

用Python實現(10個步驟)

如今咱們已經有了剪刀石頭布遊戲的意義,讓咱們逐步介紹Python的過程。oop

1.導入所需的庫

#Import the required libraries :
from tkinter import *
import random
import 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()
複製代碼

如今,咱們將使用一些將在各類事件中播放的聲音文件。當咱們的程序啓動時,它將播放開始文件。當用戶贏得遊戲,輸掉遊戲或繪製遊戲時,咱們將播放其餘三個文件。ui

須要注意的一件事是它僅接受.wav文件。首先,咱們須要將聲音文件加載到對象中。而後咱們能夠.play( )在須要時使用方法播放它。spa

4.爲咱們的應用程序加載圖像

咱們將在應用程序中使用各類圖像。要首先使用這些圖像,咱們須要加載這些圖像。在這裏,咱們將使用PhotoImage類加載圖像。code

#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")
複製代碼

首先,咱們爲物體準備了手部圖像。遊戲開始時將向用戶顯示全部五個圖像。用戶必須從那些圖像中選擇一個對象。orm

用戶單擊圖像後,咱們的程序將向咱們顯示該對象的圖形圖像。必須選擇一個對象,咱們的程序也將選擇一個對象。咱們的程序將僅顯示這兩個圖形圖像,而後其他圖像將消失。

如今,咱們顯示一個簡單的決策圖像,當結果可用時,它將更改其圖像。咱們的結果有不一樣的圖像。

  • 若是用戶獲勝
  • 若是用戶輸了
  • 若是有平局

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遊戲的完整代碼(IT小站)。

#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()
複製代碼
相關文章
相關標籤/搜索