-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacman.h
348 lines (276 loc) · 10.9 KB
/
pacman.h
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
#ifndef PACMAN_H
#define PACMAN_H
extern int score , key;
class PACMAN : public OBJECT{
private:
void *rightPac , *leftPac, *upPac, *downPac, *p[3];
int index ,keyBoardPressed;
char chk;
void initialize(){
cox=20;
coy=20;
index=0;
step=10;
keyBoardPressed=0;
chk=0;
//black
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(685,200,15,15);
area = imagesize(0, 0, 40 , 40);
p[2] = malloc(area);
getimage(670 , 185 , 700 , 215 , p[2]);
putimage(670 , 185 , p[2] , XOR_PUT);
//closed mouth
setcolor(BLACK);
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(685,200,15,15);
area = imagesize(0, 0, 40 , 40);
p[1] = malloc(area);
getimage(670 , 185 , 700 , 215 , p[1]);
putimage(670 , 185 , p[1] , XOR_PUT);
//right
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(685,200,15,15);
setfillstyle(SOLID_FILL,YELLOW);
sector(685 , 200 , 30 , 330 , 15 , 15);
area = imagesize(0, 0, 40 , 40);
rightPac = malloc(area);
getimage(670 , 185 , 700 , 215 , rightPac);
putimage(670 , 185 , rightPac , XOR_PUT);
//left
setcolor(BLACK);
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(685,200,15,15);
setfillstyle(SOLID_FILL,BLACK);
sector(685 , 200 , 150 , 210 , 15 , 15);
area = imagesize(0, 0, 40 , 40);
leftPac = malloc(area);
getimage(670 , 185 , 700 , 215 , leftPac);
putimage(670 , 185 , leftPac , XOR_PUT);
//up
setcolor(BLACK);
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(685,200,15,15);
setfillstyle(SOLID_FILL,BLACK);
sector(685 , 200 , 60 , 120 , 15 , 15);
area = imagesize(0, 0, 40 , 40);
upPac = malloc(area);
getimage(670 , 185 , 700 , 215 , upPac);
putimage(670 , 185 , upPac , XOR_PUT);
//down
setcolor(BLACK);
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(685,200,15,15);
setfillstyle(SOLID_FILL,BLACK);
sector(685 , 200 , 240 , 300 , 15 , 15);
area = imagesize(0, 0, 40 , 40);
downPac = malloc(area);
getimage(670 , 185 , 700 , 215 , downPac);
putimage(670 , 185 , downPac , XOR_PUT);
p[0]=rightPac;
}
public:
PACMAN(){
initialize();
putimage(cox , coy , p[0] , COPY_PUT);
}
PACMAN(int x, int y){
initialize();
cox=x;
coy=y;
putimage(cox , coy , p[0] , COPY_PUT);
}
~PACMAN(){
erasePacman();
}
void clearPacman(){
putimage(cox , coy , p[2] , COPY_PUT);
}
void erasePacman(){
if(p[0]==rightPac){
int sAngle=30, eAngle=330;
while(sAngle!=eAngle){
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(cox+15,coy+15,15,15);
setfillstyle(SOLID_FILL,YELLOW);
sector(cox+15,coy+15 , sAngle , eAngle , 15 , 15);
sAngle+=10;
eAngle-=10;
if(sAngle>=360){
sAngle-=360;
}else if(sAngle<0){
sAngle+=360;
}
if(eAngle>=360){
eAngle-=360;
}else if(eAngle<0){
eAngle+=360;
}
delay(50);
}
putimage(cox , coy , p[2] , COPY_PUT);
}else if(p[0]==leftPac){
int sAngle=150, eAngle=210;
while(sAngle!=eAngle){
setcolor(BLACK);
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(cox+15,coy+15,15,15);
setfillstyle(SOLID_FILL,BLACK);
sector(cox+15,coy+15, sAngle , eAngle , 15 , 15);
sAngle-=10;
eAngle+=10;
if(sAngle>=360){
sAngle-=360;
}else if(sAngle<0){
sAngle+=360;
}
if(eAngle>=360){
eAngle-=360;
}else if(eAngle<0){
eAngle+=360;
}
delay(50);
}
putimage(cox , coy , p[2] , COPY_PUT);
}else if(p[0]==upPac){
int sAngle=60, eAngle=120;
while(sAngle!=eAngle){
setcolor(BLACK);
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(cox+15,coy+15,15,15);
setfillstyle(SOLID_FILL,BLACK);
sector(cox+15,coy+15, sAngle , eAngle , 15 , 15);
sAngle-=10;
eAngle+=10;
if(sAngle>=360){
sAngle-=360;
}else if(sAngle<0){
sAngle+=360;
}
if(eAngle>=360){
eAngle-=360;
}else if(eAngle<0){
eAngle+=360;
}
delay(50);
}
putimage(cox , coy , p[2] , COPY_PUT);
}else if(p[0]==downPac){
int sAngle=240, eAngle=300;
while(sAngle!=eAngle){
setcolor(BLACK);
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(cox+15,coy+15,15,15);
setfillstyle(SOLID_FILL,BLACK);
sector(cox+15,coy+15, sAngle , eAngle , 15 , 15);
sAngle-=10;
eAngle+=10;
if(sAngle>=360){
sAngle-=360;
}else if(sAngle<0){
sAngle+=360;
}
if(eAngle>=360){
eAngle-=360;
}else if(eAngle<0){
eAngle+=360;
}
delay(50);
}
putimage(cox , coy , p[2] , COPY_PUT);
}
}
int obstacleUp(){
int colour=0;
if((colour=getpixel(cox+15,coy-7))!=BLACK ){ //collided up-centre
return colour;
}else if((colour=getpixel(cox,coy-7))!=BLACK ){ //collided up-left
return colour;
}else if((colour=getpixel(cox+30,coy-7))!=BLACK ){ //collided up-right
return colour;
}else
return 0; //0 = no collision
}
int obstacleDown(){
int colour=0;
if((colour=getpixel(cox+15,coy+35))!=BLACK ){ //collided down-centre
return colour;
}else if((colour=getpixel(cox,coy+35))!=BLACK ){ //collided down-left
return colour;
}else if((colour=getpixel(cox+30,coy+35))!=BLACK ){ //collided down-right
return colour;
}else
return 0; //0 = no collision
}
int obstacleLeft(){
int colour=0;
if((colour=getpixel(cox-5,coy+15))!=BLACK ) //collided left-centre
return colour;
else if((colour=getpixel(cox-5,coy))!=BLACK ) //collided left-up
return colour;
else if((colour=getpixel(cox-5,coy+30))!=BLACK ) //collided left-bottom
return colour;
else
return 0; //0 = no collision
}
int obstacleRight(){
int colour=0;
if((colour=getpixel(cox+37,coy+15))!=BLACK ) //collided right-centre
return colour;
else if((colour=getpixel(cox+37,coy))!=BLACK ) //collided right-up
return colour;
else if((colour=getpixel(cox+37,coy+30))!=BLACK ) //collided right-bottom
return colour;
else
return 0; //0 = no collision
}
void stepType1(){ //auto move.... step by 1 frame forward
{
while(kbhit()){
key=getch();
::key=key;
}
{ //block structured just for style... :P ... K...
putimage(cox , coy , p[2] , COPY_PUT); //erase old image
int colour =0; //store the colour of obstacle
/*
colour:-
0=Black=Background=no obstacle
Green=Boundary
Red=Food
*/
if((key=='w'||key=='W') && ((colour=obstacleUp())==0 || colour==RED)){ //up. i.e go up if no obstacle or obstacle is food
coy-=step;
p[0]=upPac;
if(colour==RED)
score++;
}else if((key=='s'||key=='S') && ((colour=obstacleDown())==0 || colour==RED)){ //down
coy +=step;
p[0]=downPac;
if(colour==RED)
score++;
}else if((key=='d'||key=='D') && ((colour=obstacleRight())==0 || colour==RED)){ //right
cox +=step;
p[0]=rightPac;
if(colour==RED)
score++;
}else if((key=='a'||key=='A') && ((colour=obstacleLeft())==0 || colour==RED)){ //left
cox -=step;
p[0]=leftPac;
if(colour==RED)
score++;
}else if(key==27){ //Esc(escape) key: Exit
//break;
::key=27;
}
putimage(cox , coy , p[index] , COPY_PUT); //put new image
index=!index;
delay(100);
}
}
}
};
#endif