Created
August 4, 2022 17:32
-
-
Save JerryNixon/cb8babe5a4295ebd8a900e9c336f1787 to your computer and use it in GitHub Desktop.
A simple console sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Console.Clear(); | |
Console.CursorVisible = false; | |
var cat = new Cat(); | |
while (true) | |
{ | |
cat.Erase(); | |
if (Console.KeyAvailable) | |
{ | |
_ = Console.ReadKey(true).Key switch | |
{ | |
ConsoleKey.LeftArrow => cat.MoveLeft(), | |
ConsoleKey.UpArrow => cat.MoveUp(), | |
ConsoleKey.RightArrow => cat.MoveRight(), | |
ConsoleKey.DownArrow => cat.MoveDown(), | |
_ => default, | |
}; | |
} | |
cat.Draw(); | |
System.Threading.Thread.Sleep(100); | |
} | |
public class Cat | |
{ | |
private int _x = 10; | |
private int _y = 10; | |
public int MoveUp() => --_y; | |
public int MoveDown() => ++_y; | |
public int MoveLeft() => --_x; | |
public int MoveRight() => ++_x; | |
public void Erase() => Write(" ", ConsoleColor.Black, ConsoleColor.Black); | |
public void Draw() => Write("x", ConsoleColor.White, ConsoleColor.Black); | |
private void Write(string text, ConsoleColor f, ConsoleColor b) | |
{ | |
Console.ForegroundColor = f; | |
Console.BackgroundColor = b; | |
Console.SetCursorPosition(_x, _y); | |
Console.Write(text); | |
Console.ResetColor(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment