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 |
運行程序,選擇新游戲,就可以開始玩你自己開發的游戲了。
上面程序在Win95,VB6下運行通過。