-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathsun.cpp
64 lines (57 loc) · 1.4 KB
/
sun.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
#include "sun.h"
#include "shop.h"
Sun::Sun()
{
dest = QPointF(290 + qrand() % (82 * 7), 130 + qrand() % (98 * 5));
setPos(QPointF(dest.x(), 70));
speed = 60.0 * 50 / 1000;
counter = 0;
time = int(10.0 * 1000 / 33);
movie = new QMovie(":/images/Sun.gif");
movie->start();
setAcceptedMouseButtons(Qt::LeftButton);
}
Sun::Sun(QPointF pos)
{
dest = QPointF(pos.x() + qrand() % 30 - 15, pos.y() + qrand() % 30 + 15);
setPos(QPointF(dest.x(), pos.y()));
speed = 60 * 50 / 1000;
counter = 0;
time = int(10.0 * 1000 / 33);
movie = new QMovie(":/images/Sun.gif");
movie->start();
setAcceptedMouseButtons(Qt::LeftButton);
}
Sun::~Sun()
{
if (movie)
delete movie;
}
QRectF Sun::boundingRect() const
{
return QRectF(-35, -35, 70, 70);
}
void Sun::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
painter->drawImage(boundingRect(), movie->currentImage());
}
void Sun::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(event)
Shop *shop = qgraphicsitem_cast<Shop *>(scene()->items(QPointF(300, 15))[0]);
shop->sun += 25;
counter = time;
event->setAccepted(true);
}
void Sun::advance(int phase)
{
if (!phase)
return;
update();
if (++counter >= time)
delete this;
else if (y() < dest.y())
setY(y() + speed);
}