FFIX February 2016

How to make text flash different colors in a C# console application?

I have this program http://pastebin.com/uHNi15pW

I want this code to flash all of the colors avalible

Console.WriteLine("Welcome to Tic Tac Toe!");

How do I go about this?

Answers


blogprogramisty.net February 2016

Try this

            while (true)
            {
                foreach (ConsoleColor c in Enum.GetValues(typeof(ConsoleColor)))
                {
                    Console.ForegroundColor = c;
                    Console.WriteLine("Welcome to Tic Tac Toe!");
                    Console.Clear();
                }
            }

You can add some deley in foreach to set slow down blinking

          while (true)
            {
                foreach (ConsoleColor c in Enum.GetValues(typeof(ConsoleColor)))
                {
                    Console.ForegroundColor = c;
                    Console.WriteLine("Welcome to Tic Tac Toe!");
                    Thread.Sleep(1000); // 1 sec. deley
                    Console.Clear();
                }
            }

If You want something withoutConsole.Clear() try this: You must set the positions of X and Y

Console.WriteLine("Some text"); // this text will stay when tesxt "Welcome to Tic Tac Toe!" will by blinking

 while (true)
    {
        foreach (ConsoleColor c in Enum.GetValues(typeof(ConsoleColor)))
        {
            Console.CursorLeft = 4; // set position
            Console.CursorTop = 6; // set position
            Console.ForegroundColor = c;
            Console.WriteLine("Welcome to Tic Tac Toe!");

        }
    }

In Your code You must paste code like this befeore do loop:

var task = new Task(() =>
            {
                while (true)
                {
                    foreach (ConsoleColor c in Enum.GetValues(typeof(ConsoleColor)))
                    {
                        var x = Console.CursorLeft;
                        var y = Console.CursorTop;

                        Console.CursorLeft = 0; // set position
                        Console.CursorTop = 0; // set position

                        Console.ForegroundColor = c;
   


René Vogt February 2016

To do this, you will need to know the position of the text on the console (because Console.WriteLine will simply write at the current cursor position). You can do something like this:

public async Task ShowTextInColors(string text, int x, int y, int delay, CancellationToken token)
{
    ConsoleColor[] colors = Enum.GetValues(typeof(ConsoleColor)).OfType<ConsoleColor>().ToArray();

    int color = -1;
    while (!token.IsCancellationRequested)
    {
        color += 1;
        if (color >= colors.Length) color = 0;
        Console.CursorLeft = x;
        Console.CursorTop = y;
        Console.ForegroundColor = colors[color];
        Console.Write(text);
        await Task.Delay(delay, token);
    }
}

x and y determine the cursor position on the console at which you want to display the text.

You can call this like that:

CancellationTokenSource source = new CancellationTokenSource();
ShowTextInColors("Welcome to Tic Tac Toe!", 0, 10, 1000, source.Token);

and eventually stop it by calling

source.Cancel();

Note that this will interfer with other calls to Console.* methods in other threads. And since your question looks like you want to display a tic tac toe game below that line, you may need to synchronize your Console.* calls. But synchronization would be a new question and you will surely find a lot of them on StackOverflow (try lock keyword).

Post Status

Asked in February 2016
Viewed 2,409 times
Voted 7
Answered 2 times

Search




Leave an answer