I recently started coding in c#, and I wanted to make a program that draws something in the console, here is my code:
using System;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
private static readonly Random random = new Random();
// Console width and height
static int width = Console.WindowWidth;
static int height = Console.WindowHeight;
// Declare window grid
static string[,] Grid = new string[height, width];
public static void Main(string[] args)
{
init();
loop();
Console.ReadKey();
}
// Game loop
static async void loop()
{
bool running = true;
int i = 0;
while (running)
{
Grid[0, i] = "#";
render();
i++;
await Task.Delay(1000);
}
}
static void init()
{
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
Grid[y, x] = random.Next(0, 2) == 1? "#" : " ";
}
}
}
static void render()
{
string temp = "";
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
string current = Grid[y, x];
temp += current;
}
temp += "\n";
}
Console.Clear();
Console.Write(temp);
}
}
}
I have a problem though, when I refresh the console, it flickers ever so slightly, but very noticeably. Is there a solution for this? Or a better way to achieve my goal? Thanks in advance!