Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have about 200 lines of code, really simple oop.

When I load the console game (snake), I don't see it running smooth, like fps problems in in games, but I don't understand why, because my files are a lot lighter and demand less resources than heavy games.

Thanks.

Linking my files to Pastebin

Main file: http://pastebin.com/60aeEBRa

SnakeObject.cs: http://pastebin.com/z0MPwNSA

Utilities.cs: http://pastebin.com/7jLDKUQ9

FoodObject.cs: http://pastebin.com/w1HuBQK4

RocksObject.cs: http://pastebin.com/QYcNbSad

Much thanks for your help.

I'm welcoming tips.

Thanks

share|improve this question
1  
You should accept the answer on your other question if it helped you. –  David Gouveia Mar 5 '12 at 16:29
    
What do you mean by the game not running smoothly? Does the time between moves vary, or does it take long time between moves? If latter, then you should lower the value you pass to Thread.Sleep as I mentioned in my answer. –  Jaakko Lipsanen Mar 5 '12 at 17:04
    
You should look into some kind of profiler tool, that will tell you where your time is being spent. –  Tetrad Mar 5 '12 at 17:59

2 Answers 2

up vote 7 down vote accepted

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).

share|improve this answer
    
+1. Probably what OP was after –  Jaakko Lipsanen Mar 5 '12 at 17:28
    
How would one work with color using this solution? –  Dan-o Nov 3 '13 at 20:18

This line from SnakeGame.Start() -method doesn't seem right:

System.Threading.Thread.Sleep((int)MS * 10);

At MS 's initial value ( 6.0 ) that line causes the main-thread to sleep 60ms every update, causing you game to run at ~16 fps at maximum.

share|improve this answer
    
So how do I keep a delay, which will run in a high dps? –  Guy David Mar 5 '12 at 16:45
    
A framerate of 16FPS is fine for a text based snake game. In fact since you're forced to move character by character instead of pixel by pixel, even 16FPS might be too fast. –  David Gouveia Mar 5 '12 at 16:52
    
@DavidGouveia Thats true, but when moving down or up it MS's value is 9.6, causing the game to run at ~10fps. Though that should be fast enough for console snake still. –  Jaakko Lipsanen Mar 5 '12 at 16:59
    
@user1162727 Try to change "(int)MS * 10" to some constant value, for example 33 ( for 30fps ). If that's too fast, try something higher like 50. –  Jaakko Lipsanen Mar 5 '12 at 17:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.