-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoxingGame.cpp
More file actions
767 lines (683 loc) · 22.5 KB
/
BoxingGame.cpp
File metadata and controls
767 lines (683 loc) · 22.5 KB
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
#include <iostream>
#include <fstream>
#include <conio.h>
#include <windows.h>
#include <time.h>
using namespace std;
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); // this line for adding color
char characterA[11][15]; // Array for Player 1
char characterB[11][15]; // Array for Player 2
int Ax = 20; // ply1 x-axis
int Ay = 14; // ply1 y-axis
int Bx = 60; // ply2 x-axis
int By = 14; // ply2 y-axis
int lifeA = 14; // ply1 life
int lifeB = 14; // ply2 life
// flags variables for on/off of punch
bool ourPunch[3]; // Punch flags for ply1
bool punchFlag[2]; // punch flags for play2
bool defendFlag; // defend flag
// ------------------- Functions Prototypes -----------------
void gotoxy(int x, int y); // for printing something on specific location
char menu(); // funcyion for main menu of game
void printBox(); // function for printing game box
void Instructions(); // function for printing instructions
void About(); // function for printing about of the game
string takeName(); // function for taking player name input
void initialize(); // function for intializing variables
void playGame(string name); // function for actual game play
void printCharacter(char character[11][15], int x, int y); // function for printing game charcter
void lifeTube(); // function for printing life tube of players
void changeColor(int life); // function for changing colors of life tube
void printMaze(); // function for printing game maze
void readCharctersFile(); // function for reading game characters from file
void removeGarbage(int x, int y); // function for removing garbage leaved by charcter
void removePunch(int x, int y, int idx); // function for removing punch
void removeDefender(); // function for removing defend pose
int GenRandomNu(); // function for generating random number
void enemyCharacter(); // function for moving punches of ply2
void removeEnemyGarbage(); // function for removing ply2 garbage
int Ply2HealthSystem(); // function for ply2 health
bool checkPly1Punch(int idx, int x); // function for checking punches of ply1
int Ply1HealthSystem(); // function for ply1 health
bool checkPly2Punch(int idx, int x); // function for checking punches of ply2
bool movePly2Random(); // function for moving ply2 randomly
bool WinnerLoserCheck(); // function for checking game winner
void winMsg(int x, int y); // function for printing win message
void loseMsg(int x, int y); // function for printing losing message
void border(int x, int y); // function for printing border around messages
// --------------------- Main Function -------------------------
main()
{
readCharctersFile(); // reading cahracters file
while (true) // main menu loop
{
system("cls");
char option; // variable for options
option = menu(); // main menu
if (option == '1') // for option 1
{
string name;
name = takeName(); // taking user name as input
playGame(name); // actual play game
cin.ignore(); // ignoring some extra pressed keys
}
else if (option == '2') // for option 2
{
Instructions(); // instructions
}
else if (option == '3') // for option 3
{
About(); // about
}
else if (option == '4') // for option 4
{
break; // exit
}
}
gotoxy(1, 27); // taking cursor below on screen
}
// -------------------- Function Definitions ----------------------------------
// function for displaying something on screen on specific location
void gotoxy(int x, int y)
{
COORD coordinates;
coordinates.X = x;
coordinates.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordinates);
}
// function for initailizing variables
void initialize()
{
ourPunch[0] = ourPunch[1] = ourPunch[2] = false; // ply1 punch flags
punchFlag[0] = punchFlag[1] = false; // ply2 punch flags
defendFlag = false; // ply2 defend flags
lifeA = lifeB = 14; // ply1 and ply2 lifes
}
// fuction for actual game play
void playGame(string name)
{
system("cls");
printMaze(); // printing game maze
SetConsoleTextAttribute(h, 9); // for blue color
gotoxy(23, 6);
cout << name; // printing player 1 name
printCharacter(characterA, Ax, Ay); // printing player1 character
SetConsoleTextAttribute(h, 4); // for red color
gotoxy(77, 6);
cout << "Computer"; // player 2 name
printCharacter(characterB, Bx, By); // printing player2 character
SetConsoleTextAttribute(h, 7); // for white color
int loop = 0; // varaible for loop count
int wait1 = 0, wait2 = 0, wait3 = 0; // varaibles for waiting
int count1 = 0, count2 = 0; // variables for counting
initialize(); // intializing variables
while (true) // game loop
{
if (wait1 == 0) // if wait equal to zero
{
wait1 = Ply2HealthSystem(); // checking ply 2 health
}
if (wait1 > 0)
{
wait1--;
}
if (wait2 == 0) // if wait equal to zero
{
wait2 = Ply1HealthSystem(); // checking ply 1 health
}
if (wait2 > 0)
{
wait2--;
}
lifeTube();
if (WinnerLoserCheck()) // checking for winner or loser
{
// game over
break; // breaking game loop
}
SetConsoleTextAttribute(h, 4); // for red color
count2++;
if (count2 == 5)
{
removeEnemyGarbage(); // removing ply 2 garbage
count2 = 0;
}
loop++;
if (loop == 5)
{
enemyCharacter(); // moving punches of ply2
loop = 0;
}
if (wait3 == 0) // if wait equal to zero
{
if (movePly2Random()) // moving ply 2
{
printCharacter(characterB, Bx, By); // printing player2 character
wait3 = 5;
}
}
if (wait3 > 0)
{
wait3--;
}
SetConsoleTextAttribute(h, 9); // for blue color
removePunch(12, 4, 0); // removing upper punch
removePunch(13, 6, 1); // removing lower punch
removeDefender(); // removing Defend pose
if (GetAsyncKeyState(VK_UP)) // for upper punch
{
gotoxy(Ax + 12, Ay + 4);
cout << "======8 "; // printing upper punch
ourPunch[0] = true; // turning on punch flag
}
else if (GetAsyncKeyState(VK_DOWN)) // for lower punch
{
gotoxy(Ax + 13, Ay + 6);
cout << "====8 "; // printing lower punch
ourPunch[1] = true; // turning on punch flag
}
else if (GetAsyncKeyState(VK_NUMPAD0)) // for defending
{
// printing defend pose
gotoxy(Ax + 12, Ay + 3);
cout << "|";
gotoxy(Ax + 12, Ay + 4);
cout << "|";
gotoxy(Ax + 13, Ay + 5);
cout << "|";
gotoxy(Ax + 13, Ay + 6);
cout << "|";
ourPunch[2] = true; // turning on punch flag
}
else if (GetAsyncKeyState(VK_LEFT)) // for moving left
{
if (Ax > 4) // limit from left side
{
Ax = Ax - 1;
gotoxy(Ax + 14, Ay + 6);
cout << " "; // removing garbage
printCharacter(characterA, Ax, Ay); // printing player1 character
}
}
else if (GetAsyncKeyState(VK_RIGHT)) // for moving right
{
if (Ax + 9 < Bx - 3)
{
Ax = Ax + 1;
removeGarbage(Ax, Ay); // removing garbage
printCharacter(characterA, Ax, Ay); // printing player1 character
}
}
SetConsoleTextAttribute(h, 7); // for white color
if (GetAsyncKeyState(VK_ESCAPE)) // for escaping from game
{
break; // breaking game loop
}
Sleep(50); // delaying game
gotoxy(1, 27);
}
}
// function for printing characters
void printCharacter(char character[11][15], int x, int y)
{
for (int i = 0; i < 11; i++) // for rows
{
gotoxy(x, y);
for (int c = 0; c < 15; c++) // for colunms
{
cout << character[i][c]; // printing
}
cout << endl;
y++; // increment in y-axis
}
}
// function for printing life tube of players
void lifeTube()
{
// SetConsoleTextAttribute(h, 10); // for green color
char special = 219; // special character
gotoxy(11, 7);
cout << " "; // replacing with spaces
changeColor(lifeA); // changing color
gotoxy(11, 7);
for (int i = 0; i < lifeA; i++) // ply1 life
{
cout << special;
}
gotoxy(65, 7);
cout << " "; // replacing with spaces
changeColor(lifeB); // changing color
gotoxy(65, 7);
for (int i = 0; i < lifeB; i++) // ply2 life
{
cout << special;
}
SetConsoleTextAttribute(h, 7); // for white color
}
// fuvtion for changing color of life tube
void changeColor(int life)
{
if (life > 10)
{
SetConsoleTextAttribute(h, 10); // for green color
}
else if (life > 7)
{
SetConsoleTextAttribute(h, 6); // yellow color
}
else if (life > 4)
{
SetConsoleTextAttribute(h, 5); // purple color
}
else if (life <= 4)
{
SetConsoleTextAttribute(h, 4); // red color
}
}
// function for printing game maze
void printMaze()
{
fstream box;
string line;
box.open("GameMaze.txt", ios::in); // opeinig file
while (!box.eof()) // reading until end
{
getline(box, line); // line by line
cout << line << endl; // printing maze
}
box.close(); // closing file
}
// function for reading characters file
void readCharctersFile()
{
fstream file;
string line;
file.open("character.txt", ios::in); // opeinig file of character A
for (int x = 0; x < 11; x++) // for rows
{
getline(file, line); // line by line
for (int y = 0; y < 15; y++) // for coloumns
{
characterA[x][y] = line[y]; // storing one by one character A
}
}
file.close(); // closing file of character A
fstream file2;
file2.open("characterB.txt", ios::in); // opeinig file of character B
for (int x = 0; x < 11; x++) // for rows
{
getline(file2, line); // line by line
for (int y = 0; y < 15; y++) // for coloumns
{
characterB[x][y] = line[y]; // storing one by one character B
}
}
file2.close(); // closing file of character B
}
// function removing garbage leaved by characters
void removeGarbage(int x, int y)
{
// replacing all with spaces
x = x - 1;
gotoxy(x, y + 4);
cout << " ";
gotoxy(x, y + 5);
cout << " ";
gotoxy(x, y + 6);
cout << " ";
gotoxy(x, y + 7);
cout << " ";
}
// function for removing punches
void removePunch(int x, int y, int idx)
{
if (ourPunch[idx] == true) // if punch flag is true
{
gotoxy(Ax + x, Ay + y);
cout << "8 "; // replacing
ourPunch[idx] = false; // making flag false
}
}
// function for removing defenf pose
void removeDefender()
{
if (ourPunch[2] == true) // if punch flag is true
{
// replacing
gotoxy(Ax + 12, Ay + 3);
cout << " ";
gotoxy(Ax + 12, Ay + 4);
cout << "8";
gotoxy(Ax + 13, Ay + 5);
cout << " ";
gotoxy(Ax + 13, Ay + 6);
cout << "8";
ourPunch[2] = false; // making flag false
}
}
// function for generating random number
int GenRandomNu()
{
return rand() % (3); // generating 3 numbers from 0 to 2
}
// function for moving ply2 punches
void enemyCharacter()
{
int num; // variable to take random number
num = GenRandomNu(); // random number
if (num == 0 && punchFlag[0] == false) // for upper punch
{
gotoxy(Bx - 3, By + 3);
cout << "8===="; // printing upper punch
punchFlag[0] = true; // making flag true
}
else if (num == 1 && punchFlag[1] == false) // for lower punch
{
gotoxy(Bx - 4, By + 5);
cout << "8===="; // printing lower punch
punchFlag[1] = true; // making flag true
}
else if (num == 2 && defendFlag == false) // for defending pose
{
// printing defend pose
gotoxy(Bx + 1, By + 2);
cout << "|";
gotoxy(Bx + 1, By + 3);
cout << "|";
gotoxy(Bx, By + 4);
cout << "|";
gotoxy(Bx, By + 5);
cout << "|";
defendFlag = true; // making flag true
}
}
// function for removing ply2 garbage
void removeEnemyGarbage()
{
if (punchFlag[0] == true) // for upper punch
{
gotoxy(Bx - 4, By + 3);
cout << " 8"; // replacing
punchFlag[0] = false; // making flag false
}
if (punchFlag[1] == true) // for lower punch
{
gotoxy(Bx - 5, By + 5);
cout << " 8"; // replacing
punchFlag[1] = false; // making flag false
}
if (defendFlag == true) // for defending pose
{
// replacing
gotoxy(Bx + 1, By + 2);
cout << " ";
gotoxy(Bx + 1, By + 3);
cout << "8";
gotoxy(Bx, By + 4);
cout << " ";
gotoxy(Bx, By + 5);
cout << "8";
defendFlag = false; // making flag false
}
}
// function for checking player 2 health
int Ply2HealthSystem()
{
int wait = 0; // varialbe for wait
if (checkPly1Punch(0, 18)) // ply1 upper punch
{
lifeB--; // decreasing life of ply 2
wait = 10;
}
else if (checkPly1Punch(1, 17)) // ply1 lower punch
{
lifeB--; // decreasing life of ply 2
wait = 10;
}
return wait;
}
// function for checking punches of player 1
bool checkPly1Punch(int idx, int x)
{
if (ourPunch[idx] == true && defendFlag == false) // if punch flag is true and defenf flag is false
{
if (Ax + x >= Bx + 5) // if axis matches
{
return true;
}
}
return false;
}
// function for checking player 2 health
int Ply1HealthSystem()
{
int wait = 0; // varialbe for wait
if (checkPly2Punch(0, 3)) // ply2 upper punch
{
lifeA--; // decreasing life of ply 1
wait = 5;
}
else if (checkPly2Punch(1, 4)) // ply2 lower punch
{
lifeA--; // decreasing life of ply 1
wait = 5;
}
return wait;
}
// function for checking punches of player 1
bool checkPly2Punch(int idx, int x)
{
if (punchFlag[idx] == true && ourPunch[2] == false) // if punch flag is true and defenf flag is false
{
if (Bx - x <= Ax + 8) // if axis matches
{
return true;
}
}
return false;
}
// function for moving player2 randomly
bool movePly2Random()
{
if (Bx - Ax + 12 > 15) // if ply1 x-axis is to much away than move toward left
{
if (Bx > Ax + 12) // limit for left moving
{
Bx--;
removeGarbage(Bx + 15, By); // removing garbages
return true;
}
}
int num;
num = rand() % (2); // generating 2 numbers from 0 or 1
if (num == 0) // if num equal to 0
{
if (Bx > Ax + 12) // limit for left moving
{
Bx--;
removeGarbage(Bx + 15, By); // removing garbages
return true;
}
}
else if (num == 1) // if num equal to 1
{
if (Bx < 80) // limit for right moving
{
Bx++;
removeEnemyGarbage(); // removing garbages
return true;
}
}
return false;
}
// function for displaying winning message
void winMsg(int x, int y)
{
SetConsoleTextAttribute(h, 10); // for green color
border(23, 13); // calling border
gotoxy(x, y);
cout << " __ __ " << endl;
gotoxy(x, y + 1);
cout << " \\ / / \\ | | | | / \\ |\\ | " << endl;
gotoxy(x, y + 2);
cout << " \\ / | | | | | | | | | \\ | " << endl;
gotoxy(x, y + 3);
cout << " / | | | | | /\\ | | | | \\ | " << endl;
gotoxy(x, y + 4);
cout << " / \\__/ \\__/ |/ \\| \\__/ | \\| " << endl;
SetConsoleTextAttribute(h, 7); // for white color
}
// function for displaying losing message
void loseMsg(int x, int y)
{
SetConsoleTextAttribute(h, 4); // for red color
border(23, 13); // calling border
gotoxy(x, y);
cout << " __ __ __ ___ " << endl;
gotoxy(x, y + 1);
cout << " \\ / / \\ | | | / \\ / \\ | " << endl;
gotoxy(x, y + 2);
cout << " \\ / | | | | | | | \\ |___ " << endl;
gotoxy(x, y + 3);
cout << " / | | | | | | | \\ | " << endl;
gotoxy(x, y + 4);
cout << " / \\__/ \\__/ |___ \\__/ \\__/ |___ " << endl;
SetConsoleTextAttribute(h, 7); // for white color
}
// function for displaying border around message
void border(int x, int y)
{
gotoxy(x, y);
cout << " ============================================= ";
gotoxy(x, y + 1);
cout << "| | ";
gotoxy(x, y + 2);
cout << "| | ";
gotoxy(x, y + 3);
cout << "| | ";
gotoxy(x, y + 4);
cout << "| | ";
gotoxy(x, y + 5);
cout << "| | ";
gotoxy(x, y + 6);
cout << " ============================================= ";
}
// functionf for checking winner or loser
bool WinnerLoserCheck()
{
if (lifeA == 0) // if ply 1 life become zero
{
loseMsg(25, 14); // than print lose msg
return true;
}
else if (lifeB == 0) // if ply 2 life become zero
{
winMsg(25, 14); // than print win msg
return true;
}
return false;
}
// function for main menu
char menu()
{
fstream file;
string line;
file.open("MenuBox.txt", ios::in); // opening file
while (!file.eof()) // reading until enf of file
{
getline(file, line); // line by line
cout << line << endl; // displaying main menu box
}
file.close(); // closing file
char option; // variable for option
gotoxy(30, 21);
cout << " YOUR OPTION .... : ";
option = getche(); // taking input
return option;
}
// function for taking name input from user
string takeName()
{
gotoxy(29, 9);
cout << "*** PLAY GAME ***";
gotoxy(29, 12);
cout << " ";
gotoxy(29, 15);
cout << " ";
gotoxy(29, 16);
cout << " ";
gotoxy(29, 17);
cout << " ";
gotoxy(29, 18);
cout << " ";
gotoxy(29, 21);
cout << " ";
string name; // varible for name
gotoxy(2, 16);
cout << " Enter Your Name ... :";
getline(cin, name); // taking name input
gotoxy(2, 18);
cout << " Press any key to continue.....";
getch();
return name;
}
// function for printing menu box
void printBox()
{
fstream file;
string line;
file.open("Box.txt", ios::in); // opening file
while (!file.eof())
{
getline(file, line); // line by line
cout << line << endl; // displaying menu box
}
file.close(); // closing file
}
// function for displaying instructions
void Instructions()
{
system("cls");
printBox();
gotoxy(27, 9);
cout << "*** INSTRUCTIONS ***";
SetConsoleTextAttribute(h, 1); // for blue color
gotoxy(27, 12);
cout << "<< How To Play Game >>";
SetConsoleTextAttribute(h, 4); // for red color
gotoxy(32, 14);
cout << "[ CONTROLS ]"; // controls
SetConsoleTextAttribute(h, 10); // for green color
gotoxy(25, 16);
cout << " UPPER PUNCH >> Up Arrow Key";
gotoxy(25, 17);
cout << " Lower PUNCH >> Down Arrow Key";
gotoxy(25, 18);
cout << " Defend >> Zero( 0 ) Key";
gotoxy(25, 19);
cout << " LEFT MOVE >> Left Arrow Key";
gotoxy(25, 20);
cout << " RIGHT MOVE >> Right Arrow Key";
SetConsoleTextAttribute(h, 7); // for white color
gotoxy(25, 22);
cout << " Press any key to continue.....";
getch();
}
// function for displaying about of the game
void About()
{
system("cls");
printBox();
gotoxy(27, 9);
cout << "*** ABOUT ***";
SetConsoleTextAttribute(h, 10); // for green color
gotoxy(25, 14);
cout << "VERSION >> 1.0 "; // version
gotoxy(25, 15);
cout << "DEVELOPER >> Hamza Rasheed (CS-26)";
SetConsoleTextAttribute(h, 7); // for white color
gotoxy(25, 22);
cout << " Press any key to continue.....";
getch();
}