-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopter.cpp
317 lines (283 loc) · 6.08 KB
/
copter.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
/**
* Program implementing the copter game
* Created and Developed by - Manish M. Demblani
* Email - mdemblani@gmail.com
* Github Repo - https://github.com/mdemblani/Copter
*/
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<dos.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
union REGS in,out;
void show_mouse()
{ in.x.ax=1;
int86(0x33,&in,&out);
}
void hide_mouse()
{ in.x.ax=2;
int86(0x33,&in,&out);
}
int detect_click()
{ in.x.ax=3;
int86(0x33,&in,&out);
return (out.x.bx);
}
void plot_heli()
{ //Helicopter Window
setfillstyle(1,9);
sector(190,212,0,90,12,7);
//Helicopter Body Outline
line(100,200,150,200);
line(100,200,150,210);
ellipse(170,217, 10, 120,40, 20);
arc(200,217,270,30,10);
ellipse(190,207, 189,285,40, 20);
setfillstyle(1,4);
floodfill(110,201,15);
//Wings
ellipse(175,186,0,360,50,10);
setfillstyle(1,7);
floodfill(176,187,15);
//Tail Fan
putpixel(175,186,WHITE);
circle(100,200,10);
setfillstyle(1,7);
floodfill(101,201,15);
//Base
line(160,235,210,235);
line(180,229,170,235);
line(190,229,200,235);
}
void start_screen()
{
plot_heli();
setfillstyle(1,LIGHTGREEN);
bar(0,0,getmaxx(),50);
bar(0,getmaxy()-50,getmaxx(),getmaxy());
setcolor(11); //11 - LIGHTCYAN
settextstyle( TRIPLEX_FONT, HORIZ_DIR, 6 );
settextjustify( CENTER_TEXT, CENTER_TEXT );
outtextxy(300,100,"COPTER");
settextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
settextjustify( RIGHT_TEXT, CENTER_TEXT );
outtextxy(550,200,"CLICK TO PLAY");
settextstyle( TRIPLEX_FONT, HORIZ_DIR,1 );
settextjustify( LEFT_TEXT, CENTER_TEXT );
outtextxy(200,300," - Click and Hold the LEFT mouse button");
outtextxy(280,320,"to move the copter up");
outtextxy(200,340," - Release to go down");
outtextxy(200,360," - Press any key to quit");
}
int plot_bar(int left)
{ int top; //top - coordinate
randomize();
do
{ top=random(280);
}while(top<50);
setfillstyle(1,LIGHTGREEN);
bar(left,top,left+25,top+100);
return top;
}
void move_bar(int left,int top)
{ setfillstyle(1,BLACK);
bar(left+1,top,left+1+25,top+100);
setfillstyle(1,LIGHTGREEN);
bar(left,top,left+25,top+100);
};
int check_hit(int wleft,int wtop,int top,int bottom)
{
/*left=90;
top=176;
right=225;
bottom=235;
*/
top=top-2;
bottom=bottom+2;
//Front and Back Collision
if(((top)<=(wtop+100))&&((bottom)>=wtop))
{ if(wleft==225||(wleft+25)==90)
return 1;
}
//Base Collision
if((wleft<=225)&&(wleft>=90))
{ if(wtop==(bottom))
return 1;
}
if(((wleft+25)>=90)&&((wleft+25)<225))
{ if(wtop==(bottom))
return 1;
}
//top collision
if((wleft<=225)&&(wleft>=90))
{ if((wtop+100)==(top))
return 1;
}
if(((wleft+25)>=90)&&((wleft+25)<225))
{ if((wtop+100)==(top))
return 1;
}
/*//Base Collision
//line(160,235,210,235);
if(wtop==bottom)
{ if((wleft<=210)&&(wleft>=160))
{
return 1;
}
if(((wleft+25)>=160)&&((wleft+25)<210))
{
return 1;
}
}
//Tail Span Collision
if(wtop==(bottom-25))
{ if((wleft<=140)&&(wleft>=110))
{
return 1;
}
if(((wleft+25)>=110)&&((wleft+25)<160))
{
return 1;
}
}
//Wing Collision
/*{ //Main Wing
if((wleft<=225)&&(wleft>=175))
{ if(wtop+100==top)
return 1;
}
if(((wleft+25)>=175)&&((wleft+25)<225))
{ if(wtop+100==top)
return 1;
}
}*/
/*if((wleft>=90)&&(wleft<225))
if((wtop+100)==top)
return 1;
if(((wleft+25)>=90)&&((wleft+25)<=225))
if((wtop+100)==top)
return 1;
//Tail Wing Collision
if(wtop+100==top)
{ //Main Wing
if((wleft<=175)&&(wleft>=90))
{
return 1;
}
if(((wleft+25)>=90)&&((wleft+25)<175))
{
return 1;
}
}*/
return 0;
}
int start_game(int left,int top,int right,int bottom)
{
unsigned int size,score=0;
char *str;
int i,click,flag=0;
int wall[2][2]; /*bar[][] - { {left,top},
{left,top}
} - coordinates
*/
void *buffer;
setcolor(15); //15-WHITE
plot_heli();
setfillstyle(2,LIGHTGREEN);
bar(0,0,getmaxx(),50);
bar(0,getmaxy()-50,getmaxx(),getmaxy());
size=imagesize(left,top,right,bottom);
//rectangle(left,top,right,bottom);
buffer=malloc(size);
getimage(left,top,right,bottom,buffer);
//To store the image
size=imagesize(left,top,right,bottom);
buffer=malloc(size);
getimage(left,top,right,bottom,buffer);
//Initial plotting of obstacle
wall[0][0]=590;wall[1][0]=1090;
wall[0][1]=plot_bar(wall[0][0]);
wall[1][1]=plot_bar(wall[1][0]);
while(flag==0)
{ click=detect_click();
putimage(left,top,buffer,XOR_PUT);
if(click==1)
{ top=top-2;bottom=bottom-2;
putimage(left,top,buffer,XOR_PUT);
}
else
{ top=top+2;bottom=bottom+2;
putimage(left,top,buffer,XOR_PUT);
}
//Plotting the obstacles
if(wall[0][0]<=(-50))
{ //wall[0][0]=590;
wall[0][0]=wall[1][0]+400;
wall[0][1]=plot_bar(wall[0][0]);
}
if(wall[1][0]<=(-50))
{ //wall[1][0]=1090;
wall[1][0]=wall[0][0]+400;
wall[1][1]=plot_bar(wall[1][0]);
}
wall[0][0]--;wall[1][0]--;
for(i=0;i<=1;i++)
{ if((wall[i][0]<=right)&&((wall[i][0]+25)>=left))
flag=check_hit(wall[i][0],wall[i][1],top,bottom);
}
/*reduce left coordinate of the bar to create the illusion
that helicopter is moving*/
move_bar(wall[0][0],wall[0][1]);
move_bar(wall[1][0],wall[1][1]);
if(top<=50||bottom>=(getmaxy()-50))
flag=1;
score++;
delay(5);
}
return score;
}
void main()
{ int click=0,s;
int top,left,bottom,right;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
cleardevice();
in.x.ax=0;
int86(0x33,&in,&out);
if(out.x.ax==0)
printf("\nMouse Failed to Initialize");
else
{ show_mouse();
start_screen();
detect_click();
do
{
click=detect_click();
delay(100);
}while(click!=1);
delay(2000);
//cleardevice();
clearviewport();
hide_mouse();
//Set co-ordinates
left=90;
top=176;
right=225;
bottom=235;
s=start_game(left,top,right,bottom);
cleardevice();
settextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
//settextjustify( CENTER_TEXT, CENTER_TEXT );
outtextxy(150,50,"GAME OVER!!!");
outtextxy(150,100,"Score = ");
s=s/10;
char *str;
itoa(s,str,10);
outtextxy(280,100,str);
}
getch();
//while(out.x.bx!=1)
closegraph();
}