Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#控制台应用程序中使文本闪烁不同的颜色?_C# - Fatal编程技术网

如何在C#控制台应用程序中使文本闪烁不同的颜色?

如何在C#控制台应用程序中使文本闪烁不同的颜色?,c#,C#,我有这个节目 我想让这段代码闪烁所有可用的颜色 Console.WriteLine("Welcome to Tic Tac Toe!"); 我该怎么做呢?试试这个 while (true) { foreach (ConsoleColor c in Enum.GetValues(typeof(ConsoleColor))) { Console.

我有这个节目

我想让这段代码闪烁所有可用的颜色

Console.WriteLine("Welcome to Tic Tac Toe!");
我该怎么做呢?

试试这个

            while (true)
            {
                foreach (ConsoleColor c in Enum.GetValues(typeof(ConsoleColor)))
                {
                    Console.ForegroundColor = c;
                    Console.WriteLine("Welcome to Tic Tac Toe!");
                    Console.Clear();
                }
            }
您可以在foreach中添加一些deley来设置慢速闪烁

          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();
                }
            }
如果您想要不带Console.Clear()的东西,请尝试以下操作:必须设置X和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!");

        }
    }
在代码中,您必须在循环之前粘贴如下代码:

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;
                        Console.WriteLine("Welcome to Tic Tac Toe!");

                        Console.CursorLeft = x;
                        Console.CursorTop = y;

                        Thread.Sleep(1000);
                    }
                }

                });

do
{
.... rest of code
并在线路板创建后更改此选项:

                Board();// calling the board Function

                if (task.Status != TaskStatus.Running)
                {
                    task.Start();
                }

                choice = int.Parse(Console.ReadLine());//Taking users choice  
这里有完整的代码


播放时闪烁符号将产生效果。

要做到这一点,您需要知道文本在控制台上的位置(因为
console.WriteLine
将只在当前光标位置写入)。您可以这样做:

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);
    }
}
最终通过打电话来阻止它

source.Cancel();

请注意,这将干扰其他线程中对
控制台。*
方法的其他调用。由于您的问题看起来像是要在该行下方显示tic-tac-toe游戏,您可能需要同步
控制台。*
调用。但是同步将是一个新问题,您肯定会在StackOverflow上找到很多同步(请尝试
lock
关键字).

刚刚设置属性?我用控制台试过了。清除,但程序的其余部分没有显示更新我的答案——最后一个例子没有
控制台。清除
很抱歉打扰,但我还是遇到了同样的问题。嘿,我复制粘贴了你的代码,效果很好。你想把闪烁的文字放在哪里?当玩家赢了还是什么时候?不,在程序的顶端
source.Cancel();