所以我有一个控制台游戏应用程序,该应用程序生成一个x长度的随机字符串,用户必须在时间用完或丢失之前输入相同的随机字符串。时间只是Game类中保存的一个int值。这是我用来绘制模板的类:
class Template
{
public static void DrawGameplay() //draws gameplay screen , the main method used to print
{
Console.WriteLine("=====================BOMB DEFUSE========================");
if (Game.level < 10)
{
Console.WriteLine("====================== Level {0} =========================", Game.level);
if(Game.level < 6)
{
Body(16);
}
else
{
Body(13);
}
}
else
{
if (Game.level == 10) //10 special case
{
Console.WriteLine("====================== Level {0} ========================", Game.level);
Body(13);
}
else
{
Console.WriteLine("====================== Level {0} ========================", Game.level);
Body(11);
}
}
}
public static void TimeLine() //draws the time line in the middle , feeds drawgameplay
{
Console.WriteLine("");
Console.WriteLine("");
if (Game.time > 9)
{
Console.WriteLine("====================== Time: {0} ========================", Game.time);
}
else
{
Console.WriteLine("====================== Time: {0} =========================", Game.time);
}
Console.WriteLine("");
}
public static void Body(int spaces) //prints everything under the top (game title and lvl) , feeds drawgameplay
{
Console.WriteLine("");
Words.Spaces(spaces);
Game.PrintList();
TimeLine();
Words.Spaces(spaces);
}
}
然后我有这个球员课程:
class Player
{
public static List<char> inputList = new List<char>(); //the list of inputted chars that is used to compare to the defuse code
static char[] arrayChar;
static string typed;
public static void GetInputList() //populates list with letters person typed during time
{
typed = Console.ReadLine();
typed.TrimEnd(); //Gets rid of any extra spaces at the end
arrayChar = typed.ToArray();
inputList = arrayChar.ToList();
}
public static void Print() // prints inputed letters
{
foreach (var letter in inputList)
{
Console.Write(letter);
}
}
}
如何做到这一点,以便在计时器倒计时时更新可视时间,而不会干扰用户的输入?现在,我可以看到时间线在随机生成的字符串和用户输入之间。感谢您的关注。
答案 0 :(得分:0)
我只是出于好奇而尝试过。
这是一个如何运行应用程序并获取用户输入密钥的小例子:
static void Main()
{
StringBuilder msg = new StringBuilder(10);
var endTime = DateTime.Now.AddSeconds(10);
while (endTime > DateTime.Now)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey();
msg.Append(key.KeyChar);
}
Thread.Sleep(1);
Console.Write("\r Enter the message within {0} seconds; message = {1}", (endTime - DateTime.Now).Seconds, msg);
}
}
但是请记住,这段代码有很多问题。幸福的场景会起作用,但是有很多事情会导致问题(非标准/非预期的用户行为只是可能的问题之一)。