搜尋
首頁後端開發Python教學用Python Tkinter實現剪刀石頭布小遊戲

python影片教學專欄介紹使用Tkinter實作剪刀石頭布

用Python Tkinter實現剪刀石頭布小遊戲

#相關免費學習推薦: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用Python Tkinter實現剪刀石頭布小遊戲

Spock

如果大家在學習中遇到困難,想找一個python學習交流環境,可以加入我們的python圈,裙號930900780,可領取python學習資料,會節約很多時間,減少很多遇到的難題。

當使用者選擇一個物件時,我們的程式將隨機選擇一個物件。然後,它將透過一組規則來聲明用戶是贏,輸還是畫遊戲。結果將顯示在應用程式的第二行。

當使用者按下任何按鈕時,遊戲將重新開始。如果使用者想要關閉遊戲,則可以按下關閉按鈕。在遊戲開始時,我們具有用於特定物件的手形符號。現在,當使用者選擇一個物件時,它將轉換為圖形圖像。我們的程式還選擇了一個對象,它將顯示所選對象的圖形圖像。

用Python實現(10個步驟)

    現在我們已經有了剪刀石頭布遊戲的意義,讓我們逐步介紹Python的過程。
  • 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()

現在,我們將使用一些將在各種事件中播放的聲音檔案。當我們的程式啟動時,它將播放開始檔案。當用戶贏得遊戲,輸掉遊戲或繪製遊戲時,我們將播放其他三個檔案。

用Python Tkinter實現剪刀石頭布小遊戲需要注意的一件事是它只接受.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.调用函数

用Python Tkinter實現剪刀石頭布小遊戲

现在我们调用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中文網其他相關文章!

陳述
本文轉載於:简书。如有侵權,請聯絡admin@php.cn刪除
Python vs.C:申請和用例Python vs.C:申請和用例Apr 12, 2025 am 12:01 AM

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。Python以简洁和强大的生态系统著称,C 则以高性能和底层控制能力闻名。

2小時的Python計劃:一種現實的方法2小時的Python計劃:一種現實的方法Apr 11, 2025 am 12:04 AM

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python:探索其主要應用程序Python:探索其主要應用程序Apr 10, 2025 am 09:41 AM

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

您可以在2小時內學到多少python?您可以在2小時內學到多少python?Apr 09, 2025 pm 04:33 PM

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內通過項目和問題驅動的方式教計算機小白編程基礎?如何在10小時內通過項目和問題驅動的方式教計算機小白編程基礎?Apr 02, 2025 am 07:18 AM

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

如何在使用 Fiddler Everywhere 進行中間人讀取時避免被瀏覽器檢測到?如何在使用 Fiddler Everywhere 進行中間人讀取時避免被瀏覽器檢測到?Apr 02, 2025 am 07:15 AM

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Python 3.6加載Pickle文件報錯"__builtin__"模塊未找到怎麼辦?Python 3.6加載Pickle文件報錯"__builtin__"模塊未找到怎麼辦?Apr 02, 2025 am 07:12 AM

Python3.6環境下加載Pickle文件報錯:ModuleNotFoundError:Nomodulenamed...

如何提高jieba分詞在景區評論分析中的準確性?如何提高jieba分詞在景區評論分析中的準確性?Apr 02, 2025 am 07:09 AM

如何解決jieba分詞在景區評論分析中的問題?當我們在進行景區評論分析時,往往會使用jieba分詞工具來處理文�...

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能