-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.cpp
74 lines (64 loc) · 1.58 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
68
69
70
71
72
73
74
#include "Ball.h"
#include "Constants.h"
Ball::Ball(RenderWindow * window, int x, int y, float dx, float dy, int radius, Paddle* playerLeft, Paddle* playerRight)
{
this->window = window;
this->x = x;
this->y = y;
this->dx = dx;
this->dy = dy;
this->radius = radius;
this->playerLeft = playerLeft;
this->playerRight = playerRight;
this->circle.setOrigin(radius / 2, radius / 2);
this->circle.setRadius(this->radius);
this->circle.setPosition(this->x, this->y);
this->circle.setPointCount(50);
}
void Ball::draw()
{
this->window->draw(this->circle);
}
void Ball::update(float time)
{
setX(this->x + time * this->dx);
setY(this->y + time * this->dy);
if (
((this->x >= this->playerLeft->getX() - 10 / 2) && (this->x <= this->playerLeft->getX() + 10 / 2))
&&
((this->y >= this->playerLeft->getY() - 50 / 2) && (this->y <= this->playerLeft->getY() + 50 / 2))
||
((this->x >= this->playerRight->getX() - 10 / 2) && (this->x <= this->playerRight->getX() + 10 / 2))
&&
((this->y >= this->playerRight->getY() - 50 / 2) && (this->y <= this->playerRight->getY() + 50 / 2))
)
{
cout << "Ball was reflected" << endl;
this->dx *= -1;
this->dy *= -1;
}
if (this->x > WINDOW_WIDTH)
{
this->playerLeft->updateScore();
setX(WINDOW_WIDTH / 2);
}
else if (this->x < 0)
{
this->playerRight->updateScore();
setX(WINDOW_WIDTH / 2);
}
if (this->y >= WINDOW_HEIGHT || this->y < 0)
{
this->dy *= -1;
}
}
void Ball::setX(int x)
{
this->x = x;
this->circle.setPosition(this->x, this->y);
}
void Ball::setY(int y)
{
this->y = y;
this->circle.setPosition(this->x, this->y);
}