更改其他角色的位置时,无意中删除了控制台中的角色

时间:2018-09-06 16:36:44

标签: vb.net console-application keypress

我正在尝试从控制台开始游戏,其中包含20 x 40的字符数组。所有字符均为“-”,代表游戏板的默认背景,但“ @”字符代表游戏玩家。玩家位置保持在整数数组中。

用户可以按箭头键移动@字符。我设置了一个循环来读取按键,并在检查了玩家角色是否朝该方向移动后不会移出数组的边界之后,首先将光标位置设置为玩家所在的位置,然后用一个“-”替换“ @”,更新玩家位置,然后将光标位置设置为新位置并写入“ @”。

由于某种原因,当我向下,向左或向上(而不是向右)移动角色时,它会删除玩家先前位置右侧的“-”字符,如下所示:

enter image description here

enter image description here

调试后,错误立即在cki = Console.ReadKey之后发生,因此有关keypress操作的操作是删除字符,但是我无法弄清楚为什么这样做或如何阻止它。有人可以提出解决方案吗?

这是循环的代码:

 Dim cki As ConsoleKeyInfo
 Console.CursorVisible = False

    While True
        'Reads keypress - **deleted character occurs after this line**
        cki = Console.ReadKey
        Select Case cki.Key
            Case ConsoleKey.UpArrow
                If playerPos(0) - 1 >= 0 Then
                    'Deletes playerChar, replaces it with boardChar
                    Console.SetCursorPosition(playerPos(1), playerPos(0))
                    Console.Write(boardChar)
                    board(playerPos(0), playerPos(1)) = boardChar

                    'Changes player position along the vertical axis (-1)
                    playerPos(0) -= 1

                    'Rewrites the playerChar in the new position
                    Console.SetCursorPosition(playerPos(1), playerPos(0))
                    Console.Write(playerChar)
                    board(playerPos(0), playerPos(1)) = playerChar
                End If

            Case ConsoleKey.DownArrow
                If playerPos(0) + 1 < board.GetLength(0) Then
                    Console.SetCursorPosition(playerPos(1), playerPos(0))
                    Console.Write(boardChar)
                    board(playerPos(0), playerPos(1)) = boardChar

                    'Changes player position along the vertical axis (+1)
                    playerPos(0) += 1

                    Console.SetCursorPosition(playerPos(1), playerPos(0))
                    Console.Write(playerChar)
                    board(playerPos(0), playerPos(1)) = playerChar
                End If

            Case ConsoleKey.LeftArrow
                If playerPos(1) - 1 >= 0 Then
                    Console.SetCursorPosition(playerPos(1), playerPos(0))
                    Console.Write(boardChar)
                    board(playerPos(0), playerPos(1)) = boardChar

                    'Changes player position along the horizontal axis (-1)
                    playerPos(1) -= 1

                    Console.SetCursorPosition(playerPos(1), playerPos(0))
                    Console.Write(playerChar)
                    board(playerPos(0), playerPos(1)) = playerChar
                End If

            Case ConsoleKey.RightArrow
                If playerPos(1) + 1 < board.GetLength(1) Then
                    Console.SetCursorPosition(playerPos(1), playerPos(0))
                    Console.Write(boardChar)
                    board(playerPos(0), playerPos(1)) = boardChar

                    'Changes player position along the vertical axis (+1)
                    playerPos(1) += 1

                    Console.SetCursorPosition(playerPos(1), playerPos(0))
                    Console.Write(playerChar)
                    board(playerPos(0), playerPos(1)) = playerChar
                End If
        End Select
    End While

0 个答案:

没有答案