Dim Games As Integer ’關數最大為10
Dim Color As Integer ’球的顏色數
Dim Bs As Integer ’發球速度
Dim GameRun As Boolean ’遊戲狀態
Dim Score As Integer ’成績
2、程序初始化
Private Sub Form_Load()
’讀取遊戲關數,用VB的GetSetting函數從註冊表中讀取
Games = GetSetting(App.EXEName, App.Title, "Level", "1")
Num = Pb.Width \ Balls1(0).Width + 1
For i = 1 To Num ’加載控件
Load Balls1(i): Load Balls2(i)
Next
End Sub
3、遊戲初始化,完成軌道小球的初始位置
Sub InitGame()
Randomize
Color = 3 + Games \ 3
For i = 0 To Num ’生成軌道串球
Balls1(i).Picture = Image1(Int(Rnd * Color)).Picture
If i > 0 Then Balls1(i).Left = Balls1(i - 1).Left + Balls1(0).Width
Balls1(i).Visible = False
’下軌道小球位置控制代碼略
Next
Ball.Picture = Image1(Int(Rnd * Color)).Picture
Ball.Visible = True ’發球圖案
End Sub
4、遊戲開始
Private Sub Command1_Click()
Call InitGame
Timer1.Enabled = True
End Sub
5、軌道小球運動
Private Sub Timer1_Timer()
’註:下軌道小球運動代碼略
If Balls1(0).Left > 0 And Balls2(0).Left > 0 Then
’移動速度Ms1控制,代碼略
Balls1(0).Left = Balls1(0).Left - Ms1
For i = 1 To Num ’其它球隨移
Balls1(i).Left = Balls1(i - 1).Left + Balls1(0).Width
Next
Else ’移到最左邊
’停止遊戲,並作未過關處理
End If
End Sub
6、確定發球的水平位置
Private Sub Pb_MouseMove(Button, Shift, X, Y)
’發出的小球在運動時不處理
If Timer2.Enabled = True Or Timer3.Enabled = True Then Exit Sub
’根據鼠標位置確定發出的水平位置
Ball.Left = X - Ball.Width \ 2
’超出遊戲區域左邊界的處理
If Ball.Left < 0 Then Ball.Left = 0
’右邊界的處理,代碼略
End Sub
7、發球控制
Private Sub Pb_MouseDown(Button, Shift, X, Y)
’根據鼠標位置確定發球運動方向
If Y <= Pb.Height \ 2 Then
Md = -1 ’向上
Else
Md = 1 ’向下
End If
’根據鼠標離發球位置的距離確定發出小球的運動速度
Bs = Abs(Y - Pb.Height \ 2) \ 15
If GameRun = True Then
’向上運動
If Md = -1 And Timer2.Enabled = False Then Timer2.Enabled = True
’向下運動,代碼略
End If
End Sub
8、發球向上運動
Private Sub Timer2_Timer()
If Ball.Top > Line1.Y1 Then ’上升
If Ball.Top - Bs <= Line1.Y1 Then ’到頂
’暫停遊戲,代碼略
Ball.Top = Line1.Y1
K = -1 ’確定插入位置
For i = 0 To Num
’發球水平對稱線
p = Ball.Left + Ball.Width \ 2
If … Then ’判斷在哪個球,條件略
If … Then ’在左側,條件略
K = i
Else
K = i + 1
End If
Exit For
End If
Next
If K >= 0 Then ’能碰撞
’產生音效
If Dir("pop.wav") <> "" Then sndPlaySound "pop.wav", &H0 Or &H1
’插入位置的小球依次後移
For s = Num To K + 1 Step -1
Balls1(s).Picture = Balls1(s - 1).Picture
Next
Balls1(K).Picture = Ball.Picture ’插入
’找消失的左右位置V1、V2,代碼略
If V2 - V1 + 1 >= 3 Then ’有三個
’計分,代碼略
’過關及結束處理,代碼略
’消失的小球閃爍,代碼略
’消失相連小球,代碼略
’所有小球後退(V2-V1+1)個小球位置,代碼略
’向前填充直到V1處圖像非空
While Balls1(V1).Picture = LoadPicture("")
’填充,代碼略
Wend
End If
End If
’繼續遊戲,代碼略
Else ’向上運動
Ball.Top = Ball.Top - Bs
End If
End If
End Sub
9、發球向下運動
Private Sub Timer3_Timer()
’代碼略
End Sub