Console flickering on redraw

Viewed 250

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!

1 Answers

The issue is that the existing content is being removed when you call Console.Clear();, even if it's unchanged. You're writing it back immediately, but there is enough of a delay for this to present as a flicker, as you've found.

Since you're rewriting the entire grid again every time, I would suggest you use Console.SetCursorPosition(0, 0); This will move the start position for writing back to the beginning, and then it will overwrite everything without first clearing the console. This should eliminate the flicker.

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.SetCursorPosition(0, 0); // reset the cursor position
    Console.Write(temp);
}

I would go so far as to remove the string building altogether, and just update the individual characters in the console:

static void render()
{
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            Console.SetCursorPosition(x, y); // set the position to x,y
            string current = Grid[y, x];
            Console.Write(current); // write this value
        }
    }
}
Related