【问题标题】:Using enter key to bypass multiple foreach loops in C#在 C# 中使用 enter 键绕过多个 foreach 循环
【发布时间】:2021-12-06 05:17:25
【问题描述】:

我目前正在 C# 控制台中制作一个文本冒险游戏,它会在游戏开始前显示简介。我正在寻找一种方法来循环浏览长介绍而不按下任何键,但允许用户在任何时候按“Enter”以将其余文本跳过到最后。长长的介绍会在屏幕上滚动文本,但如果您要播放多次,这可能会很乏味。

到目前为止,我已经尝试在 while 循环和 if 语句中使用 Console.ReadKey 和 Console.Keyavailable,但有点卡住了。因为目前 foreach 语句没有处于任何循环中,只是打印直到完成。

//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);

感谢您的帮助

【问题讨论】:

  • 欢迎。它总是开始注意到一个模式,在你的情况下,这将是:1.询问用户他是否想跳过文本 - 2. 如果不是,打印文本,否则跳过它 - 3. 重复下一个文本。将你的文本保存在一个列表中并循环播放它们怎么样?就像你现在对每个字符所做的那样?
  • 哦,谢谢,我试试看。不得不承认还是有点 C# 的初学者,所以使用控制台游戏来提高我的技能。仍然不太清楚如何正确实施列表。
  • 不用担心,this ( + related MSDN doc) 可能值得一读,还有 this 关于如何遍历列表。

标签: c# text foreach console-application keypress


【解决方案1】:

最终提示用户查看他们是否想首先查看介绍,因为这是获得所需效果的最简单方法。

            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();
        }

【讨论】:

    【解决方案2】:

    有很多方法可以做到这一点。这是TaskCancelationToken

    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");
    

    输出

    press any key to stop
    0
    1
    2
    Finished
    

    免责声明:这并不是完美代码或游戏设计的堡垒,只是值得深思

    【讨论】:

    • 谢谢,由于我目前的技能,最终变得更简单了,但非常感谢您的帮助。
    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 2014-08-02
    • 1970-01-01
    • 2020-02-06
    • 2022-12-17
    • 1970-01-01
    相关资源
    最近更新 更多