Не так работает цикл while c#
Пуля должна долететь до стены и не ломать, а она делает наоборот.
namespace game
{
class Program
{
static void Main(string[] args)
{
Console.CursorVisible = false;//Убирает курсор из консолиъ
Console.ForegroundColor = ConsoleColor.Yellow;
char[,] map =
{
{'░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░', },
{'░',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','░', },
{'░',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','░', },
{'░',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','░', },
{'░',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','░', },
{'░',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','░', },
{'░',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','░', },
{'░',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','░', },
{'░',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','░', },
{'░',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','░', },
{'░',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','░', },
{'░',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','░', },
{'░',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','░', },
{'░',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','░', },
{'░',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','░', },
{'░',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','░', },
{'░',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','░', },
{'░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░','░', },
};
//Координаты персонажа
int UserPositionY = map.GetLength(1) / 2;//0 2
int UserPositionX = map.GetLength(1) / 2;//1 2
int BulletPositionY = map.GetLength(1) / 2;//0 2
int BulletPositionX = map.GetLength(1) / 2;//1 2
//int UserPositionY1 = map.GetLength(0) / 5;
//int UserPositionX1 = map.GetLength(1) / 5;
string mapString = "";
for (int y = 0; y < map.GetLength(0); y++)
{
for (int x = 0; x < map.GetLength(1); x++)
{
Console.Write(map[y, x]);
}
Console.WriteLine();
}
//Начало игры
object locker = new();
char player = '▲';
bool isShoot = false;
while (true)
{//Начало While
//Отрисовка
// lock (locker)
lock (locker)
{
//Ходьба персонажа
Console.SetCursorPosition(UserPositionX, UserPositionY);
//Console.SetCursorPosition(BulletPositionX, BulletPositionY);
Console.Write(player);
}
//Console.SetCursorPosition(UserPositionX1, UserPositionY1);
//Console.Write('1');
//Считываем клавиши
var key = Console.ReadKey(true).Key; //Для считывания клавиши
lock (locker)
{
Console.SetCursorPosition(UserPositionX, UserPositionY);
//Console.SetCursorPosition(BulletPositionX, BulletPositionY);
Console.Write(' ');
}
switch (key)
{
case ConsoleKey.W:
if (map[UserPositionY - 1, UserPositionX] != '░')
{
player = '▲';
UserPositionY--;
}
break;
case ConsoleKey.S:
if (map[UserPositionY + 1, UserPositionX] != '░')
{
player = '▼';
UserPositionY++;
}
break;
case ConsoleKey.A:
if (map[UserPositionY, UserPositionX - 1] != '░')
{
player = '◄';
UserPositionX--;
}
break;
case ConsoleKey.D:
if (map[UserPositionY, UserPositionX + 1] != '░')
{
player = '►';
UserPositionX++;
}
break;
case ConsoleKey.Spacebar:
if (map[UserPositionY - 1, UserPositionX] != '░')
{
if (isShoot) continue;
isShoot = true;
Task.Run(async () =>
{
char bullet = '°';
int currentPosPlayerX = UserPositionX;
int currentPosPlayerY = UserPositionY;
char player1 = player;
if (player1 == '▲')
{
int i = 1;
while (map[currentPosPlayerX, currentPosPlayerY + i + 1] != '░')
{
lock (locker)
{
Console.SetCursorPosition(currentPosPlayerX, currentPosPlayerY - i);
Console.Write(bullet);
}
await Task.Delay(100);
lock (locker)
{
Console.SetCursorPosition(currentPosPlayerX, currentPosPlayerY - i);
Console.Write(" ");
}
i++;
}
}
if (player1 == '▼')
{
int i = 1;
while (map[currentPosPlayerX, currentPosPlayerY + i + 1] != '░')
{
lock (locker)
{
Console.SetCursorPosition(currentPosPlayerX, currentPosPlayerY + i);
Console.Write(bullet);
}
await Task.Delay(100);
lock (locker)
{
Console.SetCursorPosition(currentPosPlayerX, currentPosPlayerY + i);
Console.Write(" ");
}
i++;
}
}
if (player1 == '◄')
{
int i = 1;
while (map[currentPosPlayerX, currentPosPlayerY + i + 1] != '░')
{
lock (locker)
{
Console.SetCursorPosition(currentPosPlayerX - i, currentPosPlayerY);
Console.Write(bullet);
}
await Task.Delay(100);
lock (locker)
{
Console.SetCursorPosition(currentPosPlayerX - i, currentPosPlayerY);
Console.Write(" ");
}
i++;
}
}
if (player1 == '►')
{
int i = 1;
while (map[currentPosPlayerX, currentPosPlayerY + i + 1] != '░')
{
lock (locker)
{
Console.SetCursorPosition(currentPosPlayerX + i, currentPosPlayerY);
Console.Write(bullet);
}
await Task.Delay(100);
lock (locker)
{
Console.SetCursorPosition(currentPosPlayerX + i, currentPosPlayerY);
Console.Write(" ");
}
i++;
}
}
isShoot = false;
});
}
break;
}//Конец switch
}//Конец While
}
}
}