劉金玉的零基礎VB教程070期:貪吃蛇遊戲開發第六節 記分與故障排除
- 2020 年 4 月 7 日
- 筆記
https://v.qq.com/x/page/q0939wabcdl.html
文字講解:
劉金玉的零基礎VB教程070期:
貪吃蛇遊戲開發第六節
記分與故障排除
故障點分析
蛇重新開始遊戲後,會自動變短
原因:
鍵盤按下時記錄的鍵盤的按鍵,變成了方向
解決故障:
Abs(currentDirect – KeyCode) < 4
因為四個按鍵的差值不會大於4
Move函數
Move 參數1[,參數2][,參數3][,參數4]
參數1:left
參數2:top
參數3:width
參數4:height
遊戲記分
重新定義label控制項
直接賦值顯示出來即可
記分的變數是全局變數,數值類型
課堂總結
1、故障排除
2、move函數的應用
3、記分控制項
源程式碼:
'定義顏色類型 Private Type Color R As Integer G As Integer B As Integer End Type '定義食物類型 Private Type Food X As Single Y As Single C As Color End Type Private Type Node '每一節蛇身 D As Integer '37左38上39右40下 X As Single 'left Y As Single 'top C As Color '表示蛇身顏色 End Type Dim W As Integer '每一節蛇身寬度 Dim sno() As Node '聲明一條蛇,是動態數組 Dim currentDirect As Integer '代表蛇運動的當前方向 Dim n As Long '代表蛇身結點數 Private WithEvents timer1 As Timer Private WithEvents lblscore As Label '自定義一個標籤控制項記錄分數 '聲明分數變數 Dim score As Long '聲明食物 Dim goods As Food '初始化一條蛇的各個參數 Function init() AutoRedraw = True '自動重繪 W = 200 currentDirect = 39 '默認向右運動 n = 5 ReDim sno(n) As Node '初始化蛇身顏色 Randomize Dim R%, G%, B% R = Int(Rnd * 256) G = Int(Rnd * 256) B = Int(Rnd * 256) '初始化各個坐標點 Dim i As Long For i = 0 To UBound(sno) Step 1 sno(i).D = currentDirect sno(i).X = ScaleWidth / 2 + i * W sno(i).Y = ScaleHeight / 2 '初始化蛇身顏色 sno(i).C.R = R sno(i).C.G = G sno(i).C.B = B Next i '初始化食物數據 Call rndFood End Function '隨機生成食物數據 Function rndFood() Randomize goods.X = Int(Rnd * (ScaleWidth - W)) goods.Y = Int(Rnd * (ScaleHeight - W)) goods.C.R = Int(Rnd * 256) goods.C.G = Int(Rnd * 256) goods.C.B = Int(Rnd * 256) End Function '畫食物 Function drawFood() Line (goods.X, goods.Y)-(goods.X + W, goods.Y + W), RGB(goods.C.R, goods.C.G, goods.C.B), BF End Function '畫一條蛇 Function drawSnake() Cls Dim i As Long For i = 0 To UBound(sno) Step 1 Line (sno(i).X, sno(i).Y)-(sno(i).X + W, sno(i).Y + W), RGB(sno(i).C.R, sno(i).C.G, sno(i).C.B), BF Next i End Function Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer) If Abs(currentDirect - KeyCode) <> 2 And Abs(currentDirect - KeyCode) < 4 Then currentDirect = KeyCode End Sub Private Sub Form_Load() Call init Call drawSnake '對時鐘控制項進行初始化 Set timer1 = Controls.Add("vb.timer", "timer1") timer1.Interval = 100 timer1.Enabled = True '對分數標籤初始化 Set lblscore = Controls.Add("vb.label", "lblscore") lblscore.AutoSize = True lblscore.BackStyle = vbTransparent lblscore.Caption = "得分:" & score lblscore.Move 100, 100 lblscore.Visible = True End Sub '運動思路:插入頭結點,刪除尾節點 Function sport() Dim i As Long '將每一個節點數據向前移動一位 For i = 1 To UBound(sno) Step 1 sno(i - 1) = sno(i) Next i '將頭結點,也就是數組的最後一位重新複製 If currentDirect = 37 Then sno(UBound(sno)).X = sno(UBound(sno)).X - W ElseIf currentDirect = 38 Then sno(UBound(sno)).Y = sno(UBound(sno)).Y - W ElseIf currentDirect = 39 Then sno(UBound(sno)).X = sno(UBound(sno)).X + W ElseIf currentDirect = 40 Then sno(UBound(sno)).Y = sno(UBound(sno)).Y + W End If End Function '蛇的運動 Private Sub timer1_Timer() Call sport Call drawSnake '判斷是否撞到窗體邊緣 If isCrashWall Then If MsgBox("GAME OVER !是否重新開始?", vbYesNo, "遊戲結束") = vbYes Then Call init Else End End If End If '畫食物 Call drawFood '判斷是否吃到食物 If isEatFood Then '增長蛇身 n = n + 1 score = score + 1 lblscore.Caption = "得分:" & score ReDim Preserve sno(n) sno(n).D = currentDirect sno(n).C.R = goods.C.R sno(n).C.G = goods.C.G sno(n).C.B = goods.C.B If currentDirect = 37 Then sno(n).X = sno(n - 1).X - W sno(n).Y = sno(n - 1).Y ElseIf currentDirect = 39 Then sno(n).X = sno(n - 1).X + W sno(n).Y = sno(n - 1).Y ElseIf currentDirect = 38 Then sno(n).X = sno(n - 1).X sno(n).Y = sno(n - 1).Y - W ElseIf currentDirect = 40 Then sno(n).X = sno(n - 1).X sno(n).Y = sno(n - 1).Y + W End If '隨機生成食物 Call rndFood End If End Sub '是否撞到窗體邊緣,撞到返回true,否則就是false Function isCrashWall() As Boolean isCrashWall = False If sno(UBound(sno)).X + W > ScaleWidth _ Or sno(UBound(sno)).X < 0 _ Or sno(UBound(sno)).Y < 0 _ Or sno(UBound(sno)).Y + W > ScaleHeight Then isCrashWall = True '撞到了 End If End Function '是否吃到食物,true吃到,false沒吃到 Function isEatFood() '默認沒有吃到 isEatFood = False '判斷是否吃到,就是判斷蛇頭與食物是否碰撞 If sno(UBound(sno)).X + W > goods.X _ And sno(UBound(sno)).X < goods.X + W _ And sno(UBound(sno)).Y + W > goods.Y _ And sno(UBound(sno)).Y < goods.Y + W Then isEatFood = True End If End Function
介面:
