stop screen flickering

Sep 6, 2013 at 12:16pm
this need to be a maze for a game i need to have a stable display for the game i tried sleep function but rate of frame per sec still doesnt effect too much flickering

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  int main()
{
	char Map[70][70]= {"#####################################################################",
		               "#      !                                                            #", 
                       "#                                                                   #",
	                   "#                                                                   #",
                       "#                                                                   #",
		               "#                                                                   #",
			           "#                                                                   #",
			           "#@                                                                  #",
			           "#####################################################################" };
	
	while (GameStatus == true)
	{
		system("CLS");
		for(int i=0;i<70;i++)
		{
			cout<<Map[i]<<endl;
		}
		
Sep 6, 2013 at 1:11pm
And how do i set timer in this program
Sep 6, 2013 at 1:33pm
closed account (28poGNh0)
Using Sleep does not help in this case ,because at last the delay of the Sleep function ,ends your loop goes through another periode erase what's in the screen ,THAT'S THE FLICKERING

1 : so, when you gaming developping ,You need to wait for user typing commands(go right,go left ,fire...) You can do that with getch function

2 : The background does not need to be erased everytime ,only the sprite how gets erased and drawed

PS : why are you using 70 lines when you just need 9 ?Do you expect the hero going for walk outside the maze :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# include <iostream>
# include <conio.h>
# include <cstdlib>
# include <windows.h>
using namespace std;

void gotoxy(int x,int y)
{
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}

void drawSprite(int x,int y)
{
    gotoxy(x,y);
    cout << '@';
}

void eraseSprite(int x,int y)
{
    gotoxy(x,y);
    cout << ' ';
}

int main()
{
    char Map[9][70]=
    {  "#####################################################################",
       "#      !                                                            #",
       "#                                                                   #",
       "#                                                                   #",
       "#                                                                   #",
       "#                                                                   #",
       "#                                                                   #",
       "#                                                                   #",
       "#####################################################################" };

    for(int i=0;i<9;i++)
    {
        cout << Map[i]<<endl;
    }

    int i=1;

	while(true)
	{
	    drawSprite(i,7);
		getch();
		eraseSprite(i++,7);
	}
}
Last edited on Sep 6, 2013 at 1:35pm
Sep 6, 2013 at 6:22pm
You could possibly use some of the things in the ctime header to create a timer.

http://www.cplusplus.com/reference/ctime/clock/

or even

http://www.cplusplus.com/reference/ctime/time/

Sep 6, 2013 at 6:25pm
I've been trying to cut back on my replies to these kinds of posts...

... but you really should not be using the console for this. A program like this is MUCH easier to make with a graphics lib like SFML/SDL.
Topic archived. No new replies allowed.