视频游戏一直是我们生活中有趣的一部分,让我们使用Python创建一个基本版本的石头剪刀游戏。写完后我们将拥有自己开发的小游戏。
让我们开始这个游戏。
我们将创建一个石头,纸,剪刀游戏,我们将在其中与计算机对战,我们将能够保持分数,并且我们将添加一次又一次玩的能力。
我们将使用Python及其库。我们将使用的第一个库是Tkinter,它是python提供的广泛使用的GUI库,我们不必单独安装它,因为它附带python本身。
接下来,我们将使用随机库生成随机数,以确定计算机的选择。
使用Python和Tkinter,特别是Tkinter小部件的基本了解将有助于轻松完成这个项目。但不要担心,因为在我们构建项目时,本文将提供每行代码的解释。您可以自由选择您选择的任何IDE。
私信发送: 石头剪刀布
from tkinter import *
import random
代码
Comp_dict={
"0":"Rock",
"1":"Paper",
"2":"Scissor"
}
#defining Global Variables
your_choice=""
Comp_choice=""
computer_score=0
your_score=0
root=Tk()
root.title("ProjectGurukul Rock Paper Scissor")
root.geometry('270x200')
root.config(bg="sky blue")
#text widget to display choices
text_to_display=Text(root,height=3,width=30)
text_to_display.grid(row=0,columnspan=5,pady=10)
#buttons defined accordingly
bttn_rock=Button(root,text="Rock",width=6,command=rock)
bttn_rock.grid(row=2,column=0,padx=10)
bttn_paper=Button(root,text="Paper",width=6,command=paper)
bttn_paper.grid(row=2,column=1,padx=5)
bttn_Scissor=Button(root,text="Scissor",width=6,command=scissor)
bttn_Scissor.grid(row=2,column=2,padx=10)
#Widget to add label specified by text
label_scores=Label(root,text="Your score vs Computer's score")
label_scores.grid(row=3,columnspan=5,pady=8)
text_to_scores=Text(root,height=1,width=30)
text_to_scores.grid(row=4,columnspan=5,pady=5)
#play again button to start the game again
Play_again=Button(root,text="Play Again",command=Playagain)
Play_again.grid(row=5,columnspan=3)
mainloop()
解释
#function to clear the text area where choices are displayed
def Playagain():
text_to_display.delete("1.0","end")
#function to update points after every game
def points():
text_to_scores.delete("1.0","end")
text_to_scores.insert(END,f" {your_score} {computer_score}")
#function to define what happens when user select Rock
def rock():
global computer_score
global your_score
your_choice="Rock"
#choosing random variable from the above defined dictionary
Comp_choice=Comp_dict[str(random.randint(0,2))]
#to display choices
text_to_display.insert(END,f"Your Choice: {your_choice}
Computer's Choice: {Comp_choice}"
)
#to increase the scores accordingly
if Comp_choice=="Paper":
computer_score+=1
if Comp_choice=="Scissor":
your_score+=1
points()
#same as the above function
def paper():
global computer_score
global your_score
your_choice="Paper"
Comp_choice=Comp_dict[str(random.randint(0,2))]
text_to_display.insert(END,f"Your Choice: {your_choice}
Computer's Choice: {Comp_choice}"
)
if Comp_choice=="Scissor":
computer_score+=1
if Comp_choice=="Rock":
your_score+=1
points()
def scissor():
global computer_score
global your_score
your_choice="Scissor"
Comp_choice=Comp_dict[str(random.randint(0,2))]
text_to_display.insert(END,f"Your Choice: {your_choice}
Computer's Choice: {Comp_choice}"
)
if Comp_choice=="Rock":
computer_score+=1
if Comp_choice=="Paper":
your_score+=1
points()
恭喜!我们已经使用 Python 成功地创建了我们自己的石头剪刀布游戏。通过这个项目,我们学到了很多关于 python 及其库的知识。第一个是 Tkinter 库,一个广泛使用的 GUI 库和它提供的各种其他小部件,然后我们了解了 python 用于生成随机数的 random() 库。我们还学习了 Python 中使用的一种重要数据结构,称为字典。
留言与评论(共有 0 条评论) “” |