C#-移除闪烁控制台内容
我制作了一个简单的c#控制台应用程序,它可以用作一种图形系统(或者其他什么,它只是在屏幕上显示50次随机颜色的ascii图像)。但我遇到了一个问题:控制台内容正在闪烁。我知道这是因为我反复执行Console.Clear()命令,但如何解决它呢?有人制作了一个ascii格式的3D渲染器,它比我的10x32图片序列大很多,但是在那里你看不到这个问题 代码如下:C#-移除闪烁控制台内容,c#,ascii,console-application,C#,Ascii,Console Application,我制作了一个简单的c#控制台应用程序,它可以用作一种图形系统(或者其他什么,它只是在屏幕上显示50次随机颜色的ascii图像)。但我遇到了一个问题:控制台内容正在闪烁。我知道这是因为我反复执行Console.Clear()命令,但如何解决它呢?有人制作了一个ascii格式的3D渲染器,它比我的10x32图片序列大很多,但是在那里你看不到这个问题 代码如下: using System; using System.Collections.Generic; using System.Linq; usi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
begin:
for(int i = 0; i < 50; i++)
{
Console.Clear();
for (int y = 0; y < 10; y++)
{
for (int x = 0; x < 32; x++)
{
Console.ForegroundColor = (ConsoleColor)new Random().Next(1, 5);
Console.Write("\u2588");
}
Console.WriteLine();
}
}
string input = Console.ReadLine();
if (input == "repeat")
goto begin;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
开始:
对于(int i=0;i<50;i++)
{
Console.Clear();
对于(int y=0;y<10;y++)
{
对于(int x=0;x<32;x++)
{
Console.ForegroundColor=(ConsoleColor)new Random().Next(1,5);
控制台。写入(“\u2588”);
}
Console.WriteLine();
}
}
字符串输入=Console.ReadLine();
如果(输入=“重复”)
去开始;
}
}
}
如果目标是继续覆盖同一图像,您可以将光标位置重置为起始位置(0,0),这将防止清除引起的闪烁:
var rnd = new Random(); //defined the random outside the for-loop for reusing
do //replaced the goto with a do..while
{
Console.Clear(); //call clear once before the main loop to reset the input
for (int i = 0; i < 50; i++)
{
Console.SetCursorPosition(0, 0); //go back to the top instead of clearing
for (int y = 0; y < 10; y++)
{
for (int x = 0; x < 32; x++)
{
Console.ForegroundColor = (ConsoleColor)rnd.Next(1, 5);
Console.Write("\u2588");
}
Console.WriteLine();
}
}
Console.ResetColor();
} while (Console.ReadLine() == "repeat");
var rnd=new Random()//在for循环外部定义了随机变量以供重用
do//将goto替换为do..while
{
Console.Clear();//在主循环之前调用Clear一次以重置输入
对于(int i=0;i<50;i++)
{
Console.SetCursorPosition(0,0);//返回顶部而不是清除
对于(int y=0;y<10;y++)
{
对于(int x=0;x<32;x++)
{
Console.ForegroundColor=(ConsoleColor)rnd.Next(1,5);
控制台。写入(“\u2588”);
}
Console.WriteLine();
}
}
Console.ResetColor();
}while(Console.ReadLine()=“repeat”);
另外,你不应该养成使用goto
语句的习惯。通常有更好的方法来实现相同的功能。可能重复:@DarrenYoung肯定同意:)。谢谢!像素的随机性似乎也有所改善。我将尝试使用其他一些符号,因为它们是矩形(而不是正方形)