-
Notifications
You must be signed in to change notification settings - Fork 1
/
CARRR.CPP
346 lines (340 loc) · 7.81 KB
/
CARRR.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
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<graphics.h>
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
#define ENTER 13
class CAR
{
public:
int cp, init, op1, op2, op3; //initializing car,and obstacles
CAR::CAR() //creating constructor for the class car
{
cp = 300; //putting car value at 300
init = 0;
op1 = 0; //1st obstacle starts from 0
op2 = -20; //2nd obstacle starts 20 behind the 1st obstacle
op3 = -40; //3rd one starts 40 behind the 1st one
}
}
car; //object
int randx( void ) //creating function for random obstacle appearance
{
int x = random( 55*8 );//using random function to choose a random valuefor random obstacle
if ( x < 25*8 )//if the x axis distance is less then 25 the obstacle lies outside the lane
{
x = x + ( 25 *8 );//so we add 25 to keep the values in the lane
}
return ( x );
}
void right() //this is called to perform right dirn movement
{
if ( car.cp < 420 ) //here the class object is compared
{
car.cp += 10;//to move the car from 10 dirn
}
return ;
}
void left() //this is for left dirn
{
if ( car.cp > 180 ) //for left movement
{
car.cp -= 10;
}
return ;
}
void bgmove() //car body and its movement
{
if ( car.init > 5 )
{
car.init = 1;//init set to one
}
setcolor( RED ); //car body
setfillstyle( SOLID_FILL,WHITE );//fills the block into colour assigned
sector( car.cp + 20, 330, 0, 180, 20, 30 );//formation of ellipse head of car
setfillstyle( SOLID_FILL,RED );
bar3d( car.cp, 333, car.cp + 37, 360, 4, 4 );//function to create a block i.e. looks as a cube
for ( int i = car.init;i < 70;i += 5 )//for the design of lane
{
setcolor(BLUE );
setfillstyle( 1, BLUE );
bar( 16 * 8, i * 8, 20 * 8, ( i + 2 ) * 8 );//creating walls forming at side of road
bar( 60 * 8, i * 8, 64 * 8, ( i + 2 ) * 8 );
}
return ;
}
void opp1( int x1 )//obstacle 1
{
if ( car.op1 < 600 )
{
car.op1 += 10;
setfillstyle( 1, RED );
bar3d( x1, car.op1, x1 + 30, car.op1 + 30, 2, 3 );
}
else
{
car.op1 = 0;
}
return ;
}
void opp2( int x2 )//obstacle 2
{
if ( car.op2 < 600 )
{
car.op2 += 10;
setfillstyle( 1, YELLOW );
bar3d( x2, car.op2, x2 + 30, car.op2 + 30, 2, 3 );
}
else
{
car.op2 = 0;
}
return ;
}
void opp3( int x3 ) //obstacle 3
{
if ( car.op3 < 600 )
{
car.op3 += 10;
setfillstyle( 1, BLUE );
bar3d( x3, car.op3, x3 + 30, car.op3 + 30, 2, 3 );
}
else
{
car.op3 = 0;
}
return ;
}
int check( int x, int op, int cp ) //collision part
{
int a[ 31 ], b[ 31 ], c[ 40 ], d[ 50 ];//let the sizes randomly or just let according to loop
for ( int i = 0;i < 30;i++ )
{
a[ i ] = x + i; //random value suppled + i using loop
}
for ( i = 0;i < 30;i++ )
{
b[ i ] = op + i;//returned obstacle value
}
for ( i = 0;i < 40;i++ )
{
c[ i ] = cp + i; //returned car value
}
for ( i = 0;i < 50;i++ )
{
d[ i ] = 300 + i; //
}
for ( i = 0;i < 40;i++ )
{
for ( int j = 0;j < 50;j++ )
{
for ( int k = 0;k < 30;k++ )
{
if ( a[ k ] == c[ i ] && b[ k ] == d[ j ] )
return 1;
}
}
}
return 0;
}
void logo ()
{
for ( int i = 1;i < 250;i++ ) //for the introduction part
{
int p;
setcolor( YELLOW );
setfillstyle( SOLID_FILL, LIGHTBLUE );
settextstyle( 7, HORIZ_DIR, 5 );
outtextxy( 65, 100,"HCOE GRAPHICS PROJECT" );
outtextxy(150,150 ," RACING GAME " );
outtextxy(150,200," 070 BCT B ");
setcolor( WHITE );
settextstyle( 2, HORIZ_DIR, 5 );
outtextxy(150,250, "PRESENTED TO : DEPARTMENT OF ELECTRONICS AND COMPUTER " );
outtextxy(150,300, "PRESENTED BY : ROLL NOS (48,50,52,57,64)");
}
delay( 2000 );
cleardevice();
return ;
}
void over( void )//after collision this is to show the game over
{
for ( int i = 1;i < 35;i++ )
{
cleardevice();
delay( 20 );
settextstyle( 1, 0, 6 );
setcolor( RED );
outtextxy( 150, 100, "!!!GAME OVER!!!" );
delay( 20 );
}
delay( 2000 );
cleardevice();
getch();
return ;
}
void about() //menu part and control the menu
{
while ( !kbhit() )
{
for ( int i = 300;kbhit() == 0;i++ )
{
cleardevice();
setcolor( 6 );
settextstyle( 2, 0, 6 );
outtextxy( 600 - i, 600 - i, "season maharjan(48)" );
outtextxy( i - 150, 215, "yaju shrestha(64)" );
outtextxy( i - 30, i - 130, "smriti mool(52)" );
outtextxy( 600 - i, i - 150, "shiwangi shrestha(50)" );
outtextxy( 600 -i, i -300 , "sushmita chaurasia(57)" );
setfillstyle( 1, 4 );
setcolor( 1 );
settextstyle( 2, 0, 10 );
outtextxy( 280, 380, "HCOE" );
outtextxy( 215, 420, "O70 BATCH" );
delay( 60 );
}
}
getch();
return ;
}
int play( void )//if the play is entered by user
{
int life = 1, m, x1, x2, x3, s;//assigning values for life, m for selection
//x1,x2,x3 for random values from the randx function
re:
while ( life ) //check the life value to enter the while loop
{
cleardevice();
if ( car.op1 == 0 ) //
{
x1 = randx();//getting the random values
car.op1++;//calling the obstacle in class and incrementing
}
else
{
opp1( x1 );//if not equal to zero print the obstacle
}
if ( car.op2 == 0 )//for obstacle 2
{
x2 = randx();
car.op2++;
}
else if ( car.op2 < 1 ) //for obstacle valueless than 1
{
car.op2++;
}
else
{
opp2( x2 );
}
if ( car.op3 == 0 ) //for obstacle no 3
{
x3 = randx();
car.op3++;
}
else if ( car.op3 < 1 )
{car.op3++;}
else
{
opp3( x3 );
}
setcolor( 10 );
bgmove(); //calling move function for car to appearin sceen
if ( check( x2, car.op2, car.cp ) || check( x1, car.op1, car.cp ) || check( x3, car.op3, car.cp ) )
{ //checking the collision condition from the check function
life--;
if ( life == 0 )
{
over();//calling the over fuction for finishing the game
}
car.op1 = 1; //initializing the obstacle
car.op2 = -16;
car.op3 = -32;
goto re;//using goto to repeat the processes always
}
if ( kbhit() )//check the key stroke
{
m = getch();
switch ( m ) //key stroke decision using switch case
{
case LEFT: left();//for left arrow
break;
case RIGHT: right(); //for right arrow key
break;
case 'q': exit( 0 ); //to exit the game when ever the user want to
}
}
else
{
delay( 100 );
}
}
getch();
return ( 0 );
}
void menu()//for the menu appearance
{
int s = 1, t = 0, a, p = 140;//here s is for keystroke selection in switch case
do//to contiue the program after finish
{ //here a is for the rectangle to move up and down
do //p for rectangle use in menu
{
cleardevice();
setcolor( YELLOW );
settextstyle( 1, 0, 4 );
outtextxy( 230, 30, "MENU" );
// setfillstyle( 1, 2 );
settextstyle( 1, 0, 1 );
setcolor( 10 );
outtextxy( 220, 137, "PLAY GAME" );
outtextxy( 220,167, "DEVELOPERS" );
outtextxy( 220,197, "EXIT" );
setcolor( RED );
rectangle( 200, p, 350, p + 20 );//for rectangle using rectangle funcn
a = getch();
switch ( a ) //for up and down movemen if rectangle using keyboard
{
case UP: if ( p > 140 )
{
p -= 30;
s--;
}
break;
case DOWN: if ( p < 180 )
{
p += 30;
s++;
};
break;
case ENTER: a = 1;
break;
default: break;
}
}
while ( a != 1 );//check the click of enter by checking the value of a
switch ( s )//for the functions call on press on menu to play
{
case 1: play();
break;
case 2: about();
break;
case 3: exit( 0 );
break;
}
}
while ( t == 0 ); //here t works for the continuity bringing value after the game is over
}
void main()
{
int gdriver = DETECT, gmode;
initgraph( &gdriver, &gmode, "C:\\TC\\bgi" );
cleardevice();
logo();
menu();
getch();
}