How to move the 'smiley' in my code automatically and stop the screen flickering?

Hi guys,
So far I have managed to write the code for a simple game (resembling to "Pacman") :P.
Now what I want is to move the smiley in the upper left-hand corner of the map "automatically" when the program runs and also want it to change it's direction to up or down when it touches the wall.
Also the screen is flickering when the program runs (due to re-printing the whole array?). I want to stop this flicker. Somebody suggested me to use 'gotoxy' function. Well I don't know how to :(. If somebody can help me I shall be thankful to him/her from the bottom of my heart :).

Here's my code :
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
#include <conio.h>
#include <stdlib.h>

using namespace std;

void display (char array [10][25] , int row , int column)
{
	cout << "\t\t\t\tPacman Game\nGame help\nPress (w) for upward direction\nPress (s) for" <<
		    "downward\nPress (a) for right\nPress (d) for left\nEnter to continue\n";
	
	for (int i=0 ; i<10 ; i++)
	{
		cout << "\t" << array[i] << endl;
	}
}

int main()
{
	char array [10][25] = {"XXXXXXXXXXXXXXXXXXXXXXXX",
                           "X............\3.........X",
                           "X.X.\5.XXXXX.XXXXXX...X.X",
                           "X.X...X..........X..\4..X",
                           "X.X...X.......\5..X...X.X",
                           "X.X...XXXXX.XXXXXX.\3.X.X",
                           "X...........\4..........X",
                           "X.XX.XX.XX..XX.XXXX\3XX.X",
                           "X.............\6........X",
                           "XXXXXXXXXXXXXXXXXXXXXXXX"};
	
    int count = 0;
	int score = 0;
	char ch;
	char pacman = '\2';

	//Pacman's initial position

    int x = 1;
	int y = 1;
    array [x][y] = pacman;

	display (array , 10 , 25);
	
	for (int a=0 ; a<10 ; a++)
	{	
		for (int b=0 ; b<25 ; b++)
		{
			if (array [a][b] == '.')    //counting total score of any map (plus 10 for eating dot)
			{
				count = count + 10;
			}

			else if (array [a][b] == '\3' || array [a][b] == '\4' || array [a][b] == '\5' || array [a][b] == '\6')								//plus 20 for eating power pill
			{
				count = count + 20;
			}
		}
	}
	
	do
	{
		system ("cls");
		
		display (array , 10 , 20);
		
		cout << "\nTOTAL : " << count;
		cout << "\nSCORE : " << score << endl;
		
		ch = getch();
		
		if (score == count)
		{
			ch = 'q';
		}

		switch (ch)
		{
		case 's':
			if (array [x+1][y] == 'X')
			{
				break;
			}
			
			else
			{
				if (array [x+1][y] == '.')
				{
					array [x][y] = ' ';
					x ++;
					array [x][y] = pacman;
					score = score + 10;
				}
				
				else
				{
					if (array [x+1][y] == '\3' || array [x+1][y] == '\4' || array [x+1][y] == '\5' || array [x+1][y] == '\6')	
					{
						array [x][y] = ' ';
						x ++;
						array [x][y] = pacman;
						score = score + 20;
					}
						
					else
					{
						array [x][y] = ' ';
						x ++;
						array [x][y] = pacman;
					}
				}
			}
		
			break;
		
		case 'w':
			if (array [x-1][y] == 'X')
			{
				break;
			}
				
			else
			{
				if (array [x-1][y] == '.')
				{
					array [x][y] = ' ';
					x --;
					array [x][y] = pacman;
					score = score + 10;
				}
				
				else
				{
					if (array [x-1][y] == '\3' || array [x-1][y] == '\4' || array [x-1][y] == '\5' || array [x-1][y] == '\6')	
					{
						array [x][y] =' ';
						x --;
						array [x][y] = pacman;
						score = score + 20;
					}
						
					else
					{
						array [x][y] = ' ';
						x --;
						array [x][y] = pacman;
					}
				}
			}
			
			break;
	
		case 'a':
			if (array [x][y-1] == 'X')
			{
				break;
			}
			
			else
			{
				if (array [x][y-1] == '.')
				{
					array [x][y] = ' ';
					y --;
					array [x][y] = pacman;
					score = score + 10;
				}
				
				else
				{
					if (array [x][y-1] == '\3' || array [x][y-1] == '\4' || array [x][y-1] == '\5' || array [x][y-1] == '\6')	
					{
						array [x][y] = ' ';
						y --;
						array [x][y] = pacman;
						score = score + 20;
					}
					
					else
					{
						array [x][y] = ' ';
						y --;
						array [x][y] = pacman;
					}
				}
			}
			
			break;

		case 'd':
			if (array [x][y+1] == 'X')
			{
				break;
			}
			
			else
			{
				if(array [x][y+1] == '.')
				{
					array [x][y] = ' ';
					y ++;
					array [x][y] = pacman;
					score = score + 10;
				}
				
				else
				{
					if (array [x][y+1] == '\3' || array [x][y+1] == '\4' || array [x][y+1] == '\5' || array [x][y+1] == '\6')	
					{
						array [x][y] = ' ';
						y ++;
						array [x][y] = pacman;
						score = score + 20;
					}
					
					else
					{
						array [x][y] = ' ';
						y ++;
						array [x][y] = pacman;
					}

				}
			}
			
			break;
		}
		
	}
	while (ch != 'q');
	
	cout << "\nGAME END\n";
	cout << "\nTOTAL SCORE : " << score << endl;

	return 0;
}
Please.. anyone there??. I know it is a bit awkward, but you could just at least point out what to write and where to write in this code? :/
Give us some time to look it over.
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include<windows.h>//<<<<<<<<<<<<<<<<<<<<<<<<<<<

using namespace std;

void cls()//<<<<<<<<<<<<<<<<<<<<<<<<
{
    HANDLE hOut;
    COORD Position;
    DWORD Written;
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    Position.X = 0;
    Position.Y = 0;
    FillConsoleOutputCharacter(hOut,' ', 0, Position, &Written);
    SetConsoleCursorPosition(hOut, Position);
}

void display (char array [10][25] , int row , int column)
{
	cout << "\t\t\t\tPacman Game\nGame help\nPress (w) for upward direction\nPress (s) for" <<
		    "downward\nPress (a) for right\nPress (d) for left\nEnter to continue\n";

	for (int i=0 ; i<10 ; i++)
	{
		cout << "\t" << array[i] << endl;
	}
}

int main()
{
	char array [10][25] = {"XXXXXXXXXXXXXXXXXXXXXXXX",
                           "X............\3.........X",
                           "X.X.\5.XXXXX.XXXXXX...X.X",
                           "X.X...X..........X..\4..X",
                           "X.X...X.......\5..X...X.X",
                           "X.X...XXXXX.XXXXXX.\3.X.X",
                           "X...........\4..........X",
                           "X.XX.XX.XX..XX.XXXX\3XX.X",
                           "X.............\6........X",
                           "XXXXXXXXXXXXXXXXXXXXXXXX"};

    int count = 0;
	int score = 0;
	char ch;
	char pacman = '\2';

	//Pacman's initial position

    int x = 1;
	int y = 1;
    array [x][y] = pacman;

	display (array , 10 , 25);

	for (int a=0 ; a<10 ; a++)
	{
		for (int b=0 ; b<25 ; b++)
		{
			if (array [a][b] == '.')    //counting total score of any map (plus 10 for eating dot)
			{
				count = count + 10;
			}

			else if (array [a][b] == '\3' || array [a][b] == '\4' || array [a][b] == '\5' || array [a][b] == '\6')								//plus 20 for eating power pill
			{
				count = count + 20;
			}
		}
	}

	do
	{
		//system ("cls");<<<<<<<<<<<<<<<<<<<<<<<<<
		cls();//<<<<<<<<<<<<<<<<<<<<<<<<
		display (array , 10 , 20);

		cout << "\nTOTAL : " << count;
		cout << "\nSCORE : " << score << endl;

		ch = getch();

		if (score == count)
		{
			ch = 'q';
		}

		switch (ch)
		{
		case 's':
			if (array [x+1][y] == 'X')
			{
				break;
			}

			else
			{
				if (array [x+1][y] == '.')
				{
					array [x][y] = ' ';
					x ++;
					array [x][y] = pacman;
					score = score + 10;
				}

				else
				{
					if (array [x+1][y] == '\3' || array [x+1][y] == '\4' || array [x+1][y] == '\5' || array [x+1][y] == '\6')
					{
						array [x][y] = ' ';
						x ++;
						array [x][y] = pacman;
						score = score + 20;
					}

					else
					{
						array [x][y] = ' ';
						x ++;
						array [x][y] = pacman;
					}
				}
			}

			break;

		case 'w':
			if (array [x-1][y] == 'X')
			{
				break;
			}

			else
			{
				if (array [x-1][y] == '.')
				{
					array [x][y] = ' ';
					x --;
					array [x][y] = pacman;
					score = score + 10;
				}

				else
				{
					if (array [x-1][y] == '\3' || array [x-1][y] == '\4' || array [x-1][y] == '\5' || array [x-1][y] == '\6')
					{
						array [x][y] =' ';
						x --;
						array [x][y] = pacman;
						score = score + 20;
					}

					else
					{
						array [x][y] = ' ';
						x --;
						array [x][y] = pacman;
					}
				}
			}

			break;

		case 'a':
			if (array [x][y-1] == 'X')
			{
				break;
			}

			else
			{
				if (array [x][y-1] == '.')
				{
					array [x][y] = ' ';
					y --;
					array [x][y] = pacman;
					score = score + 10;
				}

				else
				{
					if (array [x][y-1] == '\3' || array [x][y-1] == '\4' || array [x][y-1] == '\5' || array [x][y-1] == '\6')
					{
						array [x][y] = ' ';
						y --;
						array [x][y] = pacman;
						score = score + 20;
					}

					else
					{
						array [x][y] = ' ';
						y --;
						array [x][y] = pacman;
					}
				}
			}

			break;

		case 'd':
			if (array [x][y+1] == 'X')
			{
				break;
			}

			else
			{
				if(array [x][y+1] == '.')
				{
					array [x][y] = ' ';
					y ++;
					array [x][y] = pacman;
					score = score + 10;
				}

				else
				{
					if (array [x][y+1] == '\3' || array [x][y+1] == '\4' || array [x][y+1] == '\5' || array [x][y+1] == '\6')
					{
						array [x][y] = ' ';
						y ++;
						array [x][y] = pacman;
						score = score + 20;
					}

					else
					{
						array [x][y] = ' ';
						y ++;
						array [x][y] = pacman;
					}

				}
			}

			break;
		}

	}
	while (ch != 'q');

	cout << "\nGAME END\n";
	cout << "\nTOTAL SCORE : " << score << endl;

	return 0;
}
@Duoas It would be great if you could figure it out in next 15-20 minutes.
--------------------------------------------------------------------------------------------
@Chriscpp Thank you very much man for removing the flickering, but it would be 'SuperAwesome!!' if you could add a few more lines to move that character automatically (just like the real "Pacman" :) ). I shall be waiting for your reply.
The update doesn't actually do what it says..

The update should have been named something like "GoHome" -- since that's all it does.

A function to go to a specific character cell can be found here:
http://www.cplusplus.com/forum/beginner/4234/#msg18563

GoHome() can be written thus:
1
2
3
4
void GoHome()
{
	gotoxy( 0, 0 );
}

You still need to actually clear the screen before the very first call to display() (line 42 in your original code). The code for that on Windows is found here:
http://www.cplusplus.com/articles/4z18T05o/#Windows

So, at the very beginning of your program, you do clear the screen. After that, you just put the cursor in the home position (0, 0), and redraw the screen over what you have already. There is no flicker, because the screen wasn't cleared before redrawing.

You can see an example of all this together here:
http://www.cplusplus.com/forum/lounge/75168/


After that, you need to add in the idea of input and timing. We'll add a similar idea to that of my Game of Life above, except instead of just exiting the loop, we'll stop only long enough to act on the key that is pressed.

You need the Windows Sleep() function to pause your program a little time. Make sure to create a variable that you can use to control how
fast time runs:

unsigned speed = 250; // milliseconds

You can adjust that speed according to how fast or slow you want your game to run. (You may ultimately want to give the user some control over it.)

You will also need the <conio.h> function to test to see if a key is pressed.

Your main event loop will now look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	do
	{
		GoHome();
		display (array , 10 , 20);

		cout << "\nTOTAL : " << count;
		cout << "\nSCORE : " << score << endl;

		// Wait the proper amount of time
		Sleep( speed );
		// if user did not press a key, use value of 1 -- which will not be discovered in the switch below
		if (kbhit()) ch = getch();
		else         ch = 1;

		switch (ch)
		{

Now you will want to make a fairly radical modification. Pac-Man can't be stopped, you see. Once you give him a direction, he wants to keep going that direction. You'll need a new variable, to store his current direction.

1
2
enum direction_t { none, left, right, up, down };
direction_t direction = none;

Each time through the loop, try to update him in that direction. If he makes an attempt to run into the wall, reverse the direction.

And when the user presses a key, set the direction variable only.

[edit again] One more edit -- you can read the arrow keys on the numeric keypad. If getch() returns either 0x00 or 0xE0, then read getch() again -- it will be an "extended key", one of the numeric keypad keys (and other special keys). You'll have to google for the codes -- I'm not sure I can remember exactly what they are and I'm short on time.

Hope this helps.
Last edited on
closed account (j3Rz8vqX)
Hope you don't mind, I've shorten your code a bit.
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include<windows.h>//<<<<<<<<<<<<<<<<<<<<<<<<<<<

using namespace std;

void cls()//<<<<<<<<<<<<<<<<<<<<<<<<
{
    HANDLE hOut;
    COORD Position;
    DWORD Written;
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    Position.X = 0;
    Position.Y = 0;
    FillConsoleOutputCharacter(hOut,' ', 0, Position, &Written);
    SetConsoleCursorPosition(hOut, Position);
}
void inc_a_b(int &a,int &b, int a_buffer,int b_buffer)
{
    if(a_buffer !=0)
    {
        if(a_buffer >0)
        {
            a++;
        }
        else
        {
            a--;
        }
    }
    if(b_buffer !=0)
    {
        if(b_buffer >0)
        {
            b++;
        }
        else
        {
            b--;
        }
    }

}
bool key_input(char array [10][25],int &a,int &b,int &score, char pacman, int a_buffer, int b_buffer)
{
    if (array [a+a_buffer][b+b_buffer] == 'X')
    {
        return true;
    }

    else
    {
        if (array [a+a_buffer][b+b_buffer] == '.')
        {
            array [a][b] = ' ';
            inc_a_b(a,b,a_buffer,b_buffer);
            array [a][b] = pacman;
            score = score + 10;
        }

        else
        {
            if (array [a+a_buffer][b+b_buffer] == '\3' || array [a+a_buffer][b+b_buffer] == '\4' ||
                array [a+a_buffer][b+b_buffer] == '\5' || array [a+a_buffer][b+b_buffer] == '\6')
            {
                array [a][b] = ' ';
                inc_a_b(a,b,a_buffer,b_buffer);
                array [a][b] = pacman;
                score = score + 20;
            }

            else
            {
                array [a][b] = ' ';
                inc_a_b(a,b,a_buffer,b_buffer);
                array [a][b] = pacman;
            }
        }
    }
    return false;
}
void display (char array [10][25] , int row , int column)
{
	cout << "\t\t\t\tPacman Game\nGame help\nPress (w) for upward direction\nPress (s) for" <<
		    "downward\nPress (a) for right\nPress (d) for left\nEnter to continue\n";

	for (int i=0 ; i<10 ; i++)
	{
		cout << "\t" << array[i] << endl;
	}
}

int main()
{
	char array [10][25] = {"XXXXXXXXXXXXXXXXXXXXXXXX",
                           "X............\3.........X",
                           "X.X.\5.XXXXX.XXXXXX...X.X",
                           "X.X...X..........X..\4..X",
                           "X.X...X.......\5..X...X.X",
                           "X.X...XXXXX.XXXXXX.\3.X.X",
                           "X...........\4..........X",
                           "X.XX.XX.XX..XX.XXXX\3XX.X",
                           "X.............\6........X",
                           "XXXXXXXXXXXXXXXXXXXXXXXX"};

    int count = 0;
	int score = 0;
	char ch;
	char pacman = '\2';
	//Pacman's initial position
    int x = 1;
	int y = 1;
    array [x][y] = pacman;
	display (array , 10 , 25);
	for (int a=0 ; a<10 ; a++)
	{
		for (int b=0 ; b<25 ; b++)
		{
			if (array [a][b] == '.')    //counting total score of any map (plus 10 for eating dot)
			{
				count = count + 10;
			}

			else if (array [a][b] == '\3' || array [a][b] == '\4' ||
                     array [a][b] == '\5' || array [a][b] == '\6')//plus 20 for eating power pill
			{
				count = count + 20;
			}
		}
	}
    int x_move,y_move;
	do
	{
	    x_move=y_move=0;
		//system ("cls");<<<<<<<<<<<<<<<<<<<<<<<<<
		cls();//<<<<<<<<<<<<<<<<<<<<<<<<
		display (array , 10 , 20);

		cout << "\nTOTAL : " << count;
		cout << "\nSCORE : " << score << endl;

		ch = getch();

		if (score == count)
		{
			ch = 'q';
		}
		switch (ch)
		{
		case 's':
		    x_move=1;
		    key_input(array,x,y,score,pacman,x_move,y_move);
			break;

		case 'w':
		    x_move=-1;
		    key_input(array,x,y,score,pacman,x_move,y_move);
			break;

		case 'a':
		    y_move=-1;
		    key_input(array,x,y,score,pacman,x_move,y_move);
			break;

		case 'd':
		    y_move=1;
		    key_input(array,x,y,score,pacman,x_move,y_move);
			break;
		}
		//key_input could've gone here, but to remain similar to your code, I've left the codes above.
        //key_input(array,x,y,score,pacman,x_move,y_move);
	}
	while (ch != 'q');
	cout << "\nGAME END\n";
	cout << "\nTOTAL SCORE : " << score << endl;

	return 0;
}


I have not done anything else other than shorten this codes length. (Using recursion)
To enable pacman to continuously walk, you're going to have to build listeners.

Listen if any key is pressed, while continuously returning to the game to enable movements.
 
ch = getch();

The code above pauses the system, so this will be an issue.

Possibly use threads:
http://www.cplusplus.com/reference/thread/thread/

and a counter to control the number of actions pacman can move before you make a press. You'd be surprise how fast the CPU is...

For better performance, use clock.

Reference threads:
http://www.cplusplus.com/reference/thread/thread/

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

Good luck.
@Duoas Thanks for your extensive reply, but Chriscpp's code stops the flicker. So can you suggest me what to wirte or which part do I have to change in order to keep the pacman moving? (assuming that Chriscpp's code stays where it is). And arrow keys are not necessary I mean it can be controlled with W, A, S, D keys, what would you say??
And also can I use a loop to move the pacman until a wall comes and it stops?? Actually I'm trying to make things as simple as possible. :) Waiting for your reply.
----------------------------------------------------------------------------------------------------------------
@Dput Thanks for the effort man but the code you posted looks more complex to me with all the buffers and stuff like that. Can you find a same approach as I have requested to Duoas?? Waiting for your reply too. :)
I've given you some pretty step-by-step instructions. Try them first. If you can't get them working, post back with what you've got.
Topic archived. No new replies allowed.