所以我正在为SENG 100课程进行基于文本的冒险。 我正在使用此功能输出游戏中的任何文字:
static void TypeLine(string line)
{
for (int i = 0; i < line.Length; i++)
{
Console.Write(line[i]);
System.Threading.Thread.Sleep(40); // Sleep for 40 milliseconds
}
Console.Write("\n");
}
有什么方法可以让用户点击一个键来加速或立即完成文本?
答案 0 :(得分:1)
您可以在不阅读按键的情况下检查是否按下了按键:
static void TypeLine(string line) {
for (int i = 0; i < line.Length; ++i) {
Console.Write(line[i]);
if (!Console.KeyAvailable)
System.Threading.Thread.Sleep(40); // Sleep between characters
}
Console.Write("\n");
}
答案 1 :(得分:0)
printText("I Took an Arrow in the Knee");
要打电话:
ngFor
答案 2 :(得分:0)
使用线程的示例
class Program
{
static bool running = true;
static void Main(string[] args)
{
Thread t = new Thread(Keyboard);
t.SetApartmentState(ApartmentState.STA);
t.Start();
Thread t1 = new Thread(Print);
t1.Start();
}
static void Print()
{
TypeLine("hello world");
}
static void TypeLine(string line)
{
for (int i = 0; i < line.Length; i++)
{
Console.Write(line[i]);
if (running)
{
System.Threading.Thread.Sleep(40);
}
}
Console.Write("\n");
}
static void Keyboard()
{
while (running)
{
if (System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.LeftShift))
{
running = false;
}
}
}
}