c# - 重绘时控制台闪烁

标签 c# console-application

我最近开始用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);
            }
        }
    }

我有一个问题,当我刷新控制台时,它会轻微闪烁,但非常明显。有解决方案吗?或者更好的方法来实现我的目标?提前致谢!

最佳答案

问题是当您调用 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()
{
    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
        }
    }
}

关于c# - 重绘时控制台闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67850823/

相关文章:

c++ - 如何重定向 stdout 和 stderr 流(多平台)?

c - RNG 字符打印功能 - 每页有多于一行 RNG 字符? : C

c# - 在 WPF 中关闭没有代码隐藏的窗口

c# - 使用可视化 C# 后台 worker 来更新数据库?

c# - SignalR 和异构类型的客户端

c# - 从控制台应用程序创建 Windows 服务

c# - 部署 C# + Linq 控制台应用程序?

spring - 注释扫描不扫描类路径中的外部 jar

c# - 学习 C# 和方法内部的对象让我感到困惑

C#:资源文件重构