pascalp pascalp - 1 year ago 72
C# Question

How to make small changes for console output without repeating all unchanged values

I am trying to create a text game in C# with console interface

The basic process goes like this:

  1. Take user input.

  2. Manipulate the map (2d array) based on the user input.

  3. Print out the map.

  4. Repeat.

The game works fine, but my problem is that it flickers whenever you try to move the character because it has to go through an entire nested for loop to print out the map each time.

Can I change still different parts of the console output to avoid flickers?

My current actual code for display map at every step:

for (int y = 0; y < 15; y++)
for (int x = 0; x < 58; x++)
// map is an array with current state for each cell on the map
Console.Write(map[y, x]);

Answer Source

You should not reprint all the map for each cycle. The better way is to use Console.SetCursorPosition method and rewrite just modified symbols:

foreach(var changedSymbol in changes)
    Console.SetCursorPosition(changedSymbol.Row, changedSymbol.Column)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download