I'm trying to write a simple console based game engine in C# for fun. Right now what I'm trying to do is establish a simple game loop where the "game" waits for user input, updates then renders then clears the screen then repeats.
This is my code so far:
int x = 0;
bool quit = false;
Task.Run(async () =>
{
try // Catch any issues
{
while (!quit)
{
Console.WriteLine("render");
Console.WriteLine("Position of implicit object: {0}", x);
await Task.Delay(100);
Console.Clear();
}
}
catch (Exception e) { Console.WriteLine(e); }
});
while (!quit)
{
ConsoleKeyInfo keyInfo = Console.ReadKey(false);
ConsoleKey key = keyInfo.Key;
switch (key)
{
case ConsoleKey.D:
x++;
Console.WriteLine("A-key pressed");
break;
case ConsoleKey.A:
x--;
Console.WriteLine("D-key pressed");
break;
case ConsoleKey.Escape:
quit = true;
break;
}
}
But the issue is, there's a constant flicker when the console is updating. I heard Console.SetCursorPosition(0, 0);
might work but when I try that, it seems that the updating stops when the user clicks the screen.
Is there another way to do it? Even if it involves changing the method of the game loop itself.
Thank you!