-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.cpp
More file actions
77 lines (63 loc) · 1.35 KB
/
Ball.cpp
File metadata and controls
77 lines (63 loc) · 1.35 KB
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
#include "Ball.h"
#include <QtGui/QPainter>
Ball::Ball(QGraphicsItem *parent) :
QGraphicsEllipseItem(parent)
{
_init_();
}
Ball::Ball(const QRectF &rect, QGraphicsItem *parent) :
QGraphicsEllipseItem(rect, parent)
{
_init_();
}
Ball::Ball(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent) :
QGraphicsEllipseItem(x, y, w, h, parent)
{
_init_();
}
void Ball::setSettng(const Setting &setting)
{
_setting = setting;
}
Setting Ball::setting() const
{
return _setting;
}
void Ball::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
painter->setPen(QPen(Qt::black, 1));
painter->setBrush(QBrush(_setting.color()));
painter->drawEllipse(this->rect());
}
void Ball::setXDirection(qreal xDirection)
{
if (xDirection == 1 || xDirection == -1) {
_xDirection = xDirection;
}
}
qreal Ball::xDirection() const
{
return _xDirection;
}
void Ball::setYDirection(qreal yDirection)
{
if (yDirection == 1 || yDirection == -1) {
_yDirection = yDirection;
}
}
qreal Ball::yDirection() const
{
return _yDirection;
}
void Ball::advance(int phase)
{
if (phase != 0) {
qreal speed = _setting.speed();
this->setPos(this->x() + (_xDirection *speed), this->y() + (_yDirection *speed));
}
}
void Ball::_init_()
{
_xDirection = 0;
_yDirection = 0;
}