gpt4 book ai didi

c# - 如何同时在控制台应用程序中的多个位置写入? C#

转载 作者:太空狗 更新时间:2023-10-29 21:26:15 26 4
gpt4 key购买 nike

我希望与控制台宽度一样多的行同时向下写入一个字符到控制台的高度。我已经完成了大部分,但它是从上到下到右等等......

如果您需要帮助来理解我的意思,请想想矩阵码雨。

int w = Console.WindowWidth;
int h = Console.WindowHeight;

int i = 0;
while (i < w)
{
int j = 0;
while (j < h)
{
Thread.Sleep(1);
Console.SetCursorPosition(i, j);
Console.Write(".");
j++;
}
i++;
}

最佳答案

我会做的是构造一个 List<string> lines;这将包含您要写入控制台窗口的行,其中每行与控制台宽度一样宽。然后以相反的顺序将列表打印到控制台窗口,所以第一行(位于 lines[0] 处)将始终是最后打印的行,并且始终位于控制台窗口的底部。


示例实现——有人提到这可能是家庭作业。我不这么认为,但如果是,那么请先尝试自己实现上述想法。

我们可以在用于打印其项目的同一循环中将新项目添加到列表中。然而,在我们添加一行之前,我们首先检查列表中的行数是否已经与控制台窗口中的行数一样多 (Console.WindowHeight)。如果有,那么我们只需删除 lines[0] 处的行在我们添加一个新的之前。这样,List<string> lines与控制台窗口一起“滚动”。

滚动速度由 Thread.Sleep 控制, 但这段代码可以很容易地添加到 Timer 中相反,这样其他工作就可以在后台进行(比如如果这是一个“屏幕保护程序”,你想等待用户输入“唤醒”)。但无论我们如何决定实现速度,我决定创建一个 enum具有表示毫秒数的值 Thread.Sleep实现将使用:

class Program
{
enum MatrixCodeSpeed
{
Fastest = 0,
Faster = 33,
Fast = 67,
Normal = 100,
Slow = 333,
Slower = 667,
Slowest = 1000
}

我还会创建一个辅助方法,为您创建一个“随机”行。它可以接受一个指定“密度”的整数,这意味着您希望在该行中包含多少个字符。 density代表一个百分比,所以如果10指定,然后我们选择一个介于 0 和 99 之间的随机数,如果它小于 10,则我们向字符串添加一个随机矩阵字符(否则我们添加一个空格字符)。

此外,为了更接近地复制矩阵,我还选择了 4 个不同的字符进行打印,每个字符都比前一个稍暗。这增加了三维效果,其中褪色 block 看起来比实心 block 更远:

    private static Random rnd = new Random();

// Add whatever 'matrix' characters you want to this array. If you prefer to have one
// character chosen more often than the others, you can write code to favor a specific
// index, or just add more instances of that character to the array below:

private static char[] matrixChars = new[] { '░', '▒', '▓', '█' };

static string GetMatrixLine(int density)
{
var line = new StringBuilder();

for (int i = 0; i < Console.WindowWidth; i++)
{
// Choose a random number from 0-99 and see if it's greater than density
line.Append(rnd.Next(100) > density
? ' ' // If it is, add a space to reduce line density
: matrixChars[rnd.Next(matrixChars.Length)]); // Pick a random character
}

return line.ToString();
}

接下来,我们有 main 方法,它用随机行填充列表(使用 10% 的密度),然后以相反的顺序在无限循环中一次打印出它们(删除第一行,如果我们需要):

    static void Main()
{
var lines = new List<string>();
var density = 10; // (10% of each line will be a matrix character)
var speed = MatrixCodeSpeed.Normal;

// Hide the cursor - set this to 'true' again before accepting user input
Console.CursorVisible = false;
Console.ForegroundColor = ConsoleColor.DarkGreen;

while (true)
{
// Once the lines count is greater than the window height,
// remove the first item, so that the list "scrolls" also
if (lines.Count >= Console.WindowHeight)
{
lines.Remove(lines[0]);
}

// Add a new random line to the list, which will be the new topmost line.
lines.Add(GetMatrixLine(density));
Console.SetCursorPosition(0, 0);

// Print the lines out to the console in reverse order so the
// first line is always last, or on the bottom of the window
for (int i = lines.Count - 1; i >= 0; i--)
{
Console.Write(lines[i]);
}

Thread.Sleep(TimeSpan.FromMilliseconds((int)speed));
}
}
}

这是它的 gif 动图,一直到屏幕全屏(然后 gif 重复,但代码版本继续正常滚动):

enter image description here

关于c# - 如何同时在控制台应用程序中的多个位置写入? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48373134/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com