-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.cpp
67 lines (58 loc) · 1.13 KB
/
ball.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
#include "ball.h"
#include "config.h"
#include <QBrush>
#include <QTimer>
#include <QList>
Ball::Ball()
{
setRect(0, 0, m_width, m_height);
setBrush((QBrush)Qt::green);
QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(move()));
timer->start(50);
dx = -1;
dy = -1;
m_speed = 10;
}
void Ball::setPaddles(Paddle *player, Paddle *computer)
{
m_player = player;
m_computer = computer;
}
bool Ball::insideBoard()
{
return true;
}
//QRectF Ball::boundingRect() const
//{
// return QRectF(x(), y(), m_width, m_height);
//}
int Ball::width()
{
return m_width;
}
int Ball::height()
{
return m_height;
}
int Ball::speed()
{
return m_speed;
}
void Ball::move()
{
//ball collides with players
if(collidesWithItem(m_computer) || collidesWithItem(m_player)){
// dx = -dx;
dx = -dx;
dy = -(rand() % 2) + 1; // random y
}
else if(y()>BOARD_HEIGHT - PADDLE_HEIGHT) {
dy = -dy;
}
else {
// dy = -(rand() % 2) + 1; // random y
}
setPos(x() + (m_speed * dx), y() + (m_speed * dy));
m_computer->randomMove(y());
}