本帖最後由 ggjtwo 於 2011-12-25 09:00 PM 編輯
- Public Class Form1
- Dim i As Integer 'X座標
- Dim j As Integer 'Y座標
- Dim point As Integer = 0 '吃過的小點數
- Dim direct As Integer = 0 '目前行進方向
- Dim tempx As Integer '目前x座標(代替picture.image做運算用的)
- Dim tempy As Integer '目前y座標(代替picture.image做運算用的)
- '地圖陣列,0是空白 1是有小點 2是橫磚 3是直磚 4~7角磚
- Dim arr(,) As Integer = {{4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6}, _
- {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3}, _
- {3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3}, _
- {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3}, _
- {3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3}, _
- {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3}, _
- {3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3}, _
- {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3}, _
- {3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3}, _
- {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3}, _
- {3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3}, _
- {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3}, _
- {3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3}, _
- {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3}, _
- {3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3}, _
- {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3}, _
- {3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3}, _
- {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3}, _
- {3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3}, _
- {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3}, _
- {3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3}, _
- {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3}, _
- {3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3}, _
- {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3}, _
- {5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 7}}
- Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- PictureBox1.Visible = True '小精靈的圖像(四個方向加張口閉口)
- PictureBox2.Visible = True '地圖的圖像(磚塊之類)
- PictureBox1.Left = 64 '初始座標
- PictureBox1.Top = 64
- Timer1.Enabled = True '小精靈動作控制
- Timer2.Enabled = True '地圖狀態(主要為小點狀態消失與否)
- PictureBox2.Image = My.Resources.Dot '磚塊圖檔(取代my.resources.xxx,因很耗CPU 資源)
- PictureBox3.Image = My.Resources.gridver
- PictureBox4.Image = My.Resources.grid
- PictureBox5.Image = My.Resources.gridcorn1
- PictureBox6.Image = My.Resources.gridcorn2
- PictureBox7.Image = My.Resources.gridcorn3
- PictureBox8.Image = My.Resources.gridcorn4
- End Sub
- Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
- tempy = PictureBox1.Top '座標運算用
- tempx = PictureBox1.Left
- If tempx Mod 32 = 0 Then '到了整數座標時才能變換方向(X->Y)
- If direct = 42 Or direct = 12 Then
- direct = 2
- ElseIf direct = 43 Or direct = 13 Then
- direct = 3
- End If
- End If
- If tempy Mod 32 = 0 Then '到了整數座標時才能變換方向(Y->X)
- If direct = 30 Or direct = 20 Then
- direct = 0
- ElseIf direct = 31 Or direct = 21 Then
- direct = 1
- End If
- End If
- ' direct=0,1,2,3 => 右左上下
- If direct = 0 Or direct = 42 Or direct = 43 Then 'direct=42,43分別代表由向右轉到上下
- If Int(((PictureBox1.Left) / 10)) Mod 2 = 1 Then
- PictureBox1.Image = My.Resources._1
- Else
- PictureBox1.Image = My.Resources._2
- End If
- If arr(Int(tempx / 32) + 1, Int(tempy / 32)) = 0 Or arr(Int(tempx / 32) + 1, Int(tempy / 32)) = 1 Then
- PictureBox1.Left = PictureBox1.Left + 4 '不碰壁時就才能前進
- End If
- ElseIf direct = 1 Or direct = 12 Or direct = 13 Then 'direct=12,13分別代表由向左轉到上下向
- If Int(((PictureBox1.Left) / 10)) Mod 2 = 1 Then
- PictureBox1.Image = My.Resources._5
- Else
- PictureBox1.Image = My.Resources._6
- End If
- If arr(Int((tempx - 4) / 32), Int(tempy / 32)) = 0 Or arr(Int((tempx - 4) / 32), Int(tempy / 32)) = 1 Then
- PictureBox1.Left = PictureBox1.Left - 4
- End If
- ElseIf direct = 2 Or direct = 21 Or direct = 20 Then 'direct=21,20分別代表由向上轉到左右向
- If Int(((PictureBox1.Top) / 10)) Mod 2 = 1 Then
- PictureBox1.Image = My.Resources._3
- Else
- PictureBox1.Image = My.Resources._4
- End If
- If arr(Int(tempx / 32), Int(tempy / 32) + 1) = 0 Or arr(Int(tempx / 32), Int(tempy / 32) + 1) = 1 Then
- PictureBox1.Top = PictureBox1.Top + 4
- End If
- ElseIf direct = 3 Or direct = 30 Or direct = 31 Then 'direct=31,30分別代表由向下轉到左右向
- If Int(((PictureBox1.Top) / 10)) Mod 2 = 1 Then
- PictureBox1.Image = My.Resources._7
- Else
- PictureBox1.Image = My.Resources._8
- End If
- If arr(Int(tempx / 32), Int((tempy - 4) / 32)) = 0 Or arr(Int(tempx / 32), Int((tempy - 4) / 32)) = 1 Then
- PictureBox1.Top = PictureBox1.Top - 4
- End If
- End If
- If tempx Mod 32 = 0 And tempy Mod 32 = 0 Then '當吃到小點時,此位置必為整數,吃完後已空白圖片覆蓋小點點
- If arr(tempx / 32, tempy / 32) = 1 Then
- arr(tempx / 32, tempy / 32) = 0
- Using g As Graphics = Me.CreateGraphics()
- g.DrawImage(My.Resources.blank1, tempx, tempy)
- End Using
- point = point + 1 '吃完一點就積分
- End If
- End If
- Label1.Text = point
- End Sub
- Public Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
- If e.KeyCode = 39 Then '39,37,38,40分別到表鍵盤右左上下鍵
- TextBox1.Focus()
- If direct = 1 Then
- direct = 0
- ElseIf direct = 2 Then
- direct = 20
- ElseIf direct = 3 Then
- direct = 30
- End If
- ElseIf e.KeyCode = 37 Then
- TextBox1.Focus()
- If direct = 0 Then
- direct = 1
- ElseIf direct = 2 Then
- direct = 21
- ElseIf direct = 3 Then
- direct = 31
- End If
- ElseIf e.KeyCode = 40 Then
- TextBox1.Focus()
- If direct = 0 Then
- direct = 42
- ElseIf direct = 1 Then
- direct = 12
- ElseIf direct = 3 Then
- direct = 2
- End If
- ElseIf e.KeyCode = 38 Then
- TextBox1.Focus()
- If direct = 0 Then
- direct = 43
- ElseIf direct = 1 Then
- direct = 13
- ElseIf direct = 2 Then
- direct = 3
- End If
- End If
- End Sub
- Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
- i = 0 '此處之目的為地圖畫面需一再更新,否則畫面會因切換視窗或其他因素消失
- j = 0
- Using g As Graphics = Me.CreateGraphics()
- While i < 25
- While j < 20
- Select Case arr(i, j)
- Case Is = 1
- g.DrawImage(PictureBox2.Image, i * 32, j * 32)
- Case Is = 2
- g.DrawImage(PictureBox3.Image, i * 32, j * 32)
- Case Is = 3
- g.DrawImage(PictureBox4.Image, i * 32, j * 32)
- Case Is = 4
- g.DrawImage(PictureBox5.Image, i * 32, j * 32)
- Case Is = 5
- g.DrawImage(PictureBox6.Image, i * 32, j * 32)
- Case Is = 6
- g.DrawImage(PictureBox7.Image, i * 32, j * 32)
- Case Is = 7
- g.DrawImage(PictureBox8.Image, i * 32, j * 32)
- End Select
- j = j + 1
- End While
- j = 0
- i = i + 1
- End While
- End Using
- End Sub
- End Class
複製代碼 到目前的程式碼已貼上,可是我不會對齊耶,諸位前輩可能要看得辛苦一些了
1.現在遇到的問題,小精靈太大隻,經過時會蓋到磚頭(可能會用縮小小精靈圖片來解決)
2.地圖是用Me.creategraphics畫的,只畫一次的話會因為切換視窗等問題而畫面消失,須反複的畫上
,我試過用VB6來寫(paintpicture也是遇到同樣題),所以我放在timer2,每隔一段時間就畫
一次地圖,可是這樣導至整個遊戲速度變慢,目前還沒有解決辦法
3.吃小點點的時候都得到整數的位置才能吃,不知有什麼辦法可以只碰到就吃到點的
4.最後,還有鬼還沒寫,鬼追蹤小精靈的方式也毫無頭緒(這部份應該是最難的吧)
VB2008的檔案請由此下載()
程式寫了一天超累的,各位前輩如果有看到什麼可以改的地方或是可加快速度的方式
請多多給予意見,謝謝
... |