-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ghost.cpp
110 lines (101 loc) · 2.59 KB
/
Ghost.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
#include "Ghost.h"
Ghost::Ghost(QWidget *parent, int initialXX, int initialYY, QPixmap image, Pacman *pacman)
: Character(parent, initialXX, initialYY)
{
this->pacman = pacman;
this->image = image;
imgLabel->setPixmap(image);
this->setGeometry(initialX * gap_size * scale, initialY * gap_size * scale, image.width(), image.height());
this->isScared = false;
timer = new QTimer(this);
timer->setSingleShot(true);
baseExitPosition = new QPoint(14, 11);
connect(timer, SIGNAL(timeout()), this, SLOT(finishScare()));
}
void Ghost::scare()
{
timer->start(1000 * scared_duration);
imgLabel->setPixmap(scaredImage);
isScared = true;
}
void Ghost::finishScare()
{
timer->stop();
this->isScared = false;
imgLabel->setPixmap(image);
}
int Ghost::calculateDistance(const QPoint &a, const QPoint &b)
{
return (a.x() - b.x())*(a.x() - b.x()) + (a.y() - b.y())*(a.y() - b.y());
}
void Ghost::reverse()
{
if (direction == LEFT)
direction = RIGHT;
if (direction == RIGHT)
direction = LEFT;
if (direction == UP)
direction = DOWN;
if (direction == DOWN)
direction = UP;
}
void Ghost::followPoint(const QPoint &p)
{
if (isInBase())
{
newDirection = UP;
return;
}
int minDist = 10000000;
int dist;
Direction d;
if (canGoRight() && direction != LEFT)
{
position->setX(position->x() + 1);
dist = calculateDistance(*position, p);
if (dist < minDist)
{
minDist = dist;
d = RIGHT;
}
position->setX(position->x() - 1);
}
if (canGoLeft() && direction != RIGHT)
{
position->setX(position->x() - 1);
dist = calculateDistance(*position, p);
if (dist < minDist)
{
minDist = dist;
d = LEFT;
}
position->setX(position->x() + 1);
}
if (canGoUp() && direction != DOWN)
{
position->setY(position->y() - 1);
dist = calculateDistance(*position, p);
if (dist < minDist)
{
minDist = dist;
d = UP;
}
position->setY(position->y() + 1);
}
if (canGoDown() && direction != UP)
{
position->setY(position->y() + 1);
dist = calculateDistance(*position, p);
if (dist < minDist)
{
minDist = dist;
d = DOWN;
}
position->setY(position->y() - 1);
}
newDirection = d;
}
bool Ghost::isInBase()
{
return (13 < position->x() && position->x() < 16) && (11 < position->y() && position->y() < 15);
}