Python程序石头剪刀游戏

视频游戏一直是我们生活中有趣的一部分,让我们使用Python创建一个基本版本的石头剪刀游戏。写完后我们将拥有自己开发的小游戏。

让我们开始这个游戏。

关于Python石头剪刀布游戏

我们将创建一个石头,纸,剪刀游戏,我们将在其中与计算机对战,我们将能够保持分数,并且我们将添加一次又一次玩的能力。

我们将使用Python及其库。我们将使用的第一个库是Tkinter,它是python提供的广泛使用的GUI库,我们不必单独安装它,因为它附带python本身。

接下来,我们将使用随机库生成随机数,以确定计算机的选择。

项目先决条件

使用Python和Tkinter,特别是Tkinter小部件的基本了解将有助于轻松完成这个项目。但不要担心,因为在我们构建项目时,本文将提供每行代码的解释。您可以自由选择您选择的任何IDE。

下载石头剪刀程序源码的方式

私信发送: 石头剪刀布

使用Python开发石头剪刀布项目的步骤

  1. 导入重要库
  2. 定义变量和字典
  3. 创建Python石头剪刀布项目的整体布局
  4. 创建重要函数

1. 导入重要库

from tkinter import *
import random
  • 首先,我们导入 tkinter 库,一个广泛使用的 GUI 库,然后我们导入 random。
  • random 基本上用于从给定范围生成随机数。

2.定义重要的变量和字典

代码

Comp_dict={
         "0":"Rock",
         "1":"Paper",
         "2":"Scissor"
      }
     #defining Global Variables
     your_choice=""
     Comp_choice=""
     computer_score=0
     your_score=0
  • Comp_dict 是一个字典,我们将从中选择与键“0”、“1”或“2”对应的值,这由我们的随机函数确定。
  • 接下来,我们定义了一组全局变量,your_choice 存储用户的选择,comp_choice 存储计算机的选择,然后下一组变量存储各自的分数。

3.创建Python石头剪刀布项目的整体布局

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

解释

  • Tk()是一个顶级小部件,用于创建我们将在其中构建项目的主应用程序窗口。
  • title()方法用于为我们的应用程序命名,该名称显示在顶部。
  • root.geometry()用于为我们的根窗口提供尺寸,而 root.config(bg=”sky blue”) 指定我们窗口的背景颜色。
  • Text()小部件可用于多种用途,基本用途是从用户那里获取多行输入或向用户显示多行文本。
    我们将使用这个小部件来显示选择和分数。
  • grid()小部件是一个几何管理器,它在将小部件放入根窗口之前以基于网格的方式正确地组织小部件。
  • Button()小部件用于为我们的石头剪刀布应用程序制作按钮。我们已经给出了许多参数,root 基本上指定按钮应该放在我们的根窗口中。然后, text 指定要在按钮上显示的文本,而 width 为我们的按钮提供宽度。最后,该命令指定单击按钮时要执行的功能。
  • Label()小部件用于显示某些内容,它可以是一些文本或图像。
  • mainloop() 方法基本上运行 Tkinter 事件循环,运行并显示我们在代码中编写的所有内容。

4.创建重要功能

#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()
  • 当单击 Play Again 按钮时调用Playagain(),它会清除显示下一轮游戏选项的文本区域。
  • points()函数在每轮游戏后更新分数,它通过删除显示分数的文本区域然后插入更新的分数来实现。
  • 当点击 Rock 按钮时调用rock() 。我们必须通过在它们前面写关键字global来指定变量computer_score和your_score是全局变量。现在,random.randint(0,2) 返回一个介于 0 和 2 之间的随机整数,包括 0 和 2。在此之后,我们使用 str() 函数将其类型转换为字符串。然后我们在名为 Comp_choice 的字典中查找与此键对应的值。下一行基本上显示了选择。之后我们根据选择更新分数,然后我们调用 points() 函数来显示更新的分数。
  • paper()函数与 rock 函数完全相同,只是变量 your_choice 发生了变化。
  • scissor()函数的解释与上述函数相同。

代码最终输出


概括

恭喜!我们已经使用 Python 成功地创建了我们自己的石头剪刀布游戏。通过这个项目,我们学到了很多关于 python 及其库的知识。第一个是 Tkinter 库,一个广泛使用的 GUI 库和它提供的各种其他小部件,然后我们了解了 python 用于生成随机数的 random() 库。我们还学习了 Python 中使用的一种重要数据结构,称为字典。

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章