0

I'm trying to make a variation of the game PacMan, and I am running into some problems, one of them beeing not knowing how to print the whole map and pacman moving without the screen flickering. This is what i do for printing:

while(commmand) {
    system("CLS");
    display();
    if(kbhit()) {
        command = getch();
        move(command);
    }

    move(command);
    printf("SCORE: %d",score);
}

command, display, and move are my functions, they work properly. Is there any other way of doing this without the screen flickering?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128

1 Answers1

1

yes there is. You want to look at double-buffering (or multiple-buffering). The general idea, is that you have two (or more buffers), while one buffer, say buffer A, is displayed on the screen, you are preforming all your computations (i.e. physics, movement, AI for the four ghosts) and drawing to the second buffer, say buffer B. When all the computations are done, we swap A and B so that buffer B is being displayed and you use buffer A for calculations.

OpenGL supports double buffering natively, see this tutorial. I've never used DirectX so I can not speak about it, but I would be surprised if it didn't have a way to do double-buffering.

thurizas
  • 2,473
  • 1
  • 14
  • 15
  • I think the original question is about a console application that prints the game with ASCII characters. – Alexguitar Nov 29 '15 at 16:31
  • @Alexguitar you may be correct, but he didn't specify. Double buffering still should work, albeit with more work. The OP will have to manage the buffers himself, and use a low level routing, possibly bitblt, to swap buffers. You have mentioned (n)curses, do they have support for double buffering? – thurizas Nov 29 '15 at 20:44
  • Yeah, it was my bad for not mentioning...I have to do it with ASCII characters..does that change anything? – Rusu Florin Andrei Nov 29 '15 at 20:56
  • Does ncurses work for Windows?My codeblocks doesn't seem to have the library. – Rusu Florin Andrei Nov 29 '15 at 23:54
  • (n)curses comes out of the Unix/Linux world. There appears to be a Windows port (see http://stackoverflow.com/questions/138153/is-ncurses-available-for-windows or http://www.cplusplus.com/forum/general/75406/). At best, you will probably need to download the libraries and necessary headers, install them and then make sure your development environment can find them; at worst you will get source code that may, or may not cleanly compile with your compiler. This, http://cboard.cprogramming.com/game-programming/64294-how-do-i-do-double-buffering-console.html, may be of interest also. – thurizas Nov 30 '15 at 03:25