编程语言 程序设计交流站


Join the forum, it's quick and easy

编程语言 程序设计交流站
编程语言 程序设计交流站
Would you like to react to this message? Create an account in a few clicks or log in to continue.
编程语言 程序设计交流站

欢迎关顾本站,在这里你可以畅所欲言,提出你要解决的问题,各路高手会给你一个圆满的答复,另外有各种源代码、源程序等供你下载。vb vc c++ c# delphi 易语言 Java PASCAL VFP JS VBS Pascal SQL...>


您没有登录。 请登录注册

用VB6.0开发猜数字小游戏

向下  留言 [第1页/共1页]

1用VB6.0开发猜数字小游戏 Empty 用VB6.0开发猜数字小游戏 2012-07-18, 6:53 pm

Admin

Admin
Admin

随着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为1-Fixed 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 9
   Command1(i).Enabled = False
   Next i
   Command2.Enabled = False
   Command3.Enabled = False
   View.Enabled = False
   End Sub
   Private Sub New_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)
   Loop While PcB = PcA
   Do
   PcC = Int(9 * Rnd)
   Loop While PcC = PcA Or PcC = PcB
   Do
   PcD = Int(9 * Rnd)
   Loop While PcD = PcA Or PcD = PcB Or PcD = PcC
   For i = 0 To 9
   Command1(i).Enabled = True
   Next i
   Command2.Enabled = False
   Command3.Enabled = True
   End Sub
   Private Sub Command1_Click(Index As Integer)
   '用户输入时
   '使得输入过的按钮无效
   If Num <= 4 Then
   Command1(Index).Enabled = False
   End If
   '判断用户输入了几位,如果输入了四位则确认按钮有效
   If Num = 4 Then
   Command2.Enabled = True
   End If
   '取得用户输入
   Select Case Index
   Case 0
   UserEnter (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 Select
   End Sub
   Private Sub Command2_Click()
   '单击确定按钮时
   '判断用户输入是否正确
   Dim A, B As Integer
   A = 0
   B = 0
   Degree = Degree + 1
   If UserA = PcA Then
   A = A + 1
   ElseIf UserA = PcB Or UserA = PcC Or UserA = PcD Then
   B = B + 1
   用Visual BASIC 6.0 开发猜数字小游戏 End If
   If UserB = PcB Then
   A = A + 1
   ElseIf UserB = PcA Or UserB = PcC Or UserD = PcD Then
   B = B + 1
   End If
   If UserC = PcC Then
   A = A + 1
   ElseIf UserC = PcA Or UserC = PcB Or UserC = PcD Then
   B = B + 1
   End If
   If UserD = PcD Then
   A = A + 1
   ElseIf UserD = PcA Or UserD = PcB Or UserC = PcC Then
   B = B + 1
   End If
   '显示提示
   Label1.Caption = A & “A" & B & “B"
   List1.AddItem UserA & UserB & UserC & UserD & “ " & Label1.Caption
   '初始化输入按钮
   Command2.Enabled = False
   For i = 0 To 9
   Command1(i).Enabled = True
   Next i
   Num = 1
   '判断输赢
   If A = 4 Then
   MsgBox “你猜对了!" & “你一共猜了" & Degree & “次"
   For i = 0 To 9
   Command1(i).Enabled = False
   Next i
   Command2.Enabled = False
   End If
   End Sub
   Private Sub Command3_Click()
   '单击取消按钮时
   Num = 1
   For i = 0 To 9
   Command1(i).Enabled = True
   Next i
   Command2.Enabled = False
   End Sub
   Private Sub View_Click()
   '显示答案时
   MsgBox “答案是:" & PcA & PcB & PcC & PcD &“你答对了吗?"
   End Sub
   Private Sub End_Click()
   '游戏结束时
   End
   End Sub
   Sub UserEnter(i) '取得用户输入
   If Num = 1 Then
   UserA = i
   Num = Num + 1
   ElseIf Num = 2 Then
   UserB = i
   Num = Num + 1
   ElseIf Num = 3 Then
   UserC = i
   Num = Num + 1
   ElseIf Num = 4 Then
   UserD = i
   Num = Num + 1
   Else: MsgBox “四位数够了!"
   End If
   End Sub
   运行程序,选择新游戏,就可以开始玩你自己开发的游戏了。

http://proj.my-rpg.com

返回页首  留言 [第1页/共1页]

您在这个论坛的权限:
不能在这个论坛回复主题