使用Python Tkinter实现剪刀石头布小游戏功能

编写剪刀石头布游戏

让我们使用Python 3和Tkinter开发相同的游戏。我们可以将游戏命名为Rock-Paper-Scissors-Lizard-Spock

规则和玩法

  1. ock 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

程序演练

当用户运行程序时,他们必须单击五个可用对象之一:

  1. Rock
  2. Paper
  3. Scissors
  4. Lizard
  5. Spock

当用户选择一个对象时,我们的程序将随机选择一个对象。然后,它将通过一组规则来声明用户是赢,输还是画游戏。结果将显示在应用程序的第二行。

当用户按下任何按钮时,游戏将重新开始。如果用户想要关闭游戏,则可以按关闭按钮。在游戏开始时,我们具有用于特定对象的手形符号。现在,当用户选择一个对象时,它将转换为图形图像。我们的程序还选择了一个对象,它将显示所选对象的图形图像。

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

现在,我们将使用一些将在各种事件中播放的声音文件。当我们的程序启动时,它将播放开始文件。当用户赢得游戏,输掉游戏或绘制游戏时,我们将播放其他三个文件。

需要注意的一件事是它仅接受.wav文件。首先,我们需要将声音文件加载到对象中。然后我们可以.play( )在需要时使用方法播放它。

4.为我们的应用程序加载图像

我们将在应用程序中使用各种图像。要首先使用这些图像,我们需要加载这些图像。在这里,我们将使用PhotoImage类加载图像。

#Hand images :
rockHandPhoto = PhotoImage(file="Rock_1.png")
paperHandPhoto = PhotoImage(file="Paper_1.png")
scissorHandPhoto = PhotoImage(file="Scissor_1.png")
lizardHandPhoto = PhotoImage(file="Lizard_1.png")
spockHandPhoto = PhotoImage(file="Spock_1.png")

#Graphical images :
rockPhoto = PhotoImage(file="Rock_P.png")
paperPhoto = PhotoImage(file="Paper_P.png")
scissorPhoto = PhotoImage(file="Scissor_P.png")
lizardPhoto = PhotoImage(file="Lizard_P.png")
spockPhoto = PhotoImage(file="Spock_P.png")

#Decision image :
decisionPhoto = PhotoImage(file="Decision_Final.png")

#Result images :
winPhoto = PhotoImage(file="G_WIN.png")
losePhoto = PhotoImage(file="G_LOST.png")
tiePhoto = PhotoImage(file="G_DRAW.png")

首先,我们为物体准备了手部图像。游戏开始时将向用户显示所有五个图像。用户必须从那些图像中选择一个对象。

用户单击图像后,我们的程序将向我们显示该对象的图形图像。必须选择一个对象,我们的程序也将选择一个对象。我们的程序将仅显示这两个图形图像,然后其余图像将消失。

现在,我们显示一个简单的决策图像,当结果可用时,它将更改其图像。我们的结果有不同的图像。

  • 如果用户获胜
  • 如果用户输了
  • 如果有平局

5.添加Tkinter小部件

#Initialize the button variables :
rockHandButton = " "
paperHandButton = " "
scissorHandButton = " "
lizardHandButton= " "
spockHandButton = " "

#Create the result button :
resultButton = Button(root,image=decisionPhoto)

#Set the variable to True
click = True
  • 初始化五个按钮的变量。
  • 在这里,我们创建了结果按钮,它将向我们显示最终结果。
  • 我们将click变量设置为True,以便我们的程序继续运行直到将其设置为False。在接下来的几点中,我们将看到更多有关此的内容。

6. Play( )功能

def play():
 global rockHandButton,paperHandButton,scissorHandButton,lizardHandButton,spockHandButton

 #Set images and commands for buttons :
 rockHandButton = Button(root,image = rockHandPhoto, command=lambda:youPick("Rock"))
 paperHandButton = Button(root,image = paperHandPhoto, command=lambda:youPick("Paper"))
 scissorHandButton = Button(root,image = scissorHandPhoto, command=lambda:youPick("Scissor"))
 lizardHandButton = Button(root,image= lizardHandPhoto,command=lambda:youPick("Lizard"))
 spockHandButton = Button(root,image= spockHandPhoto,command=lambda:youPick("Spock"))

 #Place the buttons on window :
 rockHandButton.grid(row=0,column=0)
 paperHandButton.grid(row=0,column=1)
 scissorHandButton.grid(row=0,column=2)
 lizardHandButton.grid(row=0,column=3)
 spockHandButton.grid(row=0,column=4)

 #Add space :
 root.grid_rowconfigure(1, minsize=50) 

 #Place result button on window :
 resultButton.grid(row=2,column=0,columnspan=5)

在这里,我们为对象创建按钮。我们将为按钮设置图像,当按下按钮时,它将youPick( )与单击的对象的字符串名称一起起作用。

然后,使用该.grid( )方法将按钮排列在主窗口上。在这里,我们在的第一行添加一个空格.grid_rowconfigure( )。然后,将结果按钮放在第二行。我们正在使用columnspan结果按钮居中。

7.轮到计算机了

我们的计算机将随机选择五个可用对象之一,并为此返回一个字符串值。

def computerPick():
 choice = random.choice(["Rock","Paper","Scissor","Lizard","Spock"])
 return choice

8.主要功能: youPick( )

在此功能中,我们的程序将显示所选对象的图形图像。它将删除其余的对象。它还将应用一组规则来生成结果。

def youPick(yourChoice):
 global click
  compPick = computerPick()
  if click==True:

我们将计算机的选择存储在compPick变量中。我们将使用它来确定结果。

用户选择Rock:

如果用户选择Rock,则使用此代码块。play( )函数中的命令沿字符串发送,该字符串代表用户选择的对象。我们将其存储在yourChoice变量中。现在,计算机有五种可能性。

现在我们必须为每个规则制定规则。现在注意,当用户和计算机选择一个对象时,不允许他们对其进行更改。因此,我们将click变量更改为False。

现在,由于用户已选择,Rock我们希望我们的第一张图像变成岩石的图形图像。现在,如果计算机选择Rock,那么我们希望我们的第二张图像变成图形图像。要更改按钮的图像,我们使用.configure( )方法。

我们希望其余三个图像消失。为了使它们消失,我们使用.grid_forget( )。它还将播放绘图音频。现在,我们为其余对象开发类似的规则。

def computerPick():choice = random.choice(["Rock","Paper","Scissor","Lizard","Spock"])return choice

用户选择纸张:

请参阅上面的规则,以了解用户选择“纸张”时的规则。查看下面的代码,该代码遵循与Rock相同的规则。

elif yourChoice == "Paper":rockHandButton.configure(image=paperPhoto)if compPick == "Rock":paperHandButton.configure(image=rockPhoto)resultButton.configure(image=losePhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Lose.play()click = Falseelif compPick == "Paper":paperHandButton.configure(image=paperPhoto)resultButton.configure(image=tiePhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Draw.play()click = Falseelif compPick == "Scissor":paperHandButton.configure(image=scissorPhoto)resultButton.configure(image=losePhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Lose.play()click = Falseelif compPick =="Lizard":paperHandButton.configure(image=lizardPhoto)resultButton.configure(image=losePhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Lose.play()click = Falseelse :paperHandButton.configure(image=spockPhoto)resultButton.configure(image=winPhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Win.play()click = False

用户选择剪刀:

请从上方查看规则,以了解用户选择剪刀时的规则。查看下面的代码,该代码遵循与Rock and Paper相同的规则。

elif yourChoice=="Scissor":rockHandButton.configure(image=scissorPhoto)if compPick == "Rock":paperHandButton.configure(image=rockPhoto)resultButton.configure(image=losePhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Lose.play()click = Falseelif compPick == "Paper":paperHandButton.configure(image=paperPhoto)resultButton.configure(image=winPhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Win.play()click = Falseelif compPick=="Scissor":paperHandButton.configure(image=scissorPhoto)resultButton.configure(image=tiePhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Draw.play()click = Falseelif compPick == "Lizard":paperHandButton.configure(image=lizardPhoto)resultButton.configure(image=winPhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Win.play()click = Falseelse:paperHandButton.configure(image=spockPhoto)resultButton.configure(image=losePhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Lose.play()click = False

用户选择"Lizard"

请从上方查看规则,以了解用户选择蜥蜴的规则。查看下面的代码,该代码遵循与其他代码相同的规则。

elif yourChoice=="Lizard":rockHandButton.configure(image=lizardPhoto)if compPick == "Rock":paperHandButton.configure(image=rockPhoto)resultButton.configure(image=losePhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Lose.play()click = Falseelif compPick == "Paper":paperHandButton.configure(image=paperPhoto)resultButton.configure(image=winPhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Win.play()click = Falseelif compPick=="Scissor":paperHandButton.configure(image=scissorPhoto)resultButton.configure(image=losePhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Lose.play()click = Falseelif compPick == "Lizard":paperHandButton.configure(image=lizardPhoto)resultButton.configure(image=tiePhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Draw.play()click = Falseelse:paperHandButton.configure(image=spockPhoto)resultButton.configure(image=winPhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Win.play()click = False

用户选择Spock:

请从上方查看规则,以了解用户选择Spock的规则。查看下面的代码,该代码遵循与其他代码相同的规则。

elif yourChoice=="Spock":rockHandButton.configure(image=spockPhoto)if compPick == "Rock":paperHandButton.configure(image=rockPhoto)resultButton.configure(image=winPhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Win.play()click = Falseelif compPick == "Paper":paperHandButton.configure(image=paperPhoto)resultButton.configure(image=losePhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Lose.play()click = Falseelif compPick=="Scissor":paperHandButton.configure(image=scissorPhoto)resultButton.configure(image=winPhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Win.play()click = Falseelif compPick == "Lizard":paperHandButton.configure(image=lizardPhoto)resultButton.configure(image=losePhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Lose.play()click = Falseelse:paperHandButton.configure(image=spockPhoto)resultButton.configure(image=tiePhoto)scissorHandButton.grid_forget()lizardHandButton.grid_forget()spockHandButton.grid_forget()Draw.play()click = False

9.再玩一次

得到结果后,如果要再次播放,只需单击任何按钮。它将转换为原始的手部图像。现在,我们必须取回那些消失的图像。我们将click变量的值设置为True。然后,我们将播放开始声音文件,以便在用户进入新游戏时将播放音频。

else:
  #To reset the game :
  if yourChoice=="Rock" or yourChoice=="Paper" or yourChoice=="Scissor" or yourChoice=="Lizard" or yourChoice=="Spock":
   rockHandButton.configure(image=rockHandPhoto)
   paperHandButton.configure(image=paperHandPhoto)
   scissorHandButton.configure(image=scissorHandPhoto)
   lizardHandButton.configure(image=lizardHandPhoto)
   spockHandButton.configure(image=spockHandPhoto)
   resultButton.configure(image=decisionPhoto)

   #Get back the deleted buttons :
   scissorHandButton.grid(row=0,column=2)
   lizardHandButton.grid(row=0,column=3)
   spockHandButton.grid(row=0,column=4)

   #Set click = True :
   click=True

   #Play the sound file :
   start.play()

10.调用函数

现在我们调用play函数,它将在内部处理其余函数。要关闭该应用程序,请按标题栏上的关闭按钮。

#Calling the play function :
play()

#Enter the main loop :
root.mainloop()

放在一起

查看此Python Tkinter游戏的完整代码。

#Import the required libraries :
from tkinter import *
import random
import simpleaudio as sa

root = Tk()
root.configure(bg="#000000")
root.geometry('+0+0')
root.iconbitmap("Game.ico")
root.title("Rock-Paper-Scissor-Lizard-Spock")
root.resizable(width=False,height=False)

#To play sound files :
start = sa.WaveObject.from_wave_file("Start.wav")
Win = sa.WaveObject.from_wave_file("Win.wav")
Lose = sa.WaveObject.from_wave_file("Lose.wav")
Draw = sa.WaveObject.from_wave_file("Draw.wav")

start.play()

#Hand images :
rockHandPhoto = PhotoImage(file="Rock_1.png")
paperHandPhoto = PhotoImage(file="Paper_1.png")
scissorHandPhoto = PhotoImage(file="Scissor_1.png")
lizardHandPhoto = PhotoImage(file="Lizard_1.png")
spockHandPhoto = PhotoImage(file="Spock_1.png")

#Graphical images :
rockPhoto = PhotoImage(file="Rock_P.png")
paperPhoto = PhotoImage(file="Paper_P.png")
scissorPhoto = PhotoImage(file="Scissor_P.png")
lizardPhoto = PhotoImage(file="Lizard_P.png")
spockPhoto = PhotoImage(file="Spock_P.png")

#Decision image :
decisionPhoto = PhotoImage(file="Decision_Final.png")

#Result images :
winPhoto = PhotoImage(file="G_WIN.png")
losePhoto = PhotoImage(file="G_LOST.png")
tiePhoto = PhotoImage(file="G_DRAW.png")

#Initialize the button variables :
rockHandButton = " "
paperHandButton = " "
scissorHandButton = " "
lizardHandButton= " "
spockHandButton = " "

#Create the result button :
resultButton = Button(root,image=decisionPhoto)

#Set the variable to True
click = True

def play():
 global rockHandButton,paperHandButton,scissorHandButton,lizardHandButton,spockHandButton

 #Set images and commands for buttons :
 rockHandButton = Button(root,image = rockHandPhoto, command=lambda:youPick("Rock"))
 paperHandButton = Button(root,image = paperHandPhoto, command=lambda:youPick("Paper"))
 scissorHandButton = Button(root,image = scissorHandPhoto, command=lambda:youPick("Scissor"))
 lizardHandButton = Button(root,image= lizardHandPhoto,command=lambda:youPick("Lizard"))
 spockHandButton = Button(root,image= spockHandPhoto,command=lambda:youPick("Spock"))

 #Place the buttons on window :
 rockHandButton.grid(row=0,column=0)
 paperHandButton.grid(row=0,column=1)
 scissorHandButton.grid(row=0,column=2)
 lizardHandButton.grid(row=0,column=3)
 spockHandButton.grid(row=0,column=4)

 #Add space :
 root.grid_rowconfigure(1, minsize=50) 

 #Place result button on window :
 resultButton.grid(row=2,column=0,columnspan=5)

def computerPick():
 choice = random.choice(["Rock","Paper","Scissor","Lizard","Spock"])
 return choice

def youPick(yourChoice):
 global click

 compPick = computerPick()

 if click==True:

  if yourChoice == "Rock":

   rockHandButton.configure(image=rockPhoto)

   if compPick == "Rock":
    paperHandButton.configure(image=rockPhoto)
    resultButton.configure(image=tiePhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Draw.play()
    click = False

   elif compPick == "Paper":
    paperHandButton.configure(image=paperPhoto)
    scissorHandButton.grid_forget()
    resultButton.configure(image=losePhoto)
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Lose.play()

    click = False

   elif compPick == "Scissor":
    paperHandButton.configure(image=scissorPhoto)
    scissorHandButton.grid_forget()
    resultButton.configure(image=winPhoto)
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Win.play()
    click = False

   elif compPick =="Lizard":
    paperHandButton.configure(image=lizardPhoto)
    scissorHandButton.grid_forget()
    resultButton.configure(image=winPhoto)
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Win.play()
    click = False

   else :
    paperHandButton.configure(image=spockPhoto)
    scissorHandButton.grid_forget()
    resultButton.configure(image=losePhoto)
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Lose.play()
    click = False

  elif yourChoice == "Paper":
   rockHandButton.configure(image=paperPhoto)

   if compPick == "Rock":
    paperHandButton.configure(image=rockPhoto)
    resultButton.configure(image=losePhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Lose.play()
    click = False

   elif compPick == "Paper":
    paperHandButton.configure(image=paperPhoto)
    resultButton.configure(image=tiePhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Draw.play()
    click = False

   elif compPick == "Scissor":
    paperHandButton.configure(image=scissorPhoto)
    resultButton.configure(image=losePhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Lose.play()
    click = False

   elif compPick =="Lizard":
    paperHandButton.configure(image=lizardPhoto)
    resultButton.configure(image=losePhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Lose.play()
    click = False

   else :
    paperHandButton.configure(image=spockPhoto)
    resultButton.configure(image=winPhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Win.play()
    click = False

  elif yourChoice=="Scissor":
   rockHandButton.configure(image=scissorPhoto)
   if compPick == "Rock":
    paperHandButton.configure(image=rockPhoto)
    resultButton.configure(image=losePhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Lose.play()
    click = False

   elif compPick == "Paper":
    paperHandButton.configure(image=paperPhoto)
    resultButton.configure(image=winPhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Win.play()
    click = False

   elif compPick=="Scissor":
    paperHandButton.configure(image=scissorPhoto)
    resultButton.configure(image=tiePhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Draw.play()
    click = False

   elif compPick == "Lizard":
    paperHandButton.configure(image=lizardPhoto)
    resultButton.configure(image=winPhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Win.play()
    click = False

   else:
    paperHandButton.configure(image=spockPhoto)
    resultButton.configure(image=losePhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Lose.play()
    click = False

  elif yourChoice=="Lizard":
   rockHandButton.configure(image=lizardPhoto)
   if compPick == "Rock":
    paperHandButton.configure(image=rockPhoto)
    resultButton.configure(image=losePhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Lose.play()
    click = False

   elif compPick == "Paper":
    paperHandButton.configure(image=paperPhoto)
    resultButton.configure(image=winPhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Win.play()
    click = False

   elif compPick=="Scissor":
    paperHandButton.configure(image=scissorPhoto)
    resultButton.configure(image=losePhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Lose.play()
    click = False

   elif compPick == "Lizard":
    paperHandButton.configure(image=lizardPhoto)
    resultButton.configure(image=tiePhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Draw.play()
    click = False

   else:
    paperHandButton.configure(image=spockPhoto)
    resultButton.configure(image=winPhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Win.play()
    click = False

  elif yourChoice=="Spock":
   rockHandButton.configure(image=spockPhoto)
   if compPick == "Rock":
    paperHandButton.configure(image=rockPhoto)
    resultButton.configure(image=winPhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Win.play()
    click = False

   elif compPick == "Paper":
    paperHandButton.configure(image=paperPhoto)
    resultButton.configure(image=losePhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Lose.play()
    click = False

   elif compPick=="Scissor":
    paperHandButton.configure(image=scissorPhoto)
    resultButton.configure(image=winPhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Win.play()
    click = False

   elif compPick == "Lizard":

    paperHandButton.configure(image=lizardPhoto)
    resultButton.configure(image=losePhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Lose.play()
    click = False

   else:
    paperHandButton.configure(image=spockPhoto)
    resultButton.configure(image=tiePhoto)
    scissorHandButton.grid_forget()
    lizardHandButton.grid_forget()
    spockHandButton.grid_forget()
    Draw.play()
    click = False

 else:
  #To reset the game :
  if yourChoice=="Rock" or yourChoice=="Paper" or yourChoice=="Scissor" or yourChoice=="Lizard" or yourChoice=="Spock":
   rockHandButton.configure(image=rockHandPhoto)
   paperHandButton.configure(image=paperHandPhoto)
   scissorHandButton.configure(image=scissorHandPhoto)
   lizardHandButton.configure(image=lizardHandPhoto)
   spockHandButton.configure(image=spockHandPhoto)
   resultButton.configure(image=decisionPhoto)

   #Get back the deleted buttons :
   scissorHandButton.grid(row=0,column=2)
   lizardHandButton.grid(row=0,column=3)
   spockHandButton.grid(row=0,column=4)

   #Set click = True :
   click=True

   #Play the sound file :
   start.play()

#Calling the play function :
play()

#Enter the main loop :
root.mainloop()

到此这篇关于使用Python Tkinter实现剪刀石头布小游戏功能的文章就介绍到这了,更多相关Python Tkinter剪刀石头布小游戏内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Python TKinter如何自动关闭主窗口

    Tkinter 是 Python 的标准 GUI 库.Python 使用 Tkinter 可以快速的创建 GUI 应用程序. 由于 Tkinter 是内置到 python 的安装包中.只要安装好 Python 之后就能 import Tkinter 库.而且 IDLE 也是用 Tkinter 编写而成.对于简单的图形界面 Tkinter 还是能应付自如. 在pycharm 调试Tkinter程序的时候,关闭右上角的X 实际上并未退出进程,长期以往 再大的内存也会被耗尽. 一般就是下面的代码: "

  • Python 窗体(tkinter)下拉列表框(Combobox)实例

    废话不多说,看代码吧! import tkinter from tkinter import ttk def go(*args): #处理事件,*args表示可变参数 print(comboxlist.get()) #打印选中的值 win=tkinter.Tk() #构造窗体 comvalue=tkinter.StringVar()#窗体自带的文本,新建一个值 comboxlist=ttk.Combobox(win,textvariable=comvalue) #初始化 comboxlist["

  • Python实现剪刀石头布小游戏(与电脑对战)

    具体代码如下所述: srpgame.py #!/urs/bin/env python import random all_choice = ['石头','剪刀','布'] win_list = [['石头','剪刀'],['剪刀','布'],['布','石头']] prompt = """ (0) 石头 (1) 剪刀 (2) 布 Please input your choice(0/1/2): """ computer = random.choi

  • 使用Python Tkinter实现剪刀石头布小游戏功能

    编写剪刀石头布游戏 让我们使用Python 3和Tkinter开发相同的游戏.我们可以将游戏命名为Rock-Paper-Scissors-Lizard-Spock. 规则和玩法 ock crushes Scissors Rock crushes Lizard Paper covers Rock Paper disproves Spock Scissors cuts Paper Scissors decapitates Lizard Lizard poisons Spock Lizard eats

  • 基于python tkinter的点名小程序功能的实例代码

    代码如下所示: import datetime import json import os import random import tkinter as tk import openpyxl # 花名册文件名 excel_file_path = "花名册.xlsx"#需在当前目录创建对应花名册.xlsx # 工作表名 excel_sheet = "Sheet1" # 记录存储文件名 file_path = "name_record.json"

  • Python使用tkinter实现摇骰子小游戏功能的代码

    TKinter Python 的 GUI 库非常多,之所以选择 Tkinter,一是最为简单,二是自带库,不需下载安装,随时使用,跨平台兼容性非常好,三则是从需求出发的,Python 在实际应用中极少用于开发复杂的桌面应用,毕竟,Python 的各种 GUI 工具包都"一般得很",不具备优势. 贴吧看到的一个求助题,大致需求是:3个人摇骰子,每人摇3次,点数之和最大的获胜,支持玩家名称输入.我觉得这个题目挺有意思的,做了个界面程序,欢迎大家交流指正~ #!usr/bin/env pyt

  • Python语言编写智力问答小游戏功能

    本篇博文将使用Python代码语言简单编写一个轻松益智的小游戏,效果如下所示: 1.设计思路 本项目使用SQLite建立问答题库,每道题包括4个选项答案(3个正确答案,1个错误答案).每道题都有一定分值,根据用户的答题效率,自动计算出最后的答题成绩. 2.建立题库 使用SQLite数据库建立题库,本质上就是SQL语句,创建exam表,实现代码如下所示: #导入SQLite驱动 import sqlite3 # 连接到SQLite数据库,数据库文件是test.db # 如果文件不存在,会自动在当前

  • 用Python实现童年贪吃蛇小游戏功能的实例代码

    贪吃蛇作为一款经典小游戏,早在 1976 年就面世了,我最早接触它还是在家长的诺基亚手机中. 尽管贪吃蛇的历史相对比较久远,但它却有着十分顽强的生命力,保持经久不衰,其中很重要的原因便是游戏厂家不断的对其进行更新迭代.现在,这款游戏无论是游戏场景.规则等都变得十分丰富. 接下来,我们看一下如何通过 Python 简单的实现这款小游戏. 规则 要有游戏主界面.贪吃蛇.食物 能够控制贪吃蛇移动并获取食物 贪吃蛇吃了食物后,增加自身长度.分数,食物消失并随机生成新的食物 贪吃蛇触碰到周围边界或自己身体

  • Python+Tkinter制作猜灯谜小游戏

    目录 导语 正文 1)效果展示 2)主程序 导语 元宵节,又称上元节.灯节,是春节之后的第一个重要节日. 相传,汉文帝(前179—前157年)为庆祝周勃于正月十五勘平诸吕之乱,每逢此夜,必出宫游玩,与民同乐,在古代,夜同宵,正月又称元月,汉文帝就将正月十五定为元宵节. 随着社会和时代的变迁,元宵节的风俗习惯在不断变化,但至今仍是中国的传统节日.2008年,元宵节选入第二批国家级非物质文化遗产. 对我而言,除了吃元宵.看花灯……还有一件最重要的事情…就是… 猜灯谜!猜灯谜!!猜灯谜!!!猜谜事小,

  • Python+Tkinter实现经典井字棋小游戏

    目录 演示 介绍 官方文档 tkinter.messagebox 源码 演示 介绍 首先来介绍一下GUI库Tkinter 主要模块: tkinter Main Tkinter module. tkinter.colorchooser 让用户选择颜色的对话框. tkinter.commondialog 本文其他模块定义的对话框的基类. tkinter.filedialog 允许用户指定文件的通用对话框,用于打开或保存文件. tkinter.font 帮助操作字体的工具. tkinter.messa

  • Python+Tkinter制作股票数据抓取小程序

    目录 程序布局 抓取与保存功能 添加功能 个股查询按钮 批量查询开关 在前面的文章中,我们一起学习了如何通过 Python 抓取东方财富网的实时股票数据,链接如下 用 Python 爬取股票实时数据 今天我们就在这个基础上,实现一个 Tkinter GUI 程序,完成无代码股票抓取! 首先对于 Tkinter 相信大家都是比较了解的,如果有小伙伴对于 Tkinter 的相关用法不是特别熟悉的话,可以看如下文章 Tkinter 入门之旅 首先我们先看一下 GUI 程序的最终效果 该程序共分三个区域

  • Python实现的弹球小游戏示例

    本文实例讲述了Python实现的弹球小游戏.分享给大家供大家参考,具体如下: 弹球 1. Ball 类 draw负责移动Ball 碰撞检测,反弹,Ball检测Paddle 2.Paddle类 draw负责移动Paddle 碰撞检测,确定能不能继续 监听键盘事件 3.主循环 绘制Ball和Paddle update sleep 代码 from Tkinter import * import random import time class Ball: def __init__(self, canv

随机推荐