How to write in multiple positions in a console application at the same time? FROM#

I want the console width strings to simultaneously write down one char in the height of the console. I did most of this, but it goes from top to bottom right, etc ....

If you need help portraying what I mean, think about how it rains with the matrix code.

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++;
}
+6
source share
4 answers

, 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 , . , , :

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

, ( 10%), , , ( if ):

    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

+6

, , . , .

. , , Y ( ), X ( ).

x y, , .

// 1 step to (0,0)
*
// 3 steps to (1,1)
**
**
// 5 steps for reaching (2,2)
***
***
***
// 7 steps for reaching (3,3)
****
****
****
// 9 steps for reaching (4,4) and 11 steps for (5,5)...
// I do think everyone could get this pattern

, . , , , , . ( , , , , , - .)

, , . , X Y- , , , , .

.

:

  • , j - x y .
  • (i, j) n * 2 + 1 (I + 1, J + 1)
  • , .

:

  • n * 2 + 1 . , x n y- n , , , (n + 1, n + 1).
  • X y Y x.
  • , (n, n), n = 3, - , n = 4.
  • , (n + 1,0), (n + 1, n)
  • (0, n + 1) (n + 1, n + 1)
  • m = n + 1 ( : (

//calculate how many checkpoints (n)
int checkpoints = 1080;
//n should indicate the actual turn we are instead of naming the total turns like sucks
//The main, the outermost For-loop 
for (int n=0;n<checkpoints;n++)
{
  // The nested step
  for (int y=0;y<n;y++)
  {
    // Just fill in (n+1, y) grid 
    Console.SetCursorPosition(n+1, y);
    Console.Write(".");
  }
  for (int x=0;x<n+1;x++) 
  {         
    // Just fill in (x, n+1) grid
    Console.SetCursorPosition(x, n+1);
    Console.Write(".");
  }
  // Upon completion of each main cycle we have a sleep, yah
  Thread.Sleep(100);
}

, , , 1080x1080.

, 1920x1080 , 16: 9. , , , . ( , : (

( , , . , . , ...)

+1

, :

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

int i = 0;

while (i < h)
{
    Console.WriteLine(new string('.', w-1));
    Thread.Sleep(20);
    i++;
}
0

.

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

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

basically what i did here is just a restructuring of the logic and the right delay in the right position. Hope this helps.

0
source

All Articles