Repository navigation
Expand file tree
/
Copy pathBall.cpp
More file actions
120 lines (99 loc) · 2.47 KB
/
Copy pathBall.cpp
File metadata and controls
120 lines (99 loc) · 2.47 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
#include "Ball.h"
#include "game.h"
Ball::Ball(point center, int r_diameter, game* r_pGame) :
collidable(center, r_diameter, r_diameter, r_pGame), diameter(r_diameter)
{
setImageName("images\\Circle.jpg");
vel.x = 0;
vel.y = 0;
strength = 1;
}
void Ball::clearScreen() const {
window* pWind = pGame->getWind();
pWind->DrawImage("images\\EmptySquare.jpg", uprLft.x, uprLft.y, width, height);
}
void Ball::move()
{
clearScreen();
checkEdges();
uprLft.x += vel.x;
uprLft.y += vel.y;
}
void Ball::checkEdges()
{
window* pWind = pGame->getWind();
int w = pWind->GetWidth();
int h = pWind->GetHeight();
if (uprLft.x + width >= w || uprLft.x <= 0) {
vel.x *= -1;
}
if (uprLft.y - 5 < config.toolBarHeight) {
vel.y *= -1;
}
}
point Ball::getVelocity() const
{
return vel;
}
point Ball::getPosition() const
{
return uprLft;
}
int Ball::getStrength() const
{
return strength;
}
void Ball::setStrength(int s)
{
strength = s;
}
void Ball::setVelocity(float v_x, float v_y)
{
vel.x = v_x;
vel.y = v_y;
}
void Ball::setPosition(float x, float y)
{
eraseball();
uprLft.x = x;
uprLft.y = y;
}
void Ball::reflectOffPaddle(Paddle& paddle)
{
int speed = 10;
int distFromCenter = abs(this->getCenter().x - paddle.getCenter().x);
double ratio = 45.0 / (double(config.paddlew) / 10); //get the width to angle ratio
//divide by 2 as we are looking only at 1/2 the width on each side.
double angle = ratio * distFromCenter;
angle = 3.14 * angle / 180; // convert it to radians
vel.x = speed * cos(angle);
vel.y = -1 * speed * sin(angle);
this->setPosition(uprLft.x, paddle.getPosition().y - height - 1);
}
void Ball::reflectOffBrick(brick& brk)
{
double speed = sqrt(pow(vel.x, 2) + pow(vel.y, 2));
if (brk.collisionDir(*this) == DOWN)
this->setVelocity(-vel.x, vel.y);
else if (brk.collisionDir(*this) == UP)
this->setVelocity(-vel.x, vel.y);
else if (brk.collisionDir(*this) == RIGHT)
this->setVelocity(vel.x, -vel.y);
else
this->setVelocity(vel.x,-vel.y);
}
void Ball::draw() const
{
window* pWind = pGame->getWind();
pWind->DrawImage(imageName, uprLft.x, uprLft.y, width, height);
}
void Ball::collisionAction()
{
}
void Ball::eraseball()
{
window* pw = this->pGame->getWind();
pw->SetBrush(LAVENDER);
pw->SetPen(LAVENDER);
pw->DrawRectangle(0, uprLft.y, 1200, uprLft.y + height, FILLED);
}