剪子石头布
用VB 写的小游戏---石头剪子布
编一个程序,让你和计算机玩“石头剪子布”游戏,你可以任选石头剪子布中的一种,计算机也随机产生3种物件之一,根据“石头赢剪子,剪子赢布,布赢石头”的规则判定胜负。
根据题目要求,程序界面如图所示。
一、设计“游戏”程序界面
由题目要求,这个游戏用了5个Label 控件,分别显示游戏程序界面中的提示性标题
和游戏结果(因为“结果”是由计算机判定的,人是不能填写的,所以使用:Label 控件) ;用两个Image 控件,分别显示计算机和你的选择(物件) ;用于5个命令按钮
二、设置对象属性
三,编写代码:
随堂讲义:
(1)简单条件语句的一般格式:
If 条件表达式Then 语句1Else 语句2
说明:条件表达式的要求与块If 条件语句语句中的相同;
语句个语句2都分别可以是若干条语句(语句之间用“:”号隔开);
简单条件语句必须写在一个程序行内。
(2)条件语句的功能:如果条件成立,那么就执行语句1,否则就执行语句2。
(3)简单条件语句的省略形式
If 条件表达式Then 语句
省略格式的功能是:当If 后发条件成立时,执行Then 后的语句,若不成立,则执行条件语句的下有一个程序行。
有些题目需要判断双重或多重条件,仅用关系符号已经不够用了,下面介绍几个逻辑符号:And (与)Or (或)Not (非)
And 可以连接若干个条件。当所有条件都成立时,总的条件才能成立,其中有一个条件不成立,总条件也不能成立。
Or 也可以连接若干个条件。只要当其中一个条件成立时,总条件就可以成立,所有的条件都不成立时,总条件才不成立。
Not 表示与所设的条件相反的条件。
单击“剪子”按钮产生的事件过程(Command1_Click()),程序代码如下:
Private Sub Command1_Click()
Randomize Timer
a =1
Image2.Picture =LoadPicture("g:\剪子.jpg")
b =Fix(Rnd*3) +1
If b =1Then Image1.Picture =LoadPicture("g:\剪子1.jpg")
If b =2Then Image1.Picture =LoadPicture("g:\石头.jpg")
If b =3Then Image1.Picture =LoadPicture("g:\布.jpg")
c =b -a
If c 2And c 0Then Label4.Caption =" 你输了!"
If c =2Then Label4.Caption =" 你赢了!"
If c =0Then Label4.Caption =" 不输不赢!"
Command1.Enabled =False
Command2.Enabled =False
Command3.Enabled =False
Command4.Enabled =True
End Sub
分别用数字1、2、3表示剪子、石头、布。把“你的选择”放在变量a 中,并且将你的选择所对应的图片放人Image2中;把“计算机的选择”放在变量b 中,并且将计算机的选择所对应的图片放人Imagel 中。
单击“剪子”按钮(Commandl),则变量a 中的值为1,即a=1。这时将1所对应的物件图片放人
Image2中,即:Image2.Picture :LoadPicture("g:\剪子.jpg") 这实际上是给Image2的Picture 属性赋值,赋值用的是LoadPicture 函数,括号中("g:\剪子1.jpg") 是指出“剪子”图形文件所在的路径和文件名。括号中的引号必须是西文引号,否则会出错误。
让计算机随机产生1~3(包括1和3) 之间的一个整数来表示计算机所选物件。用到随机函数和截断函数,产生并存放在变量b 中:
Randomize Timer
b=Fix(Rndx 3)+1
根据变量b 的值来判断Imagel 中显示何种物件图形,使用如下条件语句:
If b =1Then Image1.Picture =LoadPicture("g:\剪子1.jpg")
If b =2Then Image1.Picture =LoadPicture("g:\石头.jpg")
If b =3Then Image1.Picture =LoadPicture("g:\布.jpg")
如果b=1,则将“剪子”图片放人Imagel 中;如果b=2,则将“石头”图片放人Imagel 中;如果b=3,则将“布”图片放人Imagel 中。
把变量b 中的值减去变量a 中的值,结果赋给变量c ,根据变量c 的值来判断游戏结果,程序代码如下:
c =b -a
If c 2And c 0Then Label4.Caption =" 你输了!"
If c =2Then Label4.Caption =" 你赢了!"
If c =0Then Label4.Caption =" 不输不赢!"
现在,所选择的是“剪子”,也就是a 的取值是1,如果计算机产生的随机数是3(b=3,3代表“布”),这时,由c=b-a得出变量c 的值是2,根据剪子赢布的规则,如果c=2,那么=’“你赢了!”;如果c=0时,不输不赢;除了这两种情况,你都输了(Ifc 2And c 0Then Label4.Caption =" 你输了!" 语句解释为:如果c2和c0同时成立,则将“你输了!”显示在Label4中) 。
进行选择之后,可以选择的3种物件按钮应全部失效(即不可以使用) ,而“清除”按钮(Command4)要处于有效(即可以使用) ,程序代码如下:
Command1.Enabled =False
Command2.Enabled =False
Command3.Enabled =False
Command4.Enabled =True
单击“石头”按钮和“布”按钮的事件过程与上述过程基本相似,只需改动判断变量c 的值的程序段。输入时可采取复制的方法。
说明:单击“石头”按钮,变量a 的值为2(即a=2),则将“石头”图片放人Image2中。
你选择的是“石头”,也就是a 的取值是2,如果计算机产生的随机数是1(b=1,1代表“剪子”),这时,由c=b-a得出变量c 的值是-1,根据石头赢剪子的规则,如果c=-1,那么“你赢了!”;如果c=0时,不输不赢;除了这两种情况,你都输了。程序代码如下:
If c -1And c 0Then Label4.Caption =" 你输了!"
If c =-1Then Label4.Caption =" 你赢了!"
If c =0Then Label4.Caption =" 不输不赢!"
单击“清除”按钮的事件过程(Command4_Click()),程序代码如下:
Private Sub Command4_Click()
Image1.Picture =LoadPicture("")
Image2.Picture =LoadPicture("")
Label4.Caption =""
Command1.Enabled =True
Command2.Enabled =True
Command3.Enabled =True
Command4.Enabled =False
End Sub
此段程序的功能是:单击“清除”按钮后,将Imagel 和Image2中的物件图片清除;将显示判断结果的Label4中的数据清除;使Commandl 、Command2、Command3有效,Com mand4无效。
单击“退出”按钮,结束程序。
程序代码如下:
Private Sub Command1_Click()
Randomize Timer
a =1
Image2.Picture =LoadPicture("g:\剪子.jpg")
b =Fix(Rnd*3) +1
If b =1Then Image1.Picture =LoadPicture("g:\剪子1.jpg")
If b =2Then Image1.Picture =LoadPicture("g:\石头.jpg")
If b =3Then Image1.Picture =LoadPicture("g:\布.jpg")
c =b -a
If c 2And c 0Then Label4.Caption =" 你输了!"
If c =2Then Label4.Caption =" 你赢了!"
If c =0Then Label4.Caption =" 不输不赢!"
Command1.Enabled =False
Command2.Enabled =False
Command3.Enabled =False
Command4.Enabled =True
End Sub
Private Sub Command2_Click()
Randomize Timer
a =2
Image2.Picture =LoadPicture("g:\石头.jpg")
b =Fix(Rnd*3) +1
If b =1Then Image1.Picture =LoadPicture("g:\剪子1.jpg")
If b =2Then Image1.Picture =LoadPicture("g:\石头.jpg")
If b =3Then Image1.Picture =LoadPicture("g:\布.jpg")
c =b -a
If c -1And c 0Then Label4.Caption =" 你输了!"
If c =-1Then Label4.Caption =" 你赢了!"
If c =0Then Label4.Caption =" 不输不赢!"
Command1.Enabled =False
Command2.Enabled =False
Command3.Enabled =False
Command4.Enabled =True
End Sub
Private Sub Command3_Click()
Randomize Timer
a =3
Image2.Picture =LoadPicture("g:\布.jpg")
b =Fix(Rnd*3) +1
If b =1Then Image1.Picture =LoadPicture("g:\剪子1.jpg")
If b =2Then Image1.Picture =LoadPicture("g:\石头.jpg")
If b =3Then Image1.Picture =LoadPicture("g:\布.jpg")
c =b -a
If c -1And c 0Then Label4.Caption =" 你输了!"
If c =-1Then Label4.Caption =" 你赢了!"
If c =0Then Label4.Caption =" 不输不赢!"
Command1.Enabled =False
Command2.Enabled =False
Command3.Enabled =False
Command4.Enabled =True
End Sub
Private Sub Command4_Click()
Image1.Picture =LoadPicture("")
Image2.Picture =LoadPicture("")
Label4.Caption =""
Command1.Enabled =True
Command2.Enabled =True
Command3.Enabled =True
Command4.Enabled =False
End Sub
Private Sub Command5_Click()
End
End Sub
三、调试运行程序
以“石头剪子布”为文件名存盘。
五、生成.exe 文件,
要想使VB 应用程序离开VisualBasic 环境运行(直接在indows 或DOS 下运行) ,就必须对VB 应用程序进行编译,生成.exe 文件。具体作法如下:
单击菜单栏中的“文件”菜单,在下拉式菜单中,选择“生成石头剪子布.exe…”选项,在打开的“生成
工程”对话框中选择程序所保存的文件夹和文件名,然后单击“确定”按钮,“石头剪子布.exe" 文件便生成了。