我有一个在屏幕上绘制文本地图的二维数组,我正在使用“WASD”在屏幕上移动一个字符。我想测试地图限制,这样角色就不会越界。
有什么线索吗?
这是我的代码:
static void Main(string[] args)
{
bool GameOver = false;
int playerX = 1;
int playerY = 1;
char[,] myCharArray = new char[16, 8];
string[] lol = new string[8];
lol[0] = "----------------";
lol[1] = "- -";
lol[2] = "- -";
lol[3] = "- -";
lol[4] = "- -";
lol[5] = "- -";
lol[6] = "- -";
lol[7] = "----------------";
for (int i = 0; i < 8; i++)
{
char[] letters = lol[i].ToCharArray();
for (int j = 0; j < 16; j++)
{
myCharArray[j, i] = letters[j];
}
}
do
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 16; j++)
{
if (playerX == j && playerY == i)
{
Console.Write('*');
}
else
{
Console.Write(myCharArray[j, i]);
}
}
Console.WriteLine();
}
ConsoleKeyInfo pressedKey = Console.ReadKey();
Console.WriteLine(pressedKey.Key.ToString());
if (pressedKey.Key.ToString() == "A")
{
playerX--;
}
if (pressedKey.Key.ToString() == "S")
{
playerY++;
}
if (pressedKey.Key.ToString() == "D")
{
playerX++;
}
if (pressedKey.Key.ToString() == "W")
{
playerY--;
}
Console.Clear();
} while (GameOver == false);
}