/
GObject.cpp
526 lines (496 loc) · 9.85 KB
/
GObject.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
#include "stdafx.h"
#include "GObject.h"
//GOject成员定义:
GMap* GObject::pStage =NULL;
int GObject::GetRow()
{
return dRow;
}
int GObject::GetArray()
{
return dArray;
}
int GObject::PtTransform(int k)
{
return (k -(pStage->LD)/2)/pStage->LD;
}
bool GObject::Achive()
{
int n =(point.x- pStage->LD/2)%pStage->LD;
int k =(point.y- pStage->LD/2)%pStage->LD;
bool l = (n==0&&k==0);
return l;
}
void GObject::AchiveCtrl()
{
if(Achive())
{
dArray = PtTransform(point.x);//更新列
dRow = PtTransform(point.y);//更新行
}
}
void GObject::DrawBlank(HDC& hdc)
{
RECT rect;
rect.top = mY-RD;
rect.left = mX-RD;
rect.right = mX+RD;
rect.bottom = mY+RD;
FillRect(hdc,&rect,::CreateSolidBrush(RGB(255,255,255)));
}
void GObject::SetPosition(int Row,int Array)
{
dRow = Row;
dArray = Array;
this->point.y = dRow*pStage->LD+pStage->LD/2;
this->point.x = dArray*pStage->LD+pStage->LD/2;
}
bool GObject::Collision()
{
bool b = false;
AchiveCtrl();//更新行、列的数据若是大嘴,则会执行PacMan重写的AchiveCtrl函数消除豆子
//判断指令的有效性
#pragma region
if(dArray<0||dRow<0||dArray>MAPLENTH-1||dRow>MAPLENTH-1)
{
b = true;
}
else if(Achive())
{
switch(twCommand)//判断行进的方向
{
case LEFT:
if(dArray>0&&!pStage->mapData[dRow][dArray-1])//判断下一个格子是否能够通行
{
b = true;//指令无效
}
break;
//以下方向的判断原理相同
case RIGHT:
if(dArray<MAPLENTH-1&&!pStage->mapData[dRow][dArray+1])
{
b = true;
}
break;
case UP:
if(dRow>0&&!pStage->mapData[dRow-1][dArray])
{
b = true;
}
break;
case DOWN:
if(dRow<MAPLENTH-1&&!pStage->mapData[dRow+1][dArray])
{
b = true;
}
break;
}
if(!b)
{
tw =twCommand;//没撞墙,指令成功
}
}
#pragma endregion
//依照真实的方向位移
mX = point.x;
mY = point.y;
int MAX = pStage->LD*MAPLENTH+pStage->LD/2;
int MIN = pStage->LD/2;
switch(tw)//判断行进的方向
{
case LEFT:
if(dArray>0&&!pStage->mapData[dRow][dArray-1])//判断下一个格子是否能够通行
{
b= true;
break;//"撞墙了"
}
point.x -= speed;
if(point.x<MIN)
{
point.x = MAX;
}
break;
//以下方向的判断原理相同
case RIGHT:
if(dArray<MAPLENTH-1&&!pStage->mapData[dRow][dArray+1])
{
b= true;
break;//"撞墙了"
}
point.x += speed;
if(point.x>MAX)
{
point.x = MIN;
}
break;
case UP:
if(dRow>0&&!pStage->mapData[dRow-1][dArray])
{
b= true;
break;//"撞墙了"
}
point.y -=speed;
if(point.y<MIN)
{
point.y = MAX;
}
break;
case DOWN:
if(dRow<MAPLENTH-1&&!pStage->mapData[dRow+1][dArray])
{
b= true;
break;//"撞墙了"
}
point.y +=speed;
if(point.y>MAX)
{
point.y = MIN;
}
break;
}
return b;
}
//PacMan成员定义:
void PacMan::AchiveCtrl()
{
GObject::AchiveCtrl();
if(Achive())
{
if(dRow>=0&&dRow<MAPLENTH&&dArray>=0&&dArray<MAPLENTH)//防止数组越界
{
if(pStage->peaMapData[dRow][dArray])
{
pStage->peaMapData[dRow][dArray] = false;
}
}
}
}
void PacMan::action()
{
Collision();
}
void PacMan::SetTwCommand(TWARDS command)
{
twCommand = command;
}
bool PacMan::Win()
{
for(int i=0;i<=MAPLENTH;i++)
{
for(int j=0;j<=MAPLENTH;j++)
{
if(pStage->peaMapData[i][j]==true)
{
return false; //存在任意一个豆子,没取得胜利
}
}
}
return true;//没有豆子,胜利
}
POINT PacMan::GetPos()
{
return point;
}
void PacMan::Over()
{
tw = OVER;
}
TWARDS PacMan::GetTw()
{
return tw;
}
void PacMan::Draw( HDC& memDC)
{
if(frame%2 ==0)//第4祯动画与第2祯动画
{
int x1=0,x2=0,y1=0,y2=0;
int offsetX = DISTANCE/2+D_OFFSET;//弧弦交点
int offsetY = DISTANCE/2+D_OFFSET;//弧弦交点
switch(tw) //优弧
{
case UP:
x1 = point.x - offsetX;
x2 = point.x + offsetX;
y2 = y1 = point.y-offsetY;
break;
case DOWN:
x1 = point.x + offsetX;
x2 = point.x - offsetX;
y2 = y1 = point.y+offsetY;
break;
case LEFT:
x2 = x1 = point.x-offsetX;
y1 = point.y + offsetY;
y2 = point.y - offsetY;
break;
case RIGHT:
x2 = x1 =point.x + offsetX;
y1 = point.y - offsetY;
y2 = point.y + offsetY;
break;
}
Arc(memDC,point.x-DISTANCE,point.y-DISTANCE,
point.x+DISTANCE,point.y+DISTANCE,
x1,y1,
x2,y2);
MoveToEx(memDC,x1,y1,NULL);
LineTo(memDC,point.x,point.y);
LineTo(memDC,x2,y2);
}
else if(frame%3 ==0) //圆形
{
Ellipse(memDC,point.x-DISTANCE,point.y-DISTANCE,
point.x+DISTANCE,point.y+DISTANCE);
}
else {
int x1=0,x2=0,y1=0,y2=0;
switch(tw) //半圆形
{
case UP:
x1 = point.x - DISTANCE;
x2 = point.x + DISTANCE;
y2 = y1 = point.y;
break;
case DOWN:
x1 = point.x + DISTANCE;
x2 = point.x - DISTANCE;
y2 = y1 = point.y;
break;
case LEFT:
x2 = x1 = point.x;
y1 = point.y + DISTANCE;
y2 = point.y - DISTANCE;
break;
case RIGHT:
x2 = x1 =point.x ;
y1 = point.y - DISTANCE;
y2 = point.y + DISTANCE;
break;
}
Arc(memDC,point.x-DISTANCE,point.y-DISTANCE,
point.x+DISTANCE,point.y+DISTANCE,
x1,y1,
x2,y2);
MoveToEx(memDC,x1,y1,NULL);
LineTo(memDC,point.x,point.y);
LineTo(memDC,x2,y2);
}
frame++;//绘制下一祯
}
//Enermy成员定义:
PacMan* Enermy::player = NULL;
void Enermy::Catch()
{
int DX =point.x -player->GetPos().x;
int DY =point.y -player->GetPos().y;
if((-RD<DX&&DX<RD)&&(-RD<DY&&DY<RD))
{
player->Over();
}
}
void Enermy::Draw(HDC& hdc)
{
HPEN pen =::CreatePen(0,0,color);
HPEN oldPen = (HPEN)SelectObject(hdc,pen);
Arc(hdc,point.x-DISTANCE,point.y-DISTANCE,
point.x+DISTANCE,point.y+DISTANCE,
point.x+DISTANCE,point.y,
point.x-DISTANCE,point.y);//半圆型的头
int const LEGLENTH = (DISTANCE)/(LEGCOUNTS);
//根据祯数来绘制身体和“腿部”
if(frame%2 == 0)
{
MoveToEx(hdc,point.x-DISTANCE,point.y,NULL);//矩形的身子
LineTo(hdc,point.x-DISTANCE,point.y +DISTANCE - LEGLENTH); //point.y+10-2
MoveToEx(hdc,point.x+DISTANCE,point.y,NULL);
LineTo(hdc,point.x+DISTANCE,point.y +DISTANCE - LEGLENTH);
for(int i = 0;i<LEGCOUNTS;i++)//从左往右绘制“腿部”
{
Arc(hdc,point.x-DISTANCE+i*2*LEGLENTH,point.y+DISTANCE-2*LEGLENTH,
point.x-DISTANCE+(i+1)*2*LEGLENTH,point.y+DISTANCE,
point.x-DISTANCE+i*2*LEGLENTH,point.y+DISTANCE-LEGLENTH,
point.x-DISTANCE+(i+1)*2*LEGLENTH,point.y+DISTANCE-LEGLENTH
);
}
}
else{
MoveToEx(hdc,point.x-DISTANCE,point.y,NULL);//绘制身体
LineTo(hdc,point.x-DISTANCE,point.y +DISTANCE);
MoveToEx(hdc,point.x+DISTANCE,point.y,NULL);
LineTo(hdc,point.x+DISTANCE,point.y +DISTANCE);
//从左往右绘制“腿部”
MoveToEx(hdc,point.x-DISTANCE,point.y+DISTANCE,NULL);
LineTo(hdc,point.x-DISTANCE+LEGLENTH,point.y+DISTANCE-LEGLENTH);
for(int i = 0;i<LEGCOUNTS-1;i++)
{
Arc(hdc,point.x-DISTANCE+(1+i*2)*LEGLENTH,point.y+DISTANCE-2*LEGLENTH,
point.x-DISTANCE+(3+i*2)*LEGLENTH,point.y+DISTANCE,
point.x-DISTANCE+(1+i*2)*LEGLENTH,point.y+DISTANCE-LEGLENTH,
point.x-DISTANCE+(3+i*2)*LEGLENTH,point.y+DISTANCE-LEGLENTH
);
}
MoveToEx(hdc,point.x+DISTANCE,point.y+DISTANCE,NULL);
LineTo(hdc,point.x+DISTANCE-LEGLENTH,point.y+DISTANCE-LEGLENTH);
}
//根据方向绘制眼睛
int R = DISTANCE/5; //眼睛的半径
switch(tw)
{
case UP:
Ellipse(hdc,point.x-2*R,point.y-2*R,
point.x,point.y);
Ellipse(hdc,point.x,point.y-2*R,
point.x+2*R,point.y);
break;
case DOWN:
Ellipse(hdc,point.x-2*R,point.y,point.x,point.y+2*R);
Ellipse(hdc,point.x,point.y,point.x+2*R,point.y+2*R);
break;
case LEFT:
Ellipse(hdc,point.x-3*R,point.y-R,
point.x-R,point.y +R);
Ellipse(hdc,point.x-R,point.y-R,
point.x+R,point.y +R);
break;
case RIGHT:
Ellipse(hdc,point.x-R,point.y-R,
point.x+R,point.y +R);
Ellipse(hdc,point.x+R,point.y-R,
point.x+3*R,point.y+R);
break;
}
frame++; //准备绘制下一祯
SelectObject(hdc,oldPen);
DeleteObject(pen);
return;
}
void Enermy::action()
{
bool b = Collision();
MakeDecision(b);
Catch();
}
//RedOne成员
void RedOne::Draw(HDC& hdc)
{
Enermy::Draw(hdc);
}
void RedOne::MakeDecision(bool b)
{
//srand(time(0));
int i = rand();
if(b)//撞到墙壁,改变方向
{
//逆时针转向
if(i%4==0)
{
tw == UP?twCommand = LEFT:twCommand=UP;
}
else if(i%3==0)
{
tw == DOWN?twCommand =RIGHT:twCommand=DOWN;
}
else if(i%2==0)
{
tw == RIGHT?twCommand = UP:twCommand=RIGHT;
}
else
{
tw == LEFT?twCommand = DOWN:twCommand=LEFT;
}
return;
}
if(i%4==0)
{
twCommand!=UP?tw==DOWN:twCommand ==UP;
}
else if(i%3==0)
{
tw != DOWN?twCommand = UP:twCommand=DOWN;
}
else if(i%2==0)
{
tw != RIGHT?twCommand = LEFT:twCommand=RIGHT;
}
else
{
tw != LEFT?twCommand = RIGHT:twCommand=LEFT;
}
}
//BlueOne成员定义
void BlueOne::Draw( HDC& hdc)
{
Enermy::Draw(hdc);
}
void BlueOne::MakeDecision(bool b)
{
const int DR = this->dRow-player->GetRow();
const int DA = this->dArray-player->GetArray();
if(!b&&DR==0)
{
if(DA<=BLUE_ALERT&&DA>0)//玩家在左侧边警戒范围s
{
twCommand = LEFT; //向左移动
return;
}
if(DA<0&&DA>=-BLUE_ALERT)//右侧警戒范围
{
twCommand = RIGHT;//向右移动
return;
}
}
if(!b&&DA==0)
{
if(DR<=BLUE_ALERT&&DR>0)//下方警戒范围
{
twCommand = UP;
return;
}
if(DR<0&&DR>=-BLUE_ALERT)//上方警戒范围
{
twCommand = DOWN;
return;
}
}
RedOne::MakeDecision(b);//不在追踪模式时RED行为相同
}
//YellowOne成员定义
void YellowOne::MakeDecision(bool b)
{
const int DR = this->dRow-player->GetRow();
const int DA = this->dArray-player->GetArray();
if(!b)
{
if(DR*DR>DA*DA)
{
if(DA>0)//玩家在左侧边警戒范围
{
twCommand = LEFT; //向左移动
return;
}
else if(DA<0)//右侧警戒范围
{
twCommand = RIGHT;//向右移动
return;
}
}
else
{
if(DR>0)//下方警戒范围
{
twCommand = UP;
return;
}
if(DR<0)//上方警戒范围
{
twCommand = DOWN;
return;
}
}
}
RedOne::MakeDecision(b);
}
void YellowOne::Draw(HDC& hdc)
{
Enermy::Draw(hdc);
}