Skip to main content
added 1 characters in body
Source Link
David Gouveia
  • 25k
  • 5
  • 88
  • 127

You're doing a lot of Console.SetCursorPosition and Console.Write all over the place. The problem with this approach is that you'll be able to see the characters being drawn one by one, and even at a fast rate such as 16 updates per second, you will still be able to notice a lot of flickering because of this. In a graphics application this is what happens when you don't use double buffering - you see the image being rendered instead of just seeing the complete picture.

The solution is simple, render everything to a buffer, and after everything is done, write the complete buffer to the console in one go at the end.

In your other question I explained how to do that, so I'll copy from there with a few optimizations:

// Create buffer to render to (create only once and store at class level)
char[][] render = new char[height][];
for(y=0; y<height; ++y)
    render[y] = new char[width];

// Clear buffer
for(y=0; y<height; ++y)
    for(x=0; x<width; ++x)
        render[y,x]render[y][x] = ' ';

// Render everything to buffer
map.Draw(render);
snake.Draw(render);
rocks.Draw(render);
food.Draw(render);

// Render to console
Console.Clear();
for(y=0; y<height; ++y)
    Console.WriteLine(render[y]);

All the other classes take the render buffer as a parameter to the Draw method, and rendering to it is as simple as doing, for instance:

render[2][2] = '#';

Notice that I also changed from using a multi-dimensional array (name[,]) to a jagged array (name[][]) in order to be able to pass each row as an individual array to Console.WriteLine (to the version that takes an array of chars).

You're doing a lot of Console.SetCursorPosition and Console.Write all over the place. The problem with this approach is that you'll be able to see the characters being drawn one by one, and even at a fast rate such as 16 updates per second, you will still be able to notice a lot of flickering because of this. In a graphics application this is what happens when you don't use double buffering - you see the image being rendered instead of just seeing the complete picture.

The solution is simple, render everything to a buffer, and after everything is done, write the complete buffer to the console in one go at the end.

In your other question I explained how to do that, so I'll copy from there with a few optimizations:

// Create buffer to render to (create only once and store at class level)
char[][] render = new char[height][];
for(y=0; y<height; ++y)
    render[y] = new char[width];

// Clear buffer
for(y=0; y<height; ++y)
    for(x=0; x<width; ++x)
        render[y,x] = ' ';

// Render everything to buffer
map.Draw(render);
snake.Draw(render);
rocks.Draw(render);
food.Draw(render);

// Render to console
Console.Clear();
for(y=0; y<height; ++y)
    Console.WriteLine(render[y]);

All the other classes take the render buffer as a parameter to the Draw method, and rendering to it is as simple as doing, for instance:

render[2][2] = '#';

Notice that I also changed from using a multi-dimensional array (name[,]) to a jagged array (name[][]) in order to be able to pass each row as an individual array to Console.WriteLine (to the version that takes an array of chars).

You're doing a lot of Console.SetCursorPosition and Console.Write all over the place. The problem with this approach is that you'll be able to see the characters being drawn one by one, and even at a fast rate such as 16 updates per second, you will still be able to notice a lot of flickering because of this. In a graphics application this is what happens when you don't use double buffering - you see the image being rendered instead of just seeing the complete picture.

The solution is simple, render everything to a buffer, and after everything is done, write the complete buffer to the console in one go at the end.

In your other question I explained how to do that, so I'll copy from there with a few optimizations:

// Create buffer to render to (create only once and store at class level)
char[][] render = new char[height][];
for(y=0; y<height; ++y)
    render[y] = new char[width];

// Clear buffer
for(y=0; y<height; ++y)
    for(x=0; x<width; ++x)
        render[y][x] = ' ';

// Render everything to buffer
map.Draw(render);
snake.Draw(render);
rocks.Draw(render);
food.Draw(render);

// Render to console
Console.Clear();
for(y=0; y<height; ++y)
    Console.WriteLine(render[y]);

All the other classes take the render buffer as a parameter to the Draw method, and rendering to it is as simple as doing, for instance:

render[2][2] = '#';

Notice that I also changed from using a multi-dimensional array (name[,]) to a jagged array (name[][]) in order to be able to pass each row as an individual array to Console.WriteLine (to the version that takes an array of chars).

Source Link
David Gouveia
  • 25k
  • 5
  • 88
  • 127

You're doing a lot of Console.SetCursorPosition and Console.Write all over the place. The problem with this approach is that you'll be able to see the characters being drawn one by one, and even at a fast rate such as 16 updates per second, you will still be able to notice a lot of flickering because of this. In a graphics application this is what happens when you don't use double buffering - you see the image being rendered instead of just seeing the complete picture.

The solution is simple, render everything to a buffer, and after everything is done, write the complete buffer to the console in one go at the end.

In your other question I explained how to do that, so I'll copy from there with a few optimizations:

// Create buffer to render to (create only once and store at class level)
char[][] render = new char[height][];
for(y=0; y<height; ++y)
    render[y] = new char[width];

// Clear buffer
for(y=0; y<height; ++y)
    for(x=0; x<width; ++x)
        render[y,x] = ' ';

// Render everything to buffer
map.Draw(render);
snake.Draw(render);
rocks.Draw(render);
food.Draw(render);

// Render to console
Console.Clear();
for(y=0; y<height; ++y)
    Console.WriteLine(render[y]);

All the other classes take the render buffer as a parameter to the Draw method, and rendering to it is as simple as doing, for instance:

render[2][2] = '#';

Notice that I also changed from using a multi-dimensional array (name[,]) to a jagged array (name[][]) in order to be able to pass each row as an individual array to Console.WriteLine (to the version that takes an array of chars).