问题描述
我想提出一个C#控制台基于文本的游戏,因为我希望它看起来更老派,因此任何文本(描述,教程,对话)看起来像是正在键入我添加的效果,它看起来是这样的:
I am making a C# console text-based game, and because I wanted it to look more old-school, I've added an effect so that any text (descriptions, tutorials, dialogues) looks like it's being typed, and it looks like this:
public static int pauseTime = 50;
class Writer
{
public void WriteLine(string myText)
{
int pauseTime = MainClass.time;
for (int i = 0; i < myText.Length; i++)
{
Console.Write(myText[i]);
System.Threading.Thread.Sleep(pauseTime);
}
Console.WriteLine("");
}
}
但转念一想,这可能是讨厌我想过将可以选择跳过的影响,使所有文字出现一次。所以我选择了回车键是跳跃键,它使文本出现一次,但按下回车键还创建了一个新的文本行,争先恐后的文本。
But then I thought that this might be annoying and I thought about adding an option to skip the effect and make all the text appear at once. So I chose the Enter key to be the "skip" key, and it makes the text appear at once, but pressing the enter key also creates a new text line, scrambling the text.
所以我想以某种方式禁用用户的输入,使用户不能写在控制台东西。有没有一种办法,比如,禁用命令提示符(通过命令提示符我不是指CMD.EXE,但闪烁的_下划线符号)?
So I want to somehow disable user input, so that the user cannot write anything in the console. Is there a way to, for example, disable the command prompt (and by command prompt I don't mean cmd.exe, but the flashing "_" underscore sign)?
推荐答案
我想你想要的是 Console.ReadKey(真)
将拦截按键,并不会显示出来。
I think what you want is Console.ReadKey(true)
which will intercept the pressed key and won't display it.
class Writer
{
public void WriteLine(string myText)
{
for (int i = 0; i < myText.Length; i++)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Enter)
{
Console.Write(myText.Substring(i, myText.Length - i));
break;
}
Console.Write(myText[i]);
System.Threading.Thread.Sleep(pauseTime);
}
Console.WriteLine("");
}
}
来源:的
这篇关于禁用用户输入在控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!