Console.Clear() 闪烁

Dar*_*v1l 5 c# console

while (true)
{
   Console.Clear();
   for (int row = 0; row < 50; row++)
   {
      for (int col = 0; col < 50; col++)
      {
        Console.Write(world[row, col]);
      }
      Console.WriteLine();
   }
      Thread.Sleep(500);
}
Run Code Online (Sandbox Code Playgroud)

我正在写一个游戏,我有一个由 10 个角色组成的人物。我希望当单击某些箭头按钮时它会在字符数组中移动。问题是这个游戏根本不流畅。使用时Console.Clear(),控制台会反复闪烁,这很烦人。这个问题有什么解决办法吗?(如果我不想使用Console.SetCursorPosition(),因为它会让制作这个游戏变得更加困难)。

cho*_*aib 2

尝试将所有场景汇总到 1 个字符串中,而不是一次绘制它,这会将闪烁效果(隐藏)到某个点:

string scene = "";

// iterate your array to construct the scene string
for (int row = 0; row < 50; row++)
{
   for (int col = 0; col < 50; col++)
   {
      scene += world[row, col];
   }
   scene += '\n'; // new line
}
Console.Clear();  // thanx David
Console.Write(scene);
Run Code Online (Sandbox Code Playgroud)

  • 最好将“Clear”也移动到“Write”上方。 (3认同)
  • 我建议在这里使用“StringBuilder”而不是字符串连接。 (2认同)
  • @itsme86:绝对有效的观点,我刚刚提出了这个(本机)解决方案以及OP的其余部分,我想他从这里开始会做得很好 (2认同)