使用enter键绕过c#中的多个foreach循环



我目前正在c#主机上制作一款文本冒险游戏,在游戏开始前显示介绍。我正在寻找一种方法来循环通过长介绍没有按下键,但允许用户按"Enter"在任何时候跳过其余的文本到结束。冗长的介绍会在屏幕上滚动文本,但如果你要多次播放,这可能会很乏味。

我已经尝试使用控制台。ReadKey和Console。Keyavailable到目前为止在while循环和if语句,但我有点卡住。目前,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);

谢谢你的帮助

有很多方法可以做到这一点。这里是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

<子>免责声明这并不是关于完美代码或游戏设计的堡垒,只是关于思考的食物

最终提示用户是否想要查看介绍,因为这最终成为获得预期效果的最简单方法。

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("nnPress 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("nnPress ENTER to Proceed...");
Console.ReadLine();
}
ship();
}

相关内容

  • 没有找到相关文章

最新更新