관련 무료 학습 권장사항: python 비디오 튜토리얼
가위바위보 게임 작성
Let 우리는 Python 3과 Tkinter를 사용하여 동일한 게임을 개발합니다. 게임 이름을 Rock-Paper-Scissors-Lizard-Spock로 지정할 수 있습니다.
규칙 및 게임 플레이
바위가 가위를 으깨요
바위가 도마뱀을 으깨요
종이가 바위를 덮음
종이가 스팍을 반증함
가위가 종이를 자르다
가위가 도마뱀의 목을 베다
도마뱀이 스팍을 독살함
L 이자드가 종이를 먹습니다
Spock이 박살냅니다 가위
스팍은 바위를 증발시킨다
두 개의 동일한 물체가 무승부 스팍
학습에 어려움을 겪고 파이썬 학습 및 커뮤니케이션 환경을 찾고 싶다면 우리 파이썬 서클에 가입할 수 있습니다. 스커트번호 930900780, 그리고 많은 시간을 절약하고 어려운 문제에 직면하는 일을 줄여줄 Python 학습 자료를 받으세요.
사용자가 개체를 선택하면 프로그램이 개체를 무작위로 선택합니다. 그런 다음 일련의 규칙을 통해 사용자가 게임에서 승리, 패배 또는 무승부를 선언합니다. 결과는 애플리케이션의 두 번째 줄에 표시됩니다. 사용자가 아무 버튼이나 누르면 게임이 다시 시작됩니다. 사용자가 게임을 닫고 싶다면 닫기 버튼을 누르면 됩니다. 게임이 시작될 때 특정 개체에 대한 손 기호가 있습니다. 이제 사용자가 개체를 선택하면 해당 개체가 그래픽 이미지로 변환됩니다. 우리 프로그램은 또한 개체를 선택하고 선택한 개체의 그래픽 이미지를 표시합니다. 파이썬으로 구현(10단계)이제 가위바위보 게임의 의미를 알았으니, 파이썬의 과정을 단계별로 소개하겠습니다.1. 필수 라이브러리 가져오기
#Import the required libraries : from tkinter import * import random import simpleaudio as sa
tkinter: 애플리케이션에 위젯 추가
random: 임의의 숫자 생성
simpleaudio: 사운드 파일 재생
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")
사용자가 패배한 경우
동점인 경우
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
5개 버튼의 변수를 초기화합니다.
여기서 최종 결과를 보여주는 결과 버튼을 만들었습니다.
False로 설정될 때까지 프로그램이 계속 실행되도록 click 변수를 True로 설정했습니다. 이에 대해서는 다음 몇 가지 항목에서 자세히 살펴보겠습니다.
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()
想了解更多编程学习,敬请关注php培训栏目!
위 내용은 Python Tkinter를 사용하여 가위바위보 게임 구현하기의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!