-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathball.cpp
More file actions
187 lines (158 loc) · 4.12 KB
/
ball.cpp
File metadata and controls
187 lines (158 loc) · 4.12 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include "ball.h"
#include "score1.h"
Ball::Ball(): QGraphicsEllipseItem()
{
// Connect public slot to timer
QTimer *ball_timer = new QTimer();
connect(ball_timer, SIGNAL(timeout()), this, SLOT(move()));
ball_timer->start(300);
}
void Ball::move()
{
// Move ball
int i = 0;
while(i == 0)
{
setPos(x()+5, y()-7);
if(pos().y() < 0)
{
i = 1;
}
break;
}
while(i == 1)
{
setPos(x()+5, y()+7);
if(pos().y() > 800)
{
i = 0;
}
break;
}
if(pos().x() < 0)
{
}
}
*/
#include "ball.h"
#include <QTimer>
#include <QBrush>
#include "game.h"
#include "paddle.h"
#include "block.h"
#include "score1.h"
extern Game *game;
Ball::Ball(QGraphicsItem *parent): QGraphicsEllipseItem(parent), QObject()
{
// draw rect
setRect(0,0,30,30);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::green);
setBrush(brush);
// move up right initially
xVelocity = 0;
yVelocity = -5;
// set timer
QTimer* timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(move()));
timer->start(15);
}
double Ball::getCenterX()
{
return x() + rect().width()/2;
}
void Ball::move()
{
// if out of bounds, reverse the velocity
reverseVelocityIfOutOfBounds();
// if collides with paddle, reverse yVelocity, and add xVelocity
// depending on where (in the x axis) the ball hits the paddle
handlePaddleCollision();
// handle collisions with blocks (destroy blocks and reverse ball velocity)
handleBlockCollision();
moveBy(xVelocity, yVelocity);
}
void Ball::reverseVelocityIfOutOfBounds(){
// check if out of bound, if so, reverse the proper velocity
double screenW = game->width();
double screenH = game->height();
// left edge
if(mapToScene(rect().topLeft()).x() <= 0)
{
xVelocity = -1 * xVelocity;
}
// right edge
if(mapToScene(rect().topRight()).x() >= screenW)
{
xVelocity = -1 * xVelocity;
}
// top edge
if(mapToScene(rect().topLeft()).y() <= 0)
{
yVelocity = -1 * yVelocity;
}
// bottom edge - NONE (can fall through bottom)
}
void Ball::handlePaddleCollision()
{
QList<QGraphicsItem*> cItems = collidingItems();
for(size_t i = 0, n = cItems.size(); i < n; ++i)
{
Paddle* paddle = dynamic_cast<Paddle*>(cItems[i]);
if(paddle)
{
// collides with paddle
// reverse the y velocity
yVelocity = -1 * yVelocity;
// add to x velocity depending on where it hits the paddle
double ballX = getCenterX();
double paddleX = paddle->getCenterX();
double dvx = ballX - paddleX;
xVelocity = (xVelocity + dvx)/15;
return;
}
}
}
void Ball::handleBlockCollision()
{
QList<QGraphicsItem*> cItems = collidingItems();
for(size_t i = 0, n = cItems.size(); i < n; ++i)
{
Block* block = dynamic_cast<Block*>(cItems[i]);
// collides with block
if(block)
{
double xPad = 15, yPad = 15;
double ballx = pos().x(), bally = pos().y();
double blockx = block->pos().x(), blocky = block->pos().y();
// collides from bottom
if(bally > blocky + yPad && yVelocity < 0)
{
yVelocity = -1 * yVelocity;
}
// collides from top
if(blocky > bally + yPad && yVelocity > 0 )
{
yVelocity = -1 * yVelocity;
}
// collides from right
if(ballx > blockx + xPad && xVelocity < 0)
{
xVelocity = -1 * xVelocity;
}
// collides from left
if(blockx > ballx + xPad && xVelocity > 0)
{
xVelocity = -1 * xVelocity;
}
// delete block(s)
game->scene->removeItem(block);
delete block;
game->score1->increase1();
}
}
}