Jasmine Appelblad
Jasmine Appelblad

Reputation: 1600

WriteLine on Console but in Retro Style

I am trying to write on Console let's say "Enter your User Name:" and what I know is to use Console.WriteLine("Enter your...");

But I want this message of prompt appears as its' being typed like Aliens or star trek computers. Your expert answer with best practices is much appreciated. Thanks

Upvotes: 3

Views: 1118

Answers (6)

danijels
danijels

Reputation: 5291

I think using Random to sleep thread makes for a nice touch.

    private static void RetroConsoleWriteLine()
    {
        const string message = "Enter your user name...";
        var r = new Random();
        foreach (var c in message)
        {
            Console.Write(c);
            System.Threading.Thread.Sleep(r.Next(50,300));
        }
        Console.ReadLine();
    }

Or, if just for the hell of it and to stand out from the rest

    private static void RetroConsoleWriteLine()
    {
        const string message = "Enter your user name...";
        var r = new Random();
        Action<char> action = c =>
        {
            Console.Write(c);
            System.Threading.Thread.Sleep(r.Next(50, 300));
        };
        message.ToList().ForEach(action);
        Console.ReadLine();
    }

Upvotes: 5

wageoghe
wageoghe

Reputation: 27608

My only addition would be a little bit of randomness (starting with Hans' answer):

public static void WriteSlow(string txt) 
{ 
    Random r = new Random();
    foreach (char ch in txt) 
    { 
        Console.Write(ch); 
        System.Threading.Thread.Sleep(r.Next(10,100)); 
    } 
} 

Upvotes: 1

explorer
explorer

Reputation: 12090

 foreach (var character in "Enter your...")
        {
            Console.Write(item);
            System.Threading.Thread.Sleep(300);

        }

Upvotes: 1

Ezweb
Ezweb

Reputation: 228

You could create a loop over your text, sleeping a small amount of time between letters like:

string text = "Enter your User Name:";
for(int i = 0; i < text.Length; i++)
{
    Console.Write(text[i]);
    System.Threading.Thread.Sleep(50);
}

Upvotes: 1

Hugo Migneron
Hugo Migneron

Reputation: 4907

Just use Thread.Sleep in the System.Threading namespace to add a wait between each character.

String text = "Enter your username";
foreach (char c in text)
{
    Console.Write(c);
    System.Threading.Thread.Sleep(100);
 }

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 941455

    public static void WriteSlow(string txt) {
        foreach (char ch in txt) {
            Console.Write(ch);
            System.Threading.Thread.Sleep(50);
        }
    }

Upvotes: 9

Related Questions