我已经设置了2D游戏并使用了瓷砖贴图来应用迷宫主题,瓷砖大小为48,我使用以下代码绘制地图。
If LevelStart = True Then
'Creates maze
a = 0 'a is the horizontal coordinate
b = 0 'b is the vertical coordinate
For X = 0 To Me.Width Step TileSize
For Y = 0 To Me.Height Step TileSize
If (a = 0 And b <= 18) Or (a = 22 And b <= 18) Or (b = 0 And a <= 22) Or (b = 18 And a <= 22) Then 'The wall border around the edge of the level
Map.DrawImage(bmpWall, X, Y, TileSize, TileSize)
ElseIf (a = 1 And ((b >= 1 And b <= 3) Or (b >= 5 And b <= 13) Or (b >= 15 And b <= 17))) Or (a = 2 And (b = 1 Or (b >= 3 And b <= 5) Or b = 11 Or (b >= 13 And b <= 15) Or b = 17)) Or (a = 3 And (b = 1 Or (b >= 7 And b <= 9) Or b = 11 Or b = 17)) Or (a = 4 And ((b >= 1 And b <= 5) Or b = 7 Or b = 9 Or (b >= 11 And b <= 14) Or b = 16 Or b = 17)) Or (a = 5 And (b = 5 Or b = 7 Or b = 9 Or b = 14 Or b = 16)) Or (a = 6 And ((b >= 1 And b <= 3) Or b = 5 Or b = 7 Or (b >= 9 And b <= 14) Or b = 16 Or b = 17)) Or (a = 7 And (b = 1 Or b = 3 Or b = 5 Or b = 7 Or b = 17)) Or (a = 8 And (b = 1 Or (b >= 5 And b <= 7) Or (b >= 9 And b <= 13) Or b = 15 Or b = 17)) Or (a = 9 And ((b >= 1 And b <= 3) Or b = 5 Or b = 9 Or b = 13 Or (b >= 15 And b <= 17)) Or (a = 10 And (b = 1 Or (b >= 3 And b <= 9) Or b = 11)) Or (a = 11 And (b = 1 Or b = 8 Or b = 11 Or (b >= 13 And b <= 17))) Or (a = 12 And (b = 1 Or (b >= 3 And b <= 11) Or b = 13 Or b = 15 Or b = 17)) Or (a = 13 And (b = 1 Or b = 3 Or b = 13 Or b = 15 Or b = 17))) Or (a = 14 And ((b >= 1 And b <= 13) Or b = 15 Or b = 17)) Or (a = 15 And (b = 1 Or b = 4 Or b = 10 Or (b >= 15 And b <= 17))) Or (a = 16 And ((b >= 1 And b <= 4) Or (b >= 6 And b <= 8) Or b = 10 Or (b >= 12 And b <= 15) Or b = 17)) Or (a = 17 And (b = 2 Or b = 8 Or b = 10 Or b = 12 Or b = 15 Or b = 17)) Or (a = 18 And (b = 2 Or (b >= 4 And b <= 6) Or (b >= 8 And b <= 10) Or b = 12 Or b = 13 Or b = 15 Or b = 17)) Or (a = 19 And (b = 1 Or b = 2 Or b = 4 Or b = 6 Or b = 8 Or b = 13 Or b = 15 Or b = 17)) Or (a = 20 And (b = 1 Or b = 4 Or b = 6 Or b = 8 Or (b >= 10 And b <= 13) Or b = 17)) Or (a = 21 And ((b >= 1 And b <= 4) Or (b >= 6 And b <= 10) Or (b >= 13 And b <= 17))) Then 'The path
Map.DrawImage(bmpPath, X, Y, TileSize, TileSize)
ElseIf a <= 22 And b <= 18 Then 'The walls
Map.DrawImage(bmpWall, X, Y, TileSize, TileSize)
End If
b += 1
Next Y
a += 1
b = 0
Next X
End If
现在,我想知道如何检测{y}和x轴上PictureBox
的碰撞。根据这段代码,我怎么能写一个碰撞组件?
答案 0 :(得分:0)
您可以使用2D数组存储不同的图块。
Dim stage = { { 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 1 },
{ 0, 0, 1, 0, 0 },
{ 1, 1, 1, 1, 1 } }
Dim playerX = 0, playerY = 3, TileSize = 48
For x = 0 to stage.GetLength(0) - 1
For y = 0 to stage.GetLength(1) - 1
Dim index = stage(x, y) ' gets 0 or 1 from the stage 2D array
Dim image = {bmpPath, bmpWall}(i) ' image = bmpPath if i is 0, or bmpPath if i is 1
Map.DrawImage(image, x * TileSize, y * TileSize, TileSize, TileSize)
Next y
Next x
Map.DrawImage(bmpPlayer, playerX * TileSize, playerY * TileSize, TileSize, TileSize)
Me.KeyPreview = True ' needed for the KeyPress event
然后在KeyPress事件中,类似
Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = Keys.Right AndAlso
playerX < stage.GetLength(0) - 1 AndAlso ' if not at the right most tile
stage(playerX + 1, playerY) = 0 Then ' if the tile on the right is path
Map.DrawImage(bmpPath, playerX * TileSize, playerY * TileSize, TileSize, TileSize)
playerX = playerX + 1
Map.DrawImage(bmpPlayer, playerX * TileSize, playerY * TileSize, TileSize, TileSize)
Else If e.KeyChar = Keys.Left AndAlso playerX > 0 Then
' add the rest of the code for the other directions
End If
End Sub