forked from GeeeekExplorer/PVZ
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mower.cpp
46 lines (41 loc) · 1.02 KB
/
mower.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
#include "mower.h"
Mower::Mower()
{
flag = false;
speed = 270.0 * 33 / 1000;
}
QRectF Mower::boundingRect() const
{
return QRectF(-30, -30, 60, 60);
}
void Mower::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
painter->drawPixmap(QRect(-30, -30, 60, 60), QPixmap(":/images/LawnMower.png"));
}
bool Mower::collidesWithItem(const QGraphicsItem *other, Qt::ItemSelectionMode mode) const
{
Q_UNUSED(mode)
return other->type() == Zombie::Type && qFuzzyCompare(other->y(), y()) && qAbs(other->x() - x()) < 15;
}
void Mower::advance(int phase)
{
if (!phase)
return;
update();
QList<QGraphicsItem *> items = collidingItems();
if (!items.empty())
{
flag = true;
foreach (QGraphicsItem *item, items)
{
Zombie *zombie = qgraphicsitem_cast<Zombie *>(item);
zombie->hp = 0;
}
}
if (flag)
setX(x() + speed);
if (x() > 1069)
delete this;
}