没有合适的资源?快使用搜索试试~ 我知道了~
使用Python Tkinter实现剪刀石头布小游戏功能
12 下载量 155 浏览量
2020-12-16
19:56:44
上传
评论 3
收藏 187KB PDF 举报
温馨提示
编写剪刀石头布游戏 让我们使用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 paper Spock smashes Scissors
资源详情
资源评论
资源推荐

使用使用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 paper
Spock smashes Scissors
Spock vaporizes Rock
Two same objects is a draw
程序演练程序演练
当用户运行程序时,他们必须单击五个可用对象之一:
Rock
Paper
Scissors
Lizard
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)
剩余7页未读,继续阅读

格式:zip 资源大小:6.5MB

格式:pdf 资源大小:27.0KB 页数:1
















格式:pdf 资源大小:42.7KB 页数:2









weixin_38710566
- 粉丝: 5
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 临时用电计算Excel表格(施工手册版).xls
- 物联网下的校园监控技术探究分析.docx
- 工程项目管理-信息管理.ppt
- (源码)基于Android的波尼音乐播放器.zip
- 高可用Redis服务架构方案.docx
- 探究式教学在中职计算机基础Excel教学中的应用.docx
- 淮河临淮岗洪水控制工程现代信息化发展规划与展望.docx
- 全国年月自学考试电子商务法概论测试试题.doc
- 农村电子商务服务站点管理与服务规范.doc
- 钢铁行业智慧工厂信息化建设解决方案.docx
- 区块链技术对供应链金融的影响研究.docx
- 信息化教学方案设计书案例.doc
- 互联网+血站物资供应管理模式初探.docx
- PHP框架开发实用技术.doc
- (源码)基于Python框架的EmbyKeeper项目.zip
- 审计信息化问题浅析.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制

评论0