Home > Software design >  Using enter key to bypass multiple foreach loops in C#
Using enter key to bypass multiple foreach loops in C#

Time:10-19

I am currently making a text adventure game in the C# console that displays an intro before the game starts. I am looking for a way to loop through the long intro with no key pressed, but allow the user to press 'Enter' at any point to skip the rest of the text to the end. The long intro scrolls the text across the screen but this can be tedious if you were to play multiple times.

I have tried using Console.ReadKey and Console.Keyavailable so far in both while loops and if statements but am a bit stuck. As it is at the moment the foreach statements are not in any kind of loop and just print until finished.

//Long Intro
            foreach (char i in introText1)
            {
                Console.Write(i);
                Thread.Sleep(1);
            }
            //Different colour to show importance of ship parts
            Console.ForegroundColor = ConsoleColor.Yellow;
            foreach (char i in introText2)
            {
                Console.Write(i);
                Thread.Sleep(1);
            }
            Console.ForegroundColor = ConsoleColor.White;
            foreach (char i in introText3)
            {
                Console.Write(i);
                Thread.Sleep(1);
            }
            //Different colour to show importance of oxygen
            Console.ForegroundColor = ConsoleColor.Cyan;
            foreach (char i in introText4)
            {
                Console.Write(i);
                Thread.Sleep(1);
            }
            Console.ForegroundColor = ConsoleColor.White;
            foreach (char i in introText5)
            {
                Console.Write(i);
                Thread.Sleep(1);
            }
            foreach (char i in introText6)
            {
                Console.Write(i);
                Thread.Sleep(1);
            }
            //Quick guide to help menu
            Console.ForegroundColor = ConsoleColor.Green;
            foreach (char i in introText7)
            {
                Console.Write(i);
                Thread.Sleep(1);
            }

            //Quick Intro
            Console.WriteLine(introText1);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(introText2);
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(introText3);
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine(introText4);
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(introText5);
            Console.WriteLine(introText6);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(introText7);

Thanks for any help

CodePudding user response:

There are oodles of way to do this. Here is a Task, and CancelationToken

using var cts = new CancellationTokenSource();
var token = cts.Token;

_ = Task.Run(async () =>
{
   var i = 0;
   // some fake loop
   while (!token.IsCancellationRequested)
   {
      Console.WriteLine(i  );
      await Task.Delay(1000);
   }
});

Console.WriteLine("press any key to stop");
Console.ReadKey();
cts.Cancel();
Console.WriteLine("Finished");

Output

press any key to stop
0
1
2
Finished

Disclaimer : This was not meant to be the bastion of perfect code or game design, just food for thought

CodePudding user response:

Ended up prompting the user to see if they want to view the intro in the first place as this ended up being the easiest way to get the desired effect.

            Console.WriteLine("Press ENTER to view intro");
            Console.WriteLine("Press Q to view quick intro");
            Console.WriteLine("Press S to skip intro");
            string temp1 = Console.ReadLine().ToUpper();
            if (temp1 == "Q")
            {
                Console.Clear();
                Console.Write(introText1);
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write(introText2);
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(introText3);
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write(introText4);
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(introText5);
                Console.Write(introText6);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write(introText7);
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("\n\nPress ENTER to Proceed...");
                Console.ReadLine();
            }
            else if(temp1 == "S")
            {
                ship();
            }
            else
            {
                //Long Intro
                Console.Clear();
                foreach (char i in introText1)
                {
                    Console.Write(i);
                    Thread.Sleep(1);
                }
                //Different colour to show importance of ship parts
                Console.ForegroundColor = ConsoleColor.Yellow;
                foreach (char i in introText2)
                {
                    Console.Write(i);
                    Thread.Sleep(1);
                }
                Console.ForegroundColor = ConsoleColor.White;
                foreach (char i in introText3)
                {
                    Console.Write(i);
                    Thread.Sleep(1);
                }
                //Different colour to show importance of oxygen
                Console.ForegroundColor = ConsoleColor.Cyan;
                foreach (char i in introText4)
                {
                    Console.Write(i);
                    Thread.Sleep(1);
                }
                Console.ForegroundColor = ConsoleColor.White;
                foreach (char i in introText5)
                {
                    Console.Write(i);
                    Thread.Sleep(1);
                }
                foreach (char i in introText6)
                {
                    Console.Write(i);
                    Thread.Sleep(1);
                }
                //Quick guide to help menu
                Console.ForegroundColor = ConsoleColor.Green;
                foreach (char i in introText7)
                {
                    Console.Write(i);
                    Thread.Sleep(1);
                }
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("\n\nPress ENTER to Proceed...");
                Console.ReadLine();
            }
            ship();
        }
  • Related