具有碰撞的二维阵列中的平铺映射[Visual Basic]

时间:2016-06-21 01:35:55

标签: vb.net

我有一个程序,目前以迷宫格式映射切片。使用VB图形。我想要一个图片框来检测与某个图块的碰撞(即地图的48x48部分)图块已经被映射成2d数组

Dim stage = {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
             {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1},
             {1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
             {1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1},
             {1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
             {1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1},
             {1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1},
             {1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
             {1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
             {1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1},
             {1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1},
             {1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1},
             {1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1},
             {1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1},
             {1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1},
             {1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
             {1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1},
             {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
              {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}}

现在,我有一个一次移动48px的图片框,根据用户输入平铺到平铺,我希望它能够检测到它何时达到“1”平铺(墙)...所以用户Slai建议这个代码

Dim playerX = 0, playerY = 3, TileSize = 48


For x = 0 to stage.GetLength(0) - 1
For y = 0 to stage.GetLength(1) - 1
    Dim image = {bmpPath, bmpWall}[stage[x, y]]   ' 0 is path, 1 is wall
    Map.DrawImage(image, x * TileSize, y * TileSize, TileSize, TileSize)
Next y
Next x

Map.DrawImage(bmpPlayer, playerX * TileSize, playerY * TileSize, TileSize,      TileSize)

现在,这个代码在理论上会运行得很好,但是我的编译器在这行中存在问题

Dim image = {bmpPath, bmpWall}[stage[x, y]] 

我收到错误 - 错误1括号内的标识符缺少关闭']'。 如果有人能够理解代码试图执行的内容,以及为什么它不会,请尝试帮助我。我试过在该行中添加,删除,替换括号等,但它似乎是完全错误的语法?

这是未来的移动代码,如果我可以使它工作

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

0 个答案:

没有答案