I have just such a simple implementation of the game Mined Out:
The location of the player, his model is depicted by the number of mines around this player. At the same time, those places where I have already been should be marked with a full stop. At me they are displayed by a dot, but the problem is that when a player is to the right of a dot, it turns out that when I get a character (WASD), it is written to the console, and the dot is erased! I automatically remove these characters in the player's Move () method like this:
Console.SetCursorPosition(X1, Y1); Console.Write(". ");
here the dot is first put where the player used to be, and the symbol of the button I pressed should be written to the right of this dot (for example, went forward - w).
How can I prevent characters from pressing the keys at all? Tell me, please, this will help me fix a lot of problems!
class Game { static private void StartGame() { Console.Clear(); Bomb[] Bombs = { new Bomb(2, 3), new Bomb(2, 1), new Bomb(6, 5) }; Field field = new Field(8, 8, Bombs, new Player_CT(4, 7, 'd'), new DefuseKit(4, 6), 0, 0); field.MakeField(); while (true) // game cycle { field.player.Move(); field.PrintPlayer(); field.player.TakeDefuseKit(field.Kits); if (!field.player.DefuseBomb(field.Bombs)) break; if (field.HaveWon) { Console.SetCursorPosition(4, field.Height + 20); Console.Write("You\'ve won!!!"); break; } } } static void Main(string[] args) { StartGame(); PressEnter: if (Console.ReadKey().Key == ConsoleKey.Enter) ; else goto PressEnter; } } class Bomb { int _x; int _y; public int X { get { return _x; } set { if (value > 0) _x = value; } } public int Y { get { return _y; } set { if (value > 0) _y = value; } } public Bomb(int X, int Y) { this.X = X; this.Y = Y; } } class Field { int _x; int _y; int X { get { return _x; } set { if (value >= 0) _x = value; } } int Y { get { return _y; } set { if (value >= 0) _y = value; } } public int _height; public int _width; public int Height { get { return _height; } private set { _height = value; } } public int Width { get { return _width; } private set { _width = value; } } public bool HaveWon { get { return player.X == Width / 2 && player.Y == Y; } } internal Bomb[] _bombs; internal Bomb[] Bombs { get { return _bombs; } set { _bombs = value; } } internal DefuseKit Kits; public Player_CT player; public Field(int Height, int Width, Bomb[] Bombs, Player_CT player, DefuseKit DefuseKits, int x, int y) { X = x; Y = y; this.Bombs = Bombs; this.Height = Height; // Y -- start, Y + Height -- end this.Width = Width; // X -- start, X + Width -- end this.player = player; this.Kits = DefuseKits; } public void MakeField() { Console.SetCursorPosition(X, Y); for (int i = 0; i <= Width; i++) { if (i == Width / 2) { Console.Write(' '); continue; } else Console.Write('-'); } Console.SetCursorPosition(X, Y); for (int i = 1; i <= Height; i++) { Console.WriteLine('|'); } for (int i = 0; i <= Width; i++) { Console.Write('-'); } /*for (int i = 0; i < Bombs.Length; i++) { Console.SetCursorPosition(Bombs[i].X, Bombs[i].Y); Console.Write('B'); }*/ Console.SetCursorPosition(Kits.X, Kits.Y); Console.Write('D'); Console.SetCursorPosition(player.X, player.Y); Console.Write('P'); } public int BombsNearTo(int x, int y) { if (Bombs == null) return 0; int Count = 0; for (int i = 0; i < Bombs.Length; i++) { if (Bombs[i] == null) continue; if (Math.Abs(x - Bombs[i].X) <= 1 && Math.Abs(y - Bombs[i].Y) <= 1 && (x != Bombs[i].X || y != Bombs[i].Y)) Count++; } return Count; } public bool IsOver() { for (int i = 0; i < Bombs.Length; i++) { if (Bombs[i] != null) { if (player.X == Bombs[i].X && player.Y == Bombs[i].Y) { Console.SetCursorPosition(0, Height + 50); Console.WriteLine("You\'ve lost!"); return true; } } } return false; } public void PrintPlayer() { Console.SetCursorPosition(player.X, player.Y); Console.Write(BombsNearTo(player.X, player.Y)); } public string this[int x, int y] { get { for (int i = 0; i < Bombs.Length; i++) { if (Bombs[i].X == x && Bombs[i].Y == y) { return "Bomb"; } } if (Kits.X == x && Kits.Y == y) return "Kits"; if (player.X == x && player.Y == y) { return "Player"; } else return "empty point"; } } } class Player_CT : Player { public Player_CT(int x, int y, char d) : base(x, y, d) { } bool DefuseKits; // defuse the bomb with defuse kits - 5 seconds, without - 10 seconds int _hp = 100; int hp { get { return _hp; } set { if (value > -1 && value < 101) _hp = value; } } // hp range = (0, 100) public bool DefuseBomb(Bomb[] bombs) { for (int i = 0; i < bombs.Length; i++) { if (!(bombs[i] == null)) { if (X - bombs[i].X == 0 && Y - bombs[i].Y == 0) // находится там где бомба { if (DefuseKits) { bombs[i] = null; // bomb doesnt exist anymore DefuseKits = false; } else { Console.SetCursorPosition(3, 30); Console.Write("You\'ve lost!"); return false; } } } } return true; } public void TakeDefuseKit(DefuseKit Kits) { if (Kits.X == X && Kits.Y == Y) DefuseKits = true; } } class DefuseKit { public int X; public int Y; public DefuseKit(int x, int y) { X = x; Y = y; } } abstract class Player { public int X; public int Y; public char _direct; public char direct { get { return _direct; } set { _direct = value; } } public Player(int x, int y, char d) { X = x; Y = y; direct = d; } public void Move() { int X1 = X; int Y1 = Y; if (Console.KeyAvailable) { var command = Console.ReadKey().Key; switch (command) { case ConsoleKey.W: Y--; break; case ConsoleKey.S: Y++; break; case ConsoleKey.D: X++; break; case ConsoleKey.A: X--; break; } Console.SetCursorPosition(X1, Y1); Console.Write(". "); } } }