-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacMan.cpp
682 lines (655 loc) · 21 KB
/
PacMan.cpp
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
//--------------Header Files---------//
#include <iostream>
#include <fstream>
#include <conio.h>
#include <windows.h>
#include <ctime>
#include <time.h>
using namespace std;
//-----------------Function Prototypes-------------------//
int menu();
void printMaze();
void movePacmanLeft();
void movePacmanRight();
void movePacmanUp();
void movePacmanDown();
void playGame();
void gotoxy(int x, int y);
void scoreboard();
void winner();
bool gameover();
void readMaze();
void moveGhost1();
void moveGhost2();
void moveGhost3();
int ghostDirection();
int moveGhost4();
void gameOverMessage();
//----------------Variable Declaration-----------------//
bool playingGame = true;
//---------Coordinates of Position of Pac-Man----------//
int PacmanX = 9;
int PacmanY = 31;
//---------Coordinates of Position of Ghost------------//
int g1x = 16; // The ghost which is moving Verically
int g1y = 31;
int flag1 = 0;
int g2x = 1; // The ghost which is moving Verically
int g2y = 29;
int flag2 = 0;
int g3x = 1; // The ghost which is moving Verically
int g3y = 60;
int g4x = 5; // The intelligent ghost moving towards PacMan
int g4y = 5;
int score = 0; // for maintaining score record
int lives = 3; // for maintaining lives count
// we have done this so that ghost might eat food dots, so we have to store them in a variable before
char previousItem = ' ';
string gD1 = "up";
string gD2 = "right";
// fixed number of Rows and Columns in Maze
const int rows = 24, cols = 71;
//-----------------Array of Maze------------------//
char maze[rows][cols] = {{"#####################################################################"},
{"||.. ..................................................... ...... ||"},
{"||.. %%%%%%%%%%%%%%%% G... %%%%%%%%%%%%%% |%|.. %%%% ||"},
{"||.. |%| |%| |%|... |%| |%| |%|.. |%| ||"},
{"||.. |%| |%| |%|... |%| |%| |%|.. |%| ||"},
{"||.. %%%%%%%% . . |%|... %%%%%%%%%%%%%% .. %%%%. ||"},
{"||.. |%| . . |%|... ............... |%| .. . ||"},
{"||.. %%%%%%%%%%. . |%|... %%%%%%%%%%%% |%| .. %%%%. ||"},
{"||.. |%|. |%|...... |%| .. |%|. ||"},
{"||.. ......... |%|. P |%|......|%| .. |%|. ||"},
{"||..|%| |%|%%%|%|.|%|. |%| ......|%| ..|%| |%|. ||"},
{"||..|%| |%| |%|.. %%%%%%%%%%%%% ......|%| G.|%|. ||"},
{"||..|%| |%| |%|.. ...|%| %%%%%% . |%|. ||"},
{"||..|%| . ...|%| |%| ..|%|. ||"},
{"||..|%| %%%%%%%%%%%%%% ...|%|%%%%%%%%%% |%| ..|%|%%%%% ||"},
{"||............................................... |%| .......... ||"},
{"|| ............................................ G ....... ||"},
{"||..|%| |%| |%|.. %%%%%%%%%%%%% ......|%| |%| ..|%|. ||"},
{"||..|%| |%| |%|.. ...|%| %%%%%% |%| ..|%|. ||"},
{"||..|%| . G ...|%| |%| ..|%|. ||"},
{"||..|%| %%%%%%%%%%%%%% ...|%|%%%%%%%%%% |%| ..|%|%%%%% ||"},
{"||............................................... |%| .......... ||"},
{"|| ............................................. ....... ||"},
{"##################################################################### "}};
//------------------Color Change----------//
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
//------------------Main Body----------------------//
main()
{
system("cls");
int option;
readMaze();
option = menu();
while (option < 3)
{
if (option == 1)
{
// SetConsoleTextAttribute(hConsole, 15);
// SetConsoleTextAttribute(hConsole, 13);
SetConsoleTextAttribute(hConsole, 14);
while (playingGame)
{
lives = 3;
score = 0;
playGame();
}
}
else if (option == 2)
{
system("cls");
cout << endl;
cout << endl;
cout << endl;
SetConsoleTextAttribute(hConsole, 13);
cout << "\t\t\t\t\t ----------------------------" << endl;
cout << "\t\t\t\t\t | Game Description |" << endl;
cout << "\t\t\t\t\t ----------------------------" << endl;
cout << endl;
cout << endl;
SetConsoleTextAttribute(hConsole, 12);
cout << "\t\t How to Play : ";
SetConsoleTextAttribute(hConsole, 11);
cout << "Pac Man is a human-computer game, so only 1 player can play at a time " << endl;
cout << "\t\t\t The player controls Pac-Man, who must eat all the dots inside an" << endl;
cout << "\t\t\t enclosed maze while avoiding four colored ghosts. Eating dots called " << endl;
cout << "\t\t\t (Food Pellets) causes Pac-Man to earn bonus points. Pac-Man can only" << endl;
cout << "\t\t\t move inside the maze, cannot go outside the walls of maze. Pac-Man has" << endl;
cout << "\t\t\t to avoid ghosts. Colliding with the ghosts result in -1 from lives." << endl;
cout << "\t\t\t There are total three lives of Pac-Man. If Pac-Man collides with the" << endl;
cout << "\t\t\t ghosts more than three times, then the game will be over. If score " << endl;
cout << "\t\t\t becomes above 500, game stops and player wins" << endl;
cout << endl;
SetConsoleTextAttribute(hConsole, 12);
cout << "\t\tPlayers Required : ";
SetConsoleTextAttribute(hConsole, 11);
cout << " 1 " << endl;
cout << endl;
SetConsoleTextAttribute(hConsole, 12);
cout << "\t\tSkills Required :";
SetConsoleTextAttribute(hConsole, 11);
cout << " Strategy, Tactics, Observation." << endl;
cout << endl;
SetConsoleTextAttribute(hConsole, 12);
cout << endl;
}
// resetting if user select option 1, to play again
playingGame = true;
score = 0;
lives = 3;
//PacmanX = 10;
//PacmanY = 32;
//readMaze();
cout << endl;
SetConsoleTextAttribute(hConsole, 14);
cout << "\tPress any key to continue_";
getch();
system("cls");
option = menu();
SetConsoleTextAttribute(hConsole, 15); // set back to black background and white text coz 15 = white
}
}
//-----------------Function of Main Menu------------------//
int menu()
{
cout << endl;
cout << endl;
SetConsoleTextAttribute(hConsole, 12);
cout << "\t\t\t\t\t\t*********************" << endl;
cout << "\t\t\t\t\t\t* Pac-Man *" << endl;
cout << "\t\t\t\t\t\t*********************" << endl;
cout << endl;
cout << endl;
SetConsoleTextAttribute(hConsole, 3);
cout << " -------------------------------" << endl;
cout << " | Select one option from menu |" << endl;
cout << " -------------------------------" << endl;
cout << endl;
SetConsoleTextAttribute(hConsole, 11);
cout << "\t1. Play" << endl;
cout << "\t2. Game Description" << endl;
cout << "\t3. Exit" << endl;
cout << endl;
SetConsoleTextAttribute(hConsole, 14);
cout << " Your option_";
int option;
cin >> option;
return option;
}
//-----------------Function for playing Game------------------//
void playGame()
{
system("cls");
printMaze(); // printing maze on screen
gotoxy(PacmanY, PacmanX); // printing PacMan on a specific location on screen
cout << 'P';
while (playingGame)
{
Sleep(100); // delay in game is mandatory, otherwise screen will flicker and it will not run smoothly
// Functions to move our 4 ghosts
moveGhost1();
moveGhost2();
moveGhost3();
moveGhost4();
scoreboard(); // function to display sScoreBoard
if (gameover())
{
playingGame = false;
}
if (GetAsyncKeyState(VK_LEFT)) // to move PaCman left
{
movePacmanLeft();
}
if (GetAsyncKeyState(VK_RIGHT)) // to move PaCman right
{
movePacmanRight();
}
if (GetAsyncKeyState(VK_UP)) // to move PaCman up
{
movePacmanUp();
}
if (GetAsyncKeyState(VK_DOWN)) // to move PaCman down
{
movePacmanDown();
}
if (GetAsyncKeyState(VK_ESCAPE)) // to exit from game before game over
{
playingGame = false;
}
if (score == 100) // We also have set a score goal to win the game
{
winner();
playingGame = false;
}
}
}
//-----------------Function to print maze on screen------------------//
void printMaze()
{
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
cout << maze[row][col];
}
cout << endl;
}
}
//-----------------Function to Pac-Man Left------------------//
void movePacmanLeft()
{
if (maze[PacmanX][PacmanY - 1] == ' ' || maze[PacmanX][PacmanY - 1] == '.')
{
maze[PacmanX][PacmanY] = ' ';
gotoxy(PacmanY, PacmanX);
cout << ' ';
PacmanY--;
SetConsoleTextAttribute(hConsole, 11);
gotoxy(PacmanY, PacmanX);
cout << 'P';
SetConsoleTextAttribute(hConsole, 15);
if (maze[PacmanX][PacmanY - 1] == '.')
{
score++;
}
}
}
//-----------------Function to Pac-Man Right------------------//
void movePacmanRight()
{
if (maze[PacmanX][PacmanY + 1] == ' ' || maze[PacmanX][PacmanY + 1] == '.')
{
if (maze[PacmanX][PacmanY + 1] == '.')
{
score++;
}
maze[PacmanX][PacmanY] = ' ';
gotoxy(PacmanY, PacmanX);
cout << ' ';
PacmanY++;
SetConsoleTextAttribute(hConsole, 11);
gotoxy(PacmanY, PacmanX);
cout << 'P';
SetConsoleTextAttribute(hConsole, 15);
}
}
//-----------------Function to Pac-Man Up------------------//
void movePacmanUp()
{
if (maze[PacmanX - 1][PacmanY] == ' ' || maze[PacmanX - 1][PacmanY] == '.')
{
if (maze[PacmanX - 1][PacmanY] == '.')
{
score++;
}
maze[PacmanX][PacmanY] = ' ';
gotoxy(PacmanY, PacmanX);
cout << ' ';
PacmanX--;
SetConsoleTextAttribute(hConsole, 11);
gotoxy(PacmanY, PacmanX);
cout << 'P';
SetConsoleTextAttribute(hConsole, 15);
}
}
//-----------------Function to Pac-Man Down------------------//
void movePacmanDown()
{
if (maze[PacmanX + 1][PacmanY] == ' ' || maze[PacmanX + 1][PacmanY] == '.')
{
if (maze[PacmanX + 1][PacmanY] == '.')
{
score++;
}
maze[PacmanX][PacmanY] = ' ';
gotoxy(PacmanY, PacmanX);
cout << ' ';
PacmanX++;
SetConsoleTextAttribute(hConsole, 11);
gotoxy(PacmanY, PacmanX);
cout << 'P';
SetConsoleTextAttribute(hConsole, 15);
}
}
//-----------------Function to display on a specific location------------------//
void gotoxy(int x, int y)
{
COORD coordinates;
coordinates.X = x;
coordinates.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordinates);
}
//-----------------Function to display ScoreBoard------------------//
void scoreboard()
{
gotoxy(90, 10);
cout << "Score : " << score << endl;
gotoxy(90, 11);
cout << "Lives : " << lives << endl;
}
//-----------------Function to display Winner------------------//
void winner()
{
{
system("cls");
cout << endl;
cout << endl;
SetConsoleTextAttribute(hConsole, 12);
cout << "\t\t\t\t\t\t*********************" << endl;
cout << "\t\t\t\t\t\t* Pac-Man *" << endl;
cout << "\t\t\t\t\t\t*********************" << endl;
cout << endl;
cout << endl;
SetConsoleTextAttribute(hConsole, 13);
cout << "\t\t\t\t\t------------------------------------" << endl;
cout << "\t\t\t\t\t------------------------------------" << endl;
cout << "\t\t\t\t\t| WINNER |" << endl;
cout << "\t\t\t\t\t------------------------------------" << endl;
cout << "\t\t\t\t\t------------------------------------" << endl;
SetConsoleTextAttribute(hConsole, 15);
//cout << "\n\n Press any key to continue_";
//getch();
cout << "\n\n Enter any key to continue_";
string option;
cin >> option;
}
}
//-----------------Function to read Maze from txt file------------------//
void readMaze()
{
fstream file;
string line;
file.open("Maze.txt", ios::in);
for (int x = 0; x < 24; x++)
{
getline(file, line);
for (int y = 0; y < 71; y++)
{
maze[x][y] = line[y];
}
}
file.close();
}
//-----------------Function if GameOver------------------//
bool gameover()
{
if (PacmanX == g1x && PacmanY == g1y)
{
lives--;
PacmanX = 9;
PacmanY = 31;
}
else if (PacmanX == g2x && PacmanY == g2y)
{
lives--;
PacmanX = 9;
PacmanY = 31;
}
else if (PacmanX == g3x && PacmanY == g3y)
{
lives--;
PacmanX = 9;
PacmanY = 31;
}
else if (PacmanX == g4x && PacmanY == g4y)
{
lives--;
PacmanX = 9;
PacmanY = 31;
}
SetConsoleTextAttribute(hConsole, 11);
gotoxy(PacmanY, PacmanX);
cout << 'P';
SetConsoleTextAttribute(hConsole, 15);
if (lives == 0)
{
gameOverMessage();
return true;
}
return false;
}
//-----------------Function to move Ghost1 (Horizontal Moving Ghost)------------------//
void moveGhost1()
{
char pre;
// Move Ghost Left
if ((maze[g1x][g1y - 1] == ' ' || maze[g1x][g1y - 1] == '.') && flag1 == 1)
{
pre = maze[g1x][g1y];
maze[g1x][g1y] = pre;
gotoxy(g1y, g1x);
cout << pre;
g1y--;
SetConsoleTextAttribute(hConsole, 13);
gotoxy(g1y, g1x);
cout << 'G';
SetConsoleTextAttribute(hConsole, 15);
}
else
{
flag1 = 0;
}
// Move Ghost Right
if ((maze[g1x][g1y + 1] == ' ' || maze[g1x][g1y + 1] == '.') && flag1 == 0)
{
pre = maze[g1x][g1y];
maze[g1x][g1y] = pre;
gotoxy(g1y, g1x);
cout << pre;
g1y++;
SetConsoleTextAttribute(hConsole, 13);
gotoxy(g1y, g1x);
cout << 'G';
SetConsoleTextAttribute(hConsole, 15);
}
else
{
flag1 = 1;
}
}
//-----------------Function to move Ghost2 (Vertical Moving Ghost)------------------//
void moveGhost2()
{
char pre;
// Move Ghost Up
if ((maze[g2x - 1][g2y] == ' ' || maze[g2x - 1][g2y] == '.') && flag2 == 1)
{
pre = maze[g2x][g2y];
maze[g2x][g2y] = pre;
gotoxy(g2y, g2x);
cout << pre;
g2x--;
SetConsoleTextAttribute(hConsole, 13);
gotoxy(g2y, g2x);
cout << 'G';
SetConsoleTextAttribute(hConsole, 15);
}
else
{
flag2 = 0;
}
// Move Ghost Down
if ((maze[g2x + 1][g2y] == ' ' || maze[g2x + 1][g2y] == '.') && flag2 == 0)
{
pre = maze[g2x][g2y];
maze[g2x][g2y] = pre;
gotoxy(g2y, g2x);
cout << pre;
g2x++;
SetConsoleTextAttribute(hConsole, 13);
gotoxy(g2y, g2x);
cout << 'G';
SetConsoleTextAttribute(hConsole, 15);
}
else
{
flag2 = 1;
}
}
//-----------------Function to randomly decide ghost location-----------------//
int ghostDirection()
{
srand(time(0));
int result = 1 + (rand() % 4);
return result;
}
//-----------------Function to move Ghost3 (Randomly Moving Ghost)------------------//
void moveGhost3()
{
int n = ghostDirection();
char pre;
if (n == 1) // Move Ghost Right
{
if (maze[g3x][g3y + 1] == ' ' || maze[g3x][g3y + 1] == '.')
{
pre = maze[g3x][g3y];
gotoxy(g3y, g3x);
cout << pre;
g3y++;
SetConsoleTextAttribute(hConsole, 13);
gotoxy(g3y, g3x);
cout << 'G';
SetConsoleTextAttribute(hConsole, 15);
}
}
else if (n == 2) // Move Ghost Left
{
if (maze[g3x][g3y - 1] == ' ' || maze[g3x][g3y - 1] == '.')
{
pre = maze[g3x][g3y];
gotoxy(g3y, g3x);
cout << pre;
g3y--;
SetConsoleTextAttribute(hConsole, 13);
gotoxy(g3y, g3x);
cout << 'G';
SetConsoleTextAttribute(hConsole, 15);
}
}
else if (n == 3) // Move Ghost Up
{
if (maze[g3x - 1][g3y] == ' ' || maze[g3x - 1][g3y] == '.')
{
pre = maze[g3x][g3y];
gotoxy(g3y, g3x);
cout << pre;
g3x--;
SetConsoleTextAttribute(hConsole, 13);
gotoxy(g3y, g3x);
cout << 'G';
SetConsoleTextAttribute(hConsole, 15);
}
}
else if (n == 4) // Move Ghost Down
{
if (maze[g3x + 1][g3y] == ' ' || maze[g3x + 1][g3y] == '.')
{
pre = maze[g3x][g3y];
gotoxy(g3y, g3x);
cout << pre;
g3x++;
SetConsoleTextAttribute(hConsole, 13);
gotoxy(g3y, g3x);
cout << 'G';
SetConsoleTextAttribute(hConsole, 15);
}
}
}
//-----------------Function to move Ghost4 (behind the PacMan)------------------//
int moveGhost4()
{
char pre;
Sleep(50);
// if PacmanX greater than ghostX, then move ghost right
if (PacmanX > g4x)
{
if (maze[g4x + 1][g4y] == ' ' || maze[g4x + 1][g4y] == '.')
{
pre = maze[g4x][g4y];
gotoxy(g4y, g4x);
cout << pre;
g4x++;
SetConsoleTextAttribute(hConsole, 13);
gotoxy(g4y, g4x);
cout << 'G';
SetConsoleTextAttribute(hConsole, 15);
return 1;
}
}
// if PacmanX less than ghostX, then move ghost left
if (PacmanX < g4x)
{
if (maze[g4x - 1][g4y] == ' ' || maze[g4x - 1][g4y] == '.')
{
pre = maze[g4x][g4y];
gotoxy(g4y, g4x);
cout << pre;
g4x--;
SetConsoleTextAttribute(hConsole, 13);
gotoxy(g4y, g4x);
cout << 'G';
SetConsoleTextAttribute(hConsole, 15);
return 1;
}
}
// if PacmanY greater than ghostY, then move ghost Down
if (PacmanY > g4y)
{
if (maze[g4x][g4y + 1] == ' ' || maze[g4x][g4y + 1] == '.')
{
pre = maze[g4x][g4y];
gotoxy(g4y, g4x);
cout << pre;
g4y++;
SetConsoleTextAttribute(hConsole, 13);
gotoxy(g4y, g4x);
cout << 'G';
SetConsoleTextAttribute(hConsole, 15);
return 1;
}
}
// if PacmanY less than ghostY, then move ghost up
if (PacmanY < g4y)
{
if (maze[g4x][g4y - 1] == ' ' || maze[g4x][g4y - 1] == '.')
{
pre = maze[g4x][g4y];
gotoxy(g4y, g4x);
cout << pre;
g4y--;
SetConsoleTextAttribute(hConsole, 13);
gotoxy(g4y, g4x);
cout << 'G';
SetConsoleTextAttribute(hConsole, 15);
return 1;
}
}
}
//-----------------Function to print GameOver------------------//
void gameOverMessage()
{
system("cls");
cout << endl;
cout << endl;
SetConsoleTextAttribute(hConsole, 12);
cout << "\t\t\t\t\t\t*********************" << endl;
cout << "\t\t\t\t\t\t* Pac-Man *" << endl;
cout << "\t\t\t\t\t\t*********************" << endl;
cout << endl;
cout << endl;
cout << "\n\n\n";
SetConsoleTextAttribute(hConsole, 13);
cout << "\t\t\t\t\t-----------------------------------" << endl;
cout << "\t\t\t\t\t-----------------------------------" << endl;
cout << "\t\t\t\t\t| Game Over |" << endl;
cout << "\t\t\t\t\t-----------------------------------" << endl;
cout << "\t\t\t\t\t-----------------------------------" << endl;
SetConsoleTextAttribute(hConsole, 15);
//cout << "\n\n Press any key to continue_";
//getch();
cout << "\n\n Enter any key to continue_";
string option;
cin >> option;
}