public virtual void MoveXY(map M, int X, int Y) { if (X < M.Width && X > 0 && Y < M.Height && Y >= 0) { //replace the old x,y position of the player with map's character. Console.SetCursorPosition(this.OldX, this.OldY); Console.Write(M.CharArray[this.OldX, this.OldY]); Console.SetCursorPosition(X, Y); Console.Write(this.Char); //This is done on purpose. Make sure to always set this way //to prevent disappearing text from occuring this.X = this.OldX = X; this.Y = this.OldY = Y; } else { Console.SetCursorPosition(this.OldX, this.OldY); Console.Write(this.Char); } }
public virtual void MoveUp(map M) { this.MoveXY(M, this.X, this.Y - 1); }
public virtual void MoveRight(map M) { this.MoveXY(M, this.X + 1, this.Y); }
public virtual void MoveLeft(map M) { this.MoveXY(M, this.X - 1, this.Y); }
public virtual void MoveDown(map M) { this.MoveXY(M, this.X, this.Y + 1); }
static void Main(string[] args) { map testMap = new map("map.txt"); testMap.printMapAll(); player Player = new player(); // Prevent example from ending if CTL+C is pressed. Console.TreatControlCAsInput = true; // Hide the cursor from blinking while we are running the game Console.CursorVisible = false; //key variable is used for the input switch below ConsoleKey Key; do { //Console.Write(" --- You pressed "); Key = System.Console.ReadKey(true).Key; switch (Key) { //respond to player's input case ConsoleKey.UpArrow: //move the player up one space Player.MoveUp(testMap); break; case ConsoleKey.DownArrow: Player.MoveDown(testMap); break; case ConsoleKey.LeftArrow: Player.MoveLeft(testMap); break; case ConsoleKey.RightArrow: Player.MoveRight(testMap); break; } //Console.WriteLine(System.Console.ReadKey(true).Key.ToString()); } while (Key != ConsoleKey.Escape); }