Skip to main content
 首页 » 编程设计

c#之Console.Clear() 闪烁

2024年02月06日14kuangbin

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); 
} 

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

请您参考如下方法:

尝试将所有场景汇总到 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);