C# 控制台在重画时闪烁

C# 控制台在重画时闪烁,c#,console-application,C#,Console Application,我最近开始用c#编写代码,我想制作一个在控制台中绘制东西的程序,下面是我的代码: using System; using System.Threading.Tasks; namespace HelloWorld { class Program { private static readonly Random random = new Random(); // Console wid

我最近开始用c#编写代码,我想制作一个在控制台中绘制东西的程序,下面是我的代码:

    using System;
    using System.Threading.Tasks;

    namespace HelloWorld
    {
        class Program
        {
            private static readonly Random random = new Random();

            // Console width and height
            static int width = Console.WindowWidth;
            static int height = Console.WindowHeight;

            // Declare window grid
            static string[,] Grid = new string[height, width];

            public static void Main(string[] args)
            {
                init();
                loop();

                Console.ReadKey();
            }

            // Game loop

            static async void loop()
            {
                bool running = true;
                int i = 0;

                while (running)
                {
                    Grid[0, i] = "#";

                    render();

                    i++;
                    await Task.Delay(1000);
                }
            }

            static void init()
            {
                for(int y = 0; y < height; y++)
                {
                    for(int x = 0; x < width; x++)
                    {
                        Grid[y, x] = random.Next(0, 2) == 1? "#" : " ";
                    }
                }
            }

            static void render()
            {
                string temp = "";

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        string current = Grid[y, x];

                        temp += current;
                    }
                    temp += "\n";
                }

                Console.Clear();
                Console.Write(temp);
            }
        }
    }
使用系统;
使用System.Threading.Tasks;
名称空间HelloWorld
{
班级计划
{
私有静态只读随机=新随机();
//控制台宽度和高度
静态int-width=Console.WindowWidth;
静态内部高度=Console.WindowHeight;
//声明窗口网格
静态字符串[,]网格=新字符串[高度,宽度];
公共静态void Main(字符串[]args)
{
init();
loop();
Console.ReadKey();
}
//游戏循环
静态异步void循环()
{
bool running=true;
int i=0;
(跑步时)
{
网格[0,i]=“#”;
render();
i++;
等待任务。延迟(1000);
}
}
静态void init()
{
对于(int y=0;y

不过我有一个问题,当我刷新控制台时,它的闪烁非常轻微,但非常明显。有解决办法吗?还是一个更好的方法来实现我的目标?提前谢谢

问题是,当您调用
Console.Clear()时,现有内容正在被删除,即使它保持不变。您正在立即将其写回,但正如您所发现的,有足够的延迟使其显示为闪烁

由于每次都要重写整个网格,我建议您使用
Console.SetCursorPosition(0,0)这将把写入的起始位置移回起始位置,然后它将覆盖所有内容,而无需首先清除控制台。这将消除闪烁

static void render()
{
    string temp = "";

    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            string current = Grid[y, x];

            temp += current;
        }
        temp += "\n";
    }

    Console.SetCursorPosition(0, 0); // reset the cursor position
    Console.Write(temp);
}
static void render()
{
字符串temp=“”;
对于(int y=0;y
我甚至可以完全删除字符串构建,只更新控制台中的单个字符:

static void render()
{
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            Console.SetCursorPosition(x, y); // set the position to x,y
            string current = Grid[y, x];
            Console.Write(current); // write this value
        }
    }
}
static void render()
{
对于(int y=0;y
谢谢,伙计,这帮了我很大的忙!另一个问题是,对于我的代码,是否有任何清理/处理方法会有所不同?我一直在学习c#但不确定我是否做对了…:)@Coenicorn唯一让我感到吃惊的是,你用
字符串
来保存一个字符,所以也许你可以将网格数组从
字符串[,]
更改为
字符[,]
,然后替换
网格[0,i]=“#”带有
网格[0,i]='#'
(字符由
表示,而字符串由
表示)。如果您想查看代码,这里有一个专用的堆栈交换站点,它可能对您有用:。请注意,这里只接受工作代码。谢谢!接受回答顺便说一句:)另外,还有一个问题,当我在visual studio中生成此文件并运行.exe文件时,显示的字符被一个空字符分隔,您认为你知道为什么吗?我不知道。在调试和发布版本中都很好。因为你的初始化是随机的,我建议暂时用一个特定的种子创建
random
(例如
random=newrandom(1)
),这样你就可以直接比较相同的“映射”“从VS启动时从EXE启动。这可能与它所显示的控制台窗口有关。