Reputation: 21
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?
Upvotes: 2
Views: 2034
Reputation: 1772
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;
Console.WriteLine("Welcome to Tic Tac Toe!");
Console.CursorLeft = x;
Console.CursorTop = y;
Thread.Sleep(1000);
}
}
});
do
{
.... rest of code
And change this, after Board create:
Board();// calling the board Function
if (task.Status != TaskStatus.Running)
{
task.Start();
}
choice = int.Parse(Console.ReadLine());//Taking users choice
Full code You have here
https://github.com/przemekwa/ProgramingStudy/blob/master/ProgramingStudy/Study/TikTakTou.cs
And effect will by blinking sign while You play.
Upvotes: 1
Reputation: 43906
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).
Upvotes: 3