vb猜字游戏程序

vb猜字游戏程序
vb猜字游戏程序

用VB60开发猜数字小游戏

时间:2009-6-2 10:24:01 作者:来源:

随着windows95and98的流行,越来越多的人加入windows程序设计的队伍之中。以前,windows程序设计是那些训练有素的专业程序设计者才会涉足的神秘领域,几乎所有的windows程序都是用c/c++语言编写的,这对大部分普通程序设计者来说,要想高效迅速地编写出具有一定功能的windows程序就不是一件容易的事情了。

1991年,microsoft公司首次推出了visualbasicforwindows,从此,人们不用c/c++或汇编就可以编写windows程序了。到目前为止,我认为在所有的windows程序设计工具中,visualbasic是最方便的,它以一种全新的思想让程序员快捷和高效地设计出windows程序。目前,visualbasic的最高版本为6.0,本文讲述用运行于win95/98上的vb6.0来开发一个猜数字的小游戏。

首先说一说猜数字这个游戏的玩法,一开始计算机会随机产生一个不重复的四位数,你要输入四位不重复的数与计算机给出的数作对比,如果与计算机给出的数的位置相同数字相同,那么将会是1a,如果数字相同而位置相不同,将会显示1b。例如:计算机的随机数字为:1234,我猜的数字为:1356,那么这时计算机会给你提示为:1a1b,也就是说,你猜的数字中,有一位数字是猜对的,而且数字位置都对,所以显示为1a;还有一个数字也猜对了,但是位置不对,所以显示为1b。就这些了,看谁猜的次数少。

首先在form中加入一个commandbuttion控件,在command1上点击鼠标右键,选择复制,在窗体上点击鼠标右键,选择粘贴在窗体上粘贴出九个command1,此时出现对话框问你要不要创建控件数组,在此选择是。然后再加入两个commandbuttion控件,一个listbox、一个frame、一个label。设置窗体的caption 属性为“猜一猜”、borderstyle为1-fixedsingle、控件数组的captin分别为0、1、2、3、4、5、6、7、8、9,command2的caption为“确定",command3的caption为“取消",frame1的caption为“提示:",label1的cpation为“0a0b"。然后选择菜单编辑器编辑菜单为:游戏、新游戏、显示答案、结束游戏,她们的name 属性分别为:game、new、view、end。好了,其余属性使用缺省的即可,最后的界面应跟下图一样:以下是程序清单:

dimpca,pcb,pcc,pcdasinteger'电脑给出的每一位数

dimusera,userb,userc,userdasinteger'用户输入的每一位数

dimdegreeasinteger'用户猜了几次

dimnumasinteger'判断用户输入次数的变量

privatesubform_load()

'程序运行行时

'初始化

fori=0to9

command1(i).enabled=false

nexti

command2.enabled=false

command3.enabled=false

view.enabled=false

endsub

privatesubnew_click()

'开始一个新游戏时

view.enabled=true'可以看答案

list1.clear'清空列表框

degree=0

'对随机数生成器做初始化

randomize

num=1

label1.caption=0&“a"&0&“b"

'电脑给出的每一位数

pca=int(9*rnd)

do

pcb=int(9*rnd)

loopwhilepcb=pca

do

pcc=int(9*rnd)

loopwhilepcc=pcaorpcc=pcb

do

pcd=int(9*rnd)

loopwhilepcd=pcaorpcd=pcborpcd=pcc

fori=0to9

command1(i).enabled=true

nexti

command2.enabled=false

command3.enabled=true

endsub

privatesubcommand1_click(indexasinteger)

'用户输入时

'使得输入过的按钮无效

ifnum<=4then

command1(index).enabled=false

endif

'判断用户输入了几位,如果输入了四位则确认按钮有效ifnum=4then

command2.enabled=true

endif

'取得用户输入

selectcaseindex

case0

userenter(0)'调用userenter过程

case1userenter(1)

case2userenter(2)

case3userenter(3)

case4userenter(4)

case5userenter(5)

case6userenter(6)

case7userenter(7)

case8userenter(8)

case9userenter(9)

endselect

endsub

privatesubcommand2_click()

'单击确定按钮时

'判断用户输入是否正确

dima,basinteger

a=0

b=0

degree=degree+1

ifusera=pcathen

a=a+1

elseifusera=pcborusera=pccorusera=pcdthen

b=b+1

用visualbasic6.0开发猜数字小游戏endif

ifuserb=pcbthen

a=a+1

elseifuserb=pcaoruserb=pccoruserd=pcdthen

b=b+1

endif

ifuserc=pccthen

a=a+1

elseifuserc=pcaoruserc=pcboruserc=pcdthen

b=b+1

endif

ifuserd=pcdthen

a=a+1

elseifuserd=pcaoruserd=pcboruserc=pccthen

b=b+1

endif

'显示提示

label1.caption=a&“a"&b&“b"

list1.additemusera&userb&userc&userd&“"&label1.caption '初始化输入按钮

command2.enabled=false

fori=0to9

command1(i).enabled=true

nexti

num=1

'判断输赢

ifa=4then

msgbox“你猜对了!"&“你一共猜了"&degree&“次"

fori=0to9

command1(i).enabled=false

nexti

command2.enabled=false

endif

endsub

privatesubcommand3_click()

'单击取消按钮时

num=1

fori=0to9

command1(i).enabled=true

nexti

command2.enabled=false

endsub

privatesubview_click()

'显示答案时

msgbox“答案是:"&pca&pcb&pcc&pcd&“你答对了吗?" endsub

privatesubend_click()

'游戏结束时

end

endsub

subuserenter(i)'取得用户输入

ifnum=1then

usera=i

num=num+1

elseifnum=2then

userb=i

num=num+1

elseifnum=3then

userc=i

num=num+1

elseifnum=4then

userd=i

num=num+1

else:msgbox“四位数够了!"

endif

endsub

运行程序,选择新游戏,就可以开始玩你自己开发的游戏了。上面程序在win95,vb6下运行通过。->

相关主题
相关文档
最新文档