-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.cpp
605 lines (501 loc) · 14.5 KB
/
pong.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
/* header files used*/
#include <iostream>
#include <time.h>
#include <conio.h>
#include <cstdlib>
#include <fstream>
#include <string>
#include<fcntl.h>
#include<io.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include<mmsystem.h>
void startGame();//for starting the game
void mainMenu();// for taking input from user
void choice();//for accepting input for the players
void setConsoleColour(unsigned short colour); //to set the color for the game
void highScore();//to read the highscore from the file
/*functions used for stoping the flickering behaviour of the console*/
void ClearScreen();
void hidecursor();
void gotoXY(int x,int y);
using namespace std;
unsigned char col [] = {FOREGROUND_INTENSITY, FOREGROUND_GREEN, FOREGROUND_RED, FOREGROUND_BLUE, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE};
string name[2];
int color[2];
enum eDir { STOP = 0, LEFT = 1, UPLEFT = 2, DOWNLEFT = 3, RIGHT = 4, UPRIGHT = 5, DOWNRIGHT = 6};
int main()/*the main function*/
{
startGame();
return 0;
}
struct cBall// to get , to modify and use direction of the ball
{
int x, y;
int originalX, originalY;
eDir direction;
cBall(int posX, int posY)
{
originalX = posX;
originalY = posY;
x = posX; y = posY;
direction = STOP;
}
void Reset()// to reset the direction of the ball to it's original place
{
x = originalX; y = originalY;
direction = STOP;
}
void changeDirection(eDir d)///to change the direction of the ball
{
direction = d;
}
void randomDirection()//for getting random direction of the ball
{
direction = (eDir)((rand() % 6) + 1);
}
inline int getX() { return x; }//to get direction of ball's x component
inline int getY() { return y; }//to get direction of ball's y component
inline eDir getDirection() { return direction; }//to get direction
void Move()//for the movement of the ball
{
switch (direction)
{
case STOP:
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UPLEFT:
x--; y--;
break;
case DOWNLEFT:
x--; y++;
break;
case UPRIGHT:
x++; y--;
break;
case DOWNRIGHT:
x++; y++;
break;
default:
break;
}
}
};
struct cPaddle/// to get, to modify and use direction of the paddle
{
int x, y;
int originalX, originalY;
cPaddle()
{
x = y = 0;
}
cPaddle(int posX, int posY) : cPaddle()
{
originalX = posX;
originalY = posY;
x = posX;
y = posY;
}
inline void Reset() { x = originalX; y = originalY; }
inline int getX() { return x; }
inline int getY() { return y; }
inline void moveUp() { y--; }
inline void moveDown() { y++; }
};
struct cGameManger// the whole game engine
{
int width, height;
int score1, score2;
char up1, down1, up2, down2;
bool quit;
bool endofgame = false;
cBall *ball;
cPaddle *player1;
cPaddle *player2;
cGameManger(int w, int h)
{
srand(time(NULL));
quit = false;
up1 = 'w'; up2 = 'i';
down1 = 's'; down2 = 'k';
score1 = score2 = 0;
width = w; height = h;
ball = new cBall(w / 2, h / 2);
player1 = new cPaddle(2, h / 2 - 3);
player2 = new cPaddle(w - 2, h / 2 - 3);
}
~cGameManger()
{
delete ball, player1, player2;
}
void ScoreUp(cPaddle * player)
{
PlaySound((LPCSTR) "score.wav", NULL, SND_FILENAME | SND_ASYNC);
if (player == player1)
score1++;
else if (player == player2)
score2++;
ball->Reset();
player1->Reset();
player2->Reset();
}
void Draw() // draw the main body of the game
{
ClearScreen();
string pong[9] = {"#######################################################"\
,"#######################################################"\
,"#### #### #### #### #### ####"\
,"#### ##### #### #### #### # ### #### ##########"\
,"#### #### #### #### ## ## #### ### ####"\
,"#### ########### #### #### ### # #### #### ####"\
,"#### ########### #### #### #### ####"\
,"#######################################################"\
,"#######################################################"};
for (int x= 0; x< 9; x++)
{
gotoXY(25,x+1);
for (int t= 0; t< 55; t++)
if (pong[x][t] == '#')
pong[x][t] = '\xCE';
cout<<pong[x];
}
gotoXY(((55-width)/2) +25,12);
for (int i = 0; i < width + 1; i++)
cout << "\xB2";
cout << endl;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{gotoXY(((55-width)/2) +25 + j,13+i);
int ballx = ball->getX();
int bally = ball->getY();
int player1x = player1->getX();
int player2x = player2->getX();
int player1y = player1->getY();
int player2y = player2->getY();
if (j == 0)
cout << "\xB2";
if (!endofgame)
{
if (ballx == j && bally == i)
{
setConsoleColour(col[1] | col[2]);
_setmode(_fileno(stdout), 0x20000);
wprintf(L"\x263b"); //ball
_setmode(_fileno(stdout), 0X4000);
setConsoleColour(col[4]);
}
else if (player1x == j && (player1y <=i && player1y +3 >= i))
{
setConsoleColour(col[color[0]]);
cout<<"\xDB";//player1
setConsoleColour(col[4]);
}
else if (player2x == j &&(player2y <=i && player2y +3 >= i))
{
setConsoleColour(col[color[1]]);
cout<<"\xDB";//player1
setConsoleColour(col[4]);
} //player2
else
{
setConsoleColour(col[0] | col[3]);
cout<<"\xDB";//player1
setConsoleColour(col[4]);
}
}
else
{
PlaySound(NULL, 0, 0);
unsigned char r;
if(score1 > score2)
{
if ((j == 18 || j==17) && (i >3 && i <13))
r = col[color[0]];
else
r = col[0] | col[3];
}
else
{
if((i == 3 || i == 10|| i == 6) && (j>13 && j <24))
r = col[color[1]];
else if ((i > 3 && i < 7) && (j == 22 ||j == 23))
r = col[color[1]];
else if ((i > 6 && i < 11) && (j == 14 ||j == 15))
r = col[color[1]];
else
r = col[0] | col[3];
}
setConsoleColour(r);
cout<<"\xDB";
setConsoleColour(col[4]);
}
if (j == width - 1)
cout << "\xB2";
}
cout << endl;
}
gotoXY(((55-width)/2) +25,13+height);
for (int i = 0; i < width + 1; i++)
cout << "\xB2";
cout<<endl;
cout <<" "<< name[0]<<": " << score1 << endl<<" "<<name[1]<< " : " << score2 << endl<<endl;
cout<<name[0]<<" : use 'W'for up , 'S' for down"<<endl<<name[1]<<" : use 'I'for up , 'K' for down"<<endl;
if(endofgame)// things to do after the program ends
{
fstream highScore;
int high;
highScore.open("highScore.txt",ios::in);//read high score
if(highScore.is_open()){
string line;
getline(highScore,line);
high=stoi(line);
highScore.close();
}
if(high<score1 || high<score2)//to compare the high score in the file with the players score
{
if(score1>score2){
fstream highScore;
fstream highScoreN;
int high;
highScore.open("highScore.txt",ios::out);//write high score
highScoreN.open("highScoreN.txt",ios::out);//write the name
if(highScoreN.is_open()){
highScoreN<<name[0];
highScoreN.close();
}
if(highScore.is_open()){
highScore<<score1;
highScore.close();
}
}
if(score2>score1){
fstream highScore;
fstream highScoreN;
int high;
highScore.open("highScore.txt",ios::out);//write high score
highScoreN.open("highScoreN.txt",ios::out);//write the name
if(highScoreN.is_open()){
highScoreN<<name[1];
highScoreN.close();
}
if(highScore.is_open()){
highScore<<score2;
highScore.close();
}
}
Sleep(30000);
cout << "press any key to exit";
getch();
quit = true;
}}
}
void Input()
{
ball->Move();
int player1y = player1->getY();
int player2y = player2->getY();
bool bKey [5];
for (int k = 0; k < 5; k++)
bKey[k] = (0x8000 & GetAsyncKeyState((unsigned char)("WSIKQ"[k]))) != 0;
if (bKey[0] && player1y > 0)
player1->moveUp();
if (bKey[2] && player2y > 0)
player2->moveUp();
if (bKey[1] && (player1y + 4 < height))
player1->moveDown();
if (bKey[3] && player2y + 4 < height)
player2->moveDown();
if (ball->getDirection() == STOP)
ball->randomDirection();
if (bKey[4])
quit = true;
}
void Logic()
{
int ballx = ball->getX();
int bally = ball->getY();
int player1x = player1->getX();
int player2x = player2->getX();
int player1y = player1->getY();
int player2y = player2->getY();
//left paddle
for (int i = 0; i < 4; i++)
if (ballx == player1x + 1)
if (bally == player1y + i){
ball->changeDirection((eDir)((rand() % 3) + 4));
PlaySound((LPCSTR) "paddle.wav", NULL, SND_FILENAME | SND_ASYNC);
}
//right paddle
for (int i = 0; i < 4; i++)
if (ballx == player2x - 1)
if (bally == player2y + i){
ball->changeDirection((eDir)((rand() % 3) + 1));
PlaySound((LPCSTR) "paddle.wav", NULL, SND_FILENAME | SND_ASYNC);
}
//bottom wall
if (bally == height - 1){
ball->changeDirection(ball->getDirection() == DOWNRIGHT ? UPRIGHT : UPLEFT);
PlaySound((LPCSTR) "wall.wav", NULL, SND_FILENAME | SND_ASYNC);
}
//top wall
if (bally == 0){
ball->changeDirection(ball->getDirection() == UPRIGHT ? DOWNRIGHT : DOWNLEFT);
PlaySound((LPCSTR) "wall.wav", NULL, SND_FILENAME | SND_ASYNC);
}
//right wall
if (ballx == width - 1)
ScoreUp(player1);
//left wall
if (ballx == 0)
ScoreUp(player2);
if((score1 + score2) > 4)
{
endofgame = true;
}
}
void Run()
{
while (!quit)
{
Draw();
hidecursor();
Input();
Logic();
}
}
};
void startGame()// to start the game and print the welcome page
{
char Continue;
cout<<"\n\n\n\t\t\t\t -->PONG GAME PROJECT!!<-- "<<endl;
cout<<"\n\t\t\t\t\tMADE BY: GROUP ONE "<<endl;
cout<<"\n\t\t\t\t\t SECTION: ONE "<<endl;
cout<<"\n\n\n\t\t\t\tGROUP MEMBERS "<<"ID No."<<endl;
cout<<"\t\t\t\t-------------------------------\n";
cout<<"\t\t\t\tABEL HAILEMICAHEL "<<"UGR/2301/12"<<endl;
cout<<"\t\t\t\tBLEN ASSEFA "<<"UGR/9640/12"<<endl;
cout<<"\t\t\t\tELENA GIRMA "<<"UGR/6368/12"<<endl;
cout<<"\t\t\t\tELENI ZEWDU "<<"UGR/7764/12"<<endl;
cout<<"\nplease enter any character to continue: ";
getch();
system("cls");
mainMenu();
}
void mainMenu()//to take the choice for diffrent functionalities
{
int mainChoice;
cout<<"\n\n\n\t\t\t\t------------------------------------------------------- "<<endl;
cout<<"\t\t\t\t------------------------------------------------------- "<<endl;
cout<<"\t\t\t\t----------------------CONSOLE PONG GAME---------------- "<<endl;
cout<<"\t\t\t\t------------------------------------------------------- "<<endl;
cout<<"\t\t\t\t------------------------------------------------------- "<<endl;
cout<<"\n\n\n\t\t\t\tMAIN MENU"<<endl;
cout<<"\t\t\t\t1.START NEW GAME"<<endl;
cout<<"\t\t\t\t2.HIGH SCORE"<<endl;
cout<<"\t\t\t\t3.EXIT THE PROGRAM"<<endl;
cout<<"\n\n\nplease enter your choice to continue: ";
cin>>mainChoice;
switch(mainChoice){
case 1:{
choice();
cGameManger c(35, 17);
c.Run();
break;}
case 2:
highScore();
break;
case 3:
break;
default:
cout<< "you entered incorrect choice closing..."<<endl;
Sleep(3000);
break;}
}
void highScore()//to read the highscore from the file
{
system("cls");
fstream highScore;
fstream highScoreN;
int high;
highScore.open("highScore.txt",ios::in);//open to read high score
highScoreN.open("highScoreN.txt",ios::in);//open to read the name
cout<<"\n\n\n\t\t\t\t------------------------------------------------------- "<<endl;
cout<<"\t\t\t\t------------------------------------------------------- "<<endl;
cout<<"\t\t\t\t--------------HIGH SCORE OF THE GAME---------------- "<<endl;
cout<<"\t\t\t\t------------------------------------------------------- "<<endl;
cout<<"\t\t\t\t------------------------------------------------------- "<<endl;
cout<<"\n\n\n NAME SCORE "<<endl;
if(highScoreN.is_open()){
string line;
while(getline(highScoreN,line)){
cout<<" "<<line<<" ";
}
highScoreN.close();
}
if(highScore.is_open()){
string line;
while(getline(highScore,line)){
cout<<line<<endl;
}
highScore.close();
}
cout<<"\n\n\n\t\t\t please enter any key to exit the program"<<endl;
cin>>high;
system("cls");
}
void choice()//for accepting input for the players
{
string s[] = {"first", "second"};
for(int i = 0; i<2; i++)
{
cout<<"Enter "<< s[i] <<" player name: ";
cin>>name[i];
cout<< "choose color 1 for ";
setConsoleColour(FOREGROUND_GREEN);
cout<<"\xDB\xDB";
setConsoleColour(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
cout<<" 2 for ";
setConsoleColour(FOREGROUND_RED);
cout<<"\xDB\xDB";
setConsoleColour(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
cout<<" 3 for ";
setConsoleColour(FOREGROUND_BLUE);
cout<<"\xDB\xDB\n";
setConsoleColour(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
cin>>color[i];
}
system("cls");
}
void setConsoleColour(unsigned short colour)
{
static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
std::cout.flush();
SetConsoleTextAttribute(hOut, colour);
}
void ClearScreen()
{
COORD cursorPosition; cursorPosition.X = 0; cursorPosition.Y = 0;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursorPosition);
}
void hidecursor()
{
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(consoleHandle, &info);
}
void gotoXY(int x, int y)
{
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console,CursorPosition);
}