如何在图片框网格周围“移动”图片框图像?

时间:2018-06-20 15:24:03

标签: vb.net

我无法弄清楚如何使用WASD /箭头键将图像从一个图片框移动到一个图片框网格内(所有图片框均在二维数组中创建,用于x和y)。有没有一种方法可以为每个图片框分配坐标,在这些坐标中可以操纵它以改变图片框中的图像以产生运动的错觉?

例如:程序从picturebox(3,3)开始以设置播放器图像和一个名为“ CurrentPicBox = picturebox(Xpos,Ypos)”的变量;用户按下W /向上箭头;这将使具有播放器图像集的图片框清除图像,并使“ CurrentPicBox = picturebox(Xpos,Ypos + 1)。然后使picturebox(3,4)具有图像集。

这是行之有效的还是这种逻辑不正确?

1 个答案:

答案 0 :(得分:1)

您的逻辑应该很好!尽管我认为如果将<caixa-filtro v-model="textoBusca" @onSearch="listar"></caixa-filtro> 用作左上角,将(0, 0)用作右下角,将更容易管理。

移动播放器将是:

  • 上:(4, 4)
  • 下:(Xpos, Ypos - 1)
  • 左:(Xpos, Ypos + 1)
  • 右:(Xpos - 1, Ypos)

以下是用于创建和使用游戏网格的示例类:

(Xpos + 1, Ypos)

然后您可以像这样使用它:

Public NotInheritable Class GameBoard
    'The size of each picture box.
    Public Shared ReadOnly GameTileSize As New Size(16, 16)

    'The 2D array holding our grid.
    Private Grid As PictureBox(,)

    Private PosX As Integer = 0
    Private PosY As Integer = 0

    Public Sub MovePlayer(ByVal Direction As MovementDirection)
        Select Case Direction
            Case MovementDirection.Left
                If PosX - 1 < 0 Then Return 'Error checking. Cannot move outside grid.
                Grid(PosX - 1, PosY).Image = Grid(PosX, PosY).Image 'Move image to the left.
                Grid(PosX, PosY).Image = Nothing 'Clear the current picture box.
                PosX -= 1

            Case MovementDirection.Right
                If PosX + 1 >= Grid.GetLength(0) Then Return
                Grid(PosX + 1, PosY).Image = Grid(PosX, PosY).Image
                Grid(PosX, PosY).Image = Nothing
                PosX += 1

            Case MovementDirection.Up
                If PosY - 1 < 0 Then Return
                Grid(PosX, PosY - 1).Image = Grid(PosX, PosY).Image
                Grid(PosX, PosY).Image = Nothing
                PosY -= 1

            Case MovementDirection.Down
                If PosY + 1 >= Grid.GetLength(1) Then Return
                Grid(PosX, PosY + 1).Image = Grid(PosX, PosY).Image
                Grid(PosX, PosY).Image = Nothing
                PosY += 1
        End Select
    End Sub

    Public Sub New(ByVal Container As Container, ByVal PlayerSprite As Image, ByVal TilesX As Integer, ByVal TilesY As Integer) As PictureBox(,)
        'Initialize our array.
        Grid = New PictureBox(TilesX - 1, TilesY - 1) {}

        'Iterate every "coordinate" and add a game tile to it.
        For x = 0 To TilesX - 1
            For y = 0 To TilesY - 1
                'Create a tile of the appropriate size and place it at a location that is a multiple of its size.
                Dim Tile As New PictureBox() With {
                    .Size = GameBoard.GameTileSize,
                    .Location = New Point(x * GameBoard.GameTileSize.X, y * GameBoard.GameTileSize.Y),
                    .BorderStyle = BorderStyle.FixedSingle 'Add a border to the tile.
                }

                'Add the tile to our array.
                Grid(x, y) = Tile

                'Add the tile to the specified container.
                Container.Controls.Add(Tile)
            Next
        Next

        'Place the player at the initial coordinates.
        Grid(PosX, PosY).Image = PlayerSprite
    End Sub

    Public Enum MovementDirection As Integer
        Left = 0
        Right
        Up
        Down
    End Enum
End Class

用实际的玩家精灵替换'The variable holding our game board. Dim Board As GameBoard Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'Create the grid inside the form (Me), 4 tiles tall and wide. Board = New GameBoard(Me, My.Resources.Player, 4, 4) End Sub Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown 'Handle movement. Select Case e.KeyCode Case Keys.Left, Keys.A : Board.MovePlayer(MovementDirection.Left) Case Keys.Right, Keys.D : Board.MovePlayer(MovementDirection.Right) Case Keys.Up, Keys.W : Board.MovePlayer(MovementDirection.Up) Case Keys.Down, Keys.S : Board.MovePlayer(MovementDirection.Down) End Select End Sub