用VB6开发数字小游戏.doc
用VB6.0开发猜数字小游戏2001-12-22· · ··Yesky随着Windows95 and 98的流行,越来越多的人加入Windows程序设计的队伍之中。以前,Windows程序设计是那些训练有素的专业程序设计者才会涉足的神秘领域,几乎所有的Windows程序都是用C/C语言编写的,这对大部分普通程序设计者来说,要想高效迅速地编写出具有一定功能的Windows程序就不是一件容易的事情了。1991年,Microsoft公司首次推出了Visual Basic for Windows,从此,人们不用C/C或汇编就可以编写Windows程序了。到目前为止,我认为在所有的Windows程序设计工具中,Visual Basic 是最方便的,它以一种全新的思想让程序员快捷和高效地设计出Windows程序。目前,Visual Basic 的最高版本为6.0,本文讲述用运行于Win95/98 上的VB6.0来开发一个猜数字的小游戏。首先说一说猜数字这个游戏的玩法,一开始计算机会随机产生一个不重复的四位数,你要输入四位不重复的数与计算机给出的数作对比,如果与计算机给出的数的位置相同数字相同,那么将会是1A,如果数字相同而位置相不同,将会显示1B。例如:计算机的随机数字为:1234 ,我猜的数字为:1356 ,那么这时计算机会给你提示为:1A1B,也就是说,你猜的数字中,有一位数字是猜对的,而且数字位置都对,所以显示为1A;还有一个数字也猜对了,但是位置不对,所以显示为1B。就这些了,看谁猜的次数少。首先在Form中加入一个CommandButtion控件,在Command1上点击鼠标右键,选择复制,在窗体上点击鼠标右键,选择粘贴在窗体上粘贴出九个Command1,此时出现对话框问你要不要创建控件数组,在此选择是。然后再加入两个CommandButtion控件,一个ListBox、一个Frame、一个Label 。设置窗体的Caption属性为“猜一猜”、BorderStyle为1Fixed Single、控件数组的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。好了,其余属性使用缺省的即可,最后的界面应跟下图一样:以下是程序清单:Dim PcA, PcB, PcC, PcD As Integer '电脑给出的每一位数Dim UserA, UserB, UserC, UserD As Integer '用户输入的每一位数Dim Degree As Integer '用户猜了几次Dim Num As Integer '判断用户输入次数的变量Private Sub Form_Load()'程序运行行时'初始化For i = 0 To 9Command1(i).Enabled = FalseNext iCommand2.Enabled = FalseCommand3.Enabled = FalseView.Enabled = FalseEnd SubPrivate Sub New_Click()'开始一个新游戏时View.Enabled = True '可以看答案List1.Clear '清空列表框Degree = 0' 对随机数生成器做初始化RandomizeNum = 1Label1.Caption = 0 “A" 0 “B"'电脑给出的每一位数PcA = Int(9 Rnd)DoPcB = Int(9 Rnd)Loop While PcB = PcADoPcC = Int(9 Rnd)Loop While PcC = PcA Or PcC = PcBDoPcD = Int(9 Rnd)Loop While PcD = PcA Or PcD = PcB Or PcD = PcCFor i = 0 To 9Command1(i).Enabled = True Next iCommand2.Enabled = FalseCommand3.Enabled = TrueEnd SubPrivate Sub Command1_Click(Index As Integer)'用户输入时'使得输入过的按钮无效If Num <= 4 ThenCommand1(Index).Enabled = FalseEnd If'判断用户输入了几位,如果输入了四位则确认按钮有效If Num = 4 ThenCommand2.Enabled = TrueEnd If '取得用户输入Select Case IndexCase 0UserEnter (0) '调用UserEnter过程Case 1 UserEnter (1)Case 2 UserEnter (2)Case 3 UserEnter (3)Case 4 UserEnter (4)Case 5 UserEnter (5)Case 6 UserEnter (6)Case 7 UserEnter (7)Case 8 UserEnter (8)Case 9 UserEnter (9)End SelectEnd SubPrivate Sub Command2_Click()'单击确定按钮时'判断用户输入是否正确Dim A, B As IntegerA = 0B = 0Degree = Degree 1If UserA = PcA ThenA = A 1ElseIf UserA = PcB Or UserA = PcC Or UserA = PcD ThenB = B 1用Visual BASIC 6.0 开发猜数字小游戏 End IfIf UserB = PcB ThenA = A 1ElseIf UserB = PcA Or UserB = PcC Or UserD = PcD ThenB = B 1End IfIf UserC = PcC ThenA = A 1ElseIf UserC = PcA Or UserC = PcB Or UserC = PcD ThenB = B 1End IfIf UserD = PcD ThenA = A 1ElseIf UserD = PcA Or UserD = PcB Or UserC = PcC ThenB = B 1End If'显示提示Label1.Caption = A “A" B “B"List1.AddItem UserA UserB UserC UserD “ " Label1.Caption'初始化输入按钮Command2.Enabled = FalseFor i = 0 To 9Command1(i).Enabled = TrueNext iNum = 1'判断输赢If A = 4 ThenMsgBox “你猜对了!" “你一共猜了" Degree “次"For i = 0 To 9Command1(i).Enabled = FalseNext iCommand2.Enabled = FalseEnd IfEnd SubPrivate Sub Command3_Click()'单击取消按钮时Num = 1For i = 0 To 9Command1(i).Enabled = TrueNext iCommand2.Enabled = FalseEnd SubPrivate Sub View_Click()'显示答案时MsgBox “答案是:" PcA PcB PcC PcD “你答对了吗?"End SubPrivate Sub End_Click()'游戏结束时EndEnd SubSub UserEnter(i) '取得用户输入If Num = 1 ThenUserA = iNum = Num 1ElseIf Num = 2 ThenUserB = iNum = Num 1ElseIf Num = 3 ThenUserC = iNum = Num 1ElseIf Num = 4 ThenUserD = iNum = Num 1Else: MsgBox “四位数够了!"End IfEnd Sub运行程序,选择新游戏,就可以开始玩你自己开发的游戏了。上面程序在Win95,VB6下运行通过。