-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
388 lines (337 loc) · 8.58 KB
/
game.cpp
File metadata and controls
388 lines (337 loc) · 8.58 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
#include<iostream>
#include<conio.h>
#include<fstream>
#include<dos.h>
#include <windows.h>
#include <time.h>
#define SCREEN_WIDTH 90
#define SCREEN_HEIGHT 26
#define WIN_WIDTH 70
using namespace std;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
void gotoxy(int x, int y){
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console, CursorPosition);
}
void setcursor(bool visible, DWORD size) {
if(size == 0)
size = 20;
CONSOLE_CURSOR_INFO lpCursor;
lpCursor.bVisible = visible;
lpCursor.dwSize = size;
SetConsoleCursorInfo(console,&lpCursor);
}
class car
{
char car[4][4] = { ' ','T','T',' ',
'O','&','&','O',
' ','&','&',' ',
'O','Y','Y','O' };
protected:
int carPos = WIN_WIDTH/2;
public:
void drawCar(){
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 4);
gotoxy(j+carPos, i+22); cout<<car[i][j];
SetConsoleTextAttribute(hConsole, 7);
}
}
}
void eraseCar(){
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
gotoxy(j+carPos, i+22); cout<<" ";
}
}
}
};
class enemy
{
protected:
int enemyY[3];
int enemyX[3];
int enemyFlag[3];
public:
void drawEnemy(int ind){
if( enemyFlag[ind] == true ){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 9);
gotoxy(enemyX[ind], enemyY[ind]); cout<<"****";
gotoxy(enemyX[ind], enemyY[ind]+1); cout<<" ** ";
gotoxy(enemyX[ind], enemyY[ind]+2); cout<<"****";
gotoxy(enemyX[ind], enemyY[ind]+3); cout<<" ** ";
}
}
void genEnemy(int ind){
enemyX[ind] = 17 + rand()%(33);
}
void eraseEnemy(int ind){
if( enemyFlag[ind] == true ){
gotoxy(enemyX[ind], enemyY[ind]); cout<<" ";
gotoxy(enemyX[ind], enemyY[ind]+1); cout<<" ";
gotoxy(enemyX[ind], enemyY[ind]+2); cout<<" ";
gotoxy(enemyX[ind], enemyY[ind]+3); cout<<" ";
}
}
void resetEnemy(int ind){
eraseEnemy(ind);
enemyY[ind] = 1;
genEnemy(ind);
}
};
class highscore
{
protected:
char Hname[20];
int Hscore;
public:
void displayhighscore(){
system("cls");
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 14);
cout<<"Current Highscore.."<<endl<<endl;
cout<<"Name"<<"\t"<<"Score"<<endl<<endl;
SetConsoleTextAttribute(hConsole, 14);
ifstream score("highscore.txt");
score>>Hname>>Hscore;
cout<<Hname<<"\t"<<Hscore;
score.close();
getche();
}
};
class instruction
{
public:
void instructions(){
system("cls");
cout<<"Instructions";
cout<<"\n----------------";
cout<<"\n Avoid Cars by moving left or right. ";
cout<<"\n\n Press 'a' to move left";
cout<<"\n Press 'd' to move right";
cout<<"\n Press 'escape' to exit";
cout<<"\n\nPress any key to go back to menu";
getch();
}
};
class game : protected enemy, protected car, protected highscore
{
int score;
char name[20];
int speed;
public:
void entername(){
system ("cls");
cout<<"Enter your name: ",
cin>>name;
}
void playmenu(){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 14);
gotoxy(WIN_WIDTH + 7, 2);cout<<"Car Game";
gotoxy(WIN_WIDTH + 6, 4);cout<<"----------";
gotoxy(WIN_WIDTH + 6, 11);cout<<"----------";
gotoxy(WIN_WIDTH + 7, 12);cout<<"Control ";
gotoxy(WIN_WIDTH + 7, 14);cout<<"-------- ";
gotoxy(WIN_WIDTH + 7, 15);cout<<"-------- ";
gotoxy(WIN_WIDTH + 2, 16);cout<<" A Key - Left";
gotoxy(WIN_WIDTH + 2, 17);cout<<" D Key - Right";
gotoxy(18, 4);cout<<"Welcome "<<name;
gotoxy(18, 7);cout<<"Press any key to Start..";getch();
gotoxy(18, 4);cout<<" ";
gotoxy(18, 7);cout<<" ";
SetConsoleTextAttribute(hConsole, 7);
}
void setvalue(){
carPos = -1 + WIN_WIDTH/2;
score = 0;
speed = 0;
enemyFlag[0] = 1;
enemyFlag[1] = 0;
enemyY[0] = enemyY[1] = 1;
}
void drawBorder(){
for(int i=0; i<SCREEN_HEIGHT; i++){
for(int j=0; j<17; j++){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 10);
gotoxy(0+j,i); cout<<"1";
gotoxy(WIN_WIDTH-j,i); cout<<"1";
SetConsoleTextAttribute(hConsole, 7);
}
}
for(int i=0; i<SCREEN_HEIGHT; i++){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 10);
gotoxy(SCREEN_WIDTH,i); cout<<"1";
SetConsoleTextAttribute(hConsole, 7);
}
}
int collision(int ind){
if( enemyY[ind]+4 >= 23 ){
if( enemyX[ind] + 4 - carPos >= 0 && enemyX[ind] + 4 - carPos < 9 ){
return 1;
}
}
return 0;
}
void gameover(){
system("cls");
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 4);
cout<<endl;
cout<<"\t\t------------------------------"<<endl;
cout<<"\t\t-------- GAME OVER -----------"<<endl;
cout<<"\t\t------------------------------"<<endl<<endl;
SetConsoleTextAttribute(hConsole, 7);
cout<<"\t\tPlayer Name: "<<name<<endl<<endl;
cout<<"\t\t-------- You scored: "<<score;
cout<<" -------"<<endl<<endl;
checkHscore(score);
cout<<"\t\tPress escape key to go back to menu.";
getch();
while(1){
char ch = getch();
if( ch==27){
break;
}
}
}
void updateScore(){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 14);
gotoxy(WIN_WIDTH + 7, 5);cout<<"Score: "<<score<<endl;
}
void checkHscore(int value){
int score=value;
ifstream check("highscore.txt");
check>>Hname>>Hscore;
if(score>Hscore){
writeHighScore(score);
cout<<"\t\tCongratulations "<< name <<endl<<" \t\tNew High Score!!"<<endl<<endl;
}
check.close();
}
void writeHighScore(int score) {
FILE *fp;
fp = fopen("highscore.txt", "w");
fprintf(fp, "%s %d",name,score);
fclose(fp);
}
void play(){
entername();
setvalue();
system("cls");
drawBorder();
updateScore();
genEnemy(0);
genEnemy(1);
playmenu();
while(1){
if(kbhit()){
char ch = getch();
if( ch=='a' || ch=='A' ){
if( carPos > 18 )
carPos -= 8;
}
if( ch=='d' || ch=='D' ){
if( carPos < 50 )
carPos += 8;
}
if(ch==27){
break;
}
}
drawCar();
drawEnemy(0);
drawEnemy(1);
if( collision(0) == 1 ){
gameover();
return;
}
if( collision(1) == 1 ){
gameover();
return;
}
Sleep(50 - speed);
eraseCar();
eraseEnemy(0);
eraseEnemy(1);
if( enemyY[0] == 10 )
if( enemyFlag[1] == 0 )
enemyFlag[1] = 1;
if( enemyFlag[0] == 1 )
enemyY[0] += 1;
if( enemyFlag[1] == 1 )
enemyY[1] += 1;
if( enemyY[0] > SCREEN_HEIGHT-4 ){
resetEnemy(0);
score++;
updateScore();
}
if( enemyY[1] > SCREEN_HEIGHT-4 ){
resetEnemy(1);
score++;
updateScore();
}
if(score == 5){
speed=10;
}
if(score == 15){
speed=20;
}
if(score == 25){
speed=30;
}
if(score == 35){
speed=35;
}
if(score == 45){
speed=40;
}
}
}
};
int main()
{
car c;
enemy e;
instruction i;
game g;
highscore h;
setcursor(0,0);
srand( (unsigned)time(NULL));
do{
system("cls");
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 14);
gotoxy(10,5); cout << "==============================\n";
gotoxy(10,6); cout << "| WELCOME TO |\n";
gotoxy(10,7); cout << "==============================\n";
gotoxy(10,8); cout << "| |\n";
gotoxy(10,9); cout << "| CAR GAME |\n";
gotoxy(10,10); cout << "| ____________________ |\n";
gotoxy(10,11); cout << "| |\n";
gotoxy(10,12); cout << "| 1. Start Game |\n";
gotoxy(10,13); cout << "| 2. Instructions |\n";
gotoxy(10,14); cout << "| 3. Highscore |\n";
gotoxy(10,15); cout << "| 3. Exit |\n";
gotoxy(10,16); cout << "| |\n";
gotoxy(10,17); cout << "==============================\n";
gotoxy(10,18); cout<<"Select option: ";
SetConsoleTextAttribute(hConsole, 7);
char op = getche();
if( op=='1')g.play() ;
else if( op=='2') i.instructions();
else if( op=='3') h.displayhighscore();
else if( op=='4') exit(0);
}while(1);
return 0;
}