Removed the call to updatePosition() from draw(), and added initial position and velocity vectros
This commit is contained in:
11
ball.hpp
11
ball.hpp
@@ -5,16 +5,20 @@ class Ball {
|
|||||||
public:
|
public:
|
||||||
/* Variables */
|
/* Variables */
|
||||||
raylib::Vector2 pos;
|
raylib::Vector2 pos;
|
||||||
|
raylib::Vector2 initial_pos;
|
||||||
raylib::Vector2 vel;
|
raylib::Vector2 vel;
|
||||||
|
raylib::Vector2 initial_vel;
|
||||||
int radius;
|
int radius;
|
||||||
raylib::Color color;
|
raylib::Color color;
|
||||||
|
|
||||||
/* Functions */
|
/* Functions */
|
||||||
Ball(int pos_x, int pos_y, int radius, int vel_x = 0, int vel_y = 0) {
|
Ball(int pos_x, int pos_y, int radius, int vel_x = 0, int vel_y = 0) {
|
||||||
this->pos = raylib::Vector2(pos_x, pos_y);
|
this->pos = raylib::Vector2(pos_x, pos_y);
|
||||||
|
this->initial_pos = this->pos; // Create a copy of the position vector
|
||||||
this->radius = radius;
|
this->radius = radius;
|
||||||
this->color = raylib::Color::White();
|
this->color = raylib::Color::White();
|
||||||
this->vel = raylib::Vector2(vel_x, vel_y);
|
this->vel = raylib::Vector2(vel_x, vel_y);
|
||||||
|
this->initial_vel = this->vel; // Create a copy of the velocity vector
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,6 +26,12 @@ public:
|
|||||||
this->pos = pos;
|
this->pos = pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void reset() {
|
||||||
|
this->pos = this->initial_pos;
|
||||||
|
this->vel = this->initial_vel;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
void updatePosition() {
|
void updatePosition() {
|
||||||
this->pos.x += this->vel.x;
|
this->pos.x += this->vel.x;
|
||||||
this->pos.y += this->vel.y;
|
this->pos.y += this->vel.y;
|
||||||
@@ -29,7 +39,6 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void draw() {
|
void draw() {
|
||||||
updatePosition();
|
|
||||||
DrawCircle(this->pos.x, this->pos.y, this->radius, this->color); // This is a raylib function, not a raylib-cpp function
|
DrawCircle(this->pos.x, this->pos.y, this->radius, this->color); // This is a raylib function, not a raylib-cpp function
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
11
paddle.hpp
11
paddle.hpp
@@ -7,6 +7,7 @@ private:
|
|||||||
raylib::Rectangle rectangle;
|
raylib::Rectangle rectangle;
|
||||||
raylib::Color color;
|
raylib::Color color;
|
||||||
int points;
|
int points;
|
||||||
|
raylib::Vector2 initial_pos;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/* Variables */
|
/* Variables */
|
||||||
@@ -15,6 +16,7 @@ public:
|
|||||||
/* Functions */
|
/* Functions */
|
||||||
Paddle(int pos_x, int pos_y, int width, int height) {
|
Paddle(int pos_x, int pos_y, int width, int height) {
|
||||||
this->rectangle = raylib::Rectangle(pos_x, pos_y, width, height);
|
this->rectangle = raylib::Rectangle(pos_x, pos_y, width, height);
|
||||||
|
this->initial_pos = raylib::Vector2(pos_x, pos_y);
|
||||||
this->color = raylib::Color::White();
|
this->color = raylib::Color::White();
|
||||||
this->velocity = raylib::Vector2(0,0);
|
this->velocity = raylib::Vector2(0,0);
|
||||||
points = 0;
|
points = 0;
|
||||||
@@ -30,6 +32,14 @@ public:
|
|||||||
}
|
}
|
||||||
void incrementPoints() {
|
void incrementPoints() {
|
||||||
points++;
|
points++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset() {
|
||||||
|
this->rectangle.x = this->initial_pos.x;
|
||||||
|
this->rectangle.y = this->initial_pos.y;
|
||||||
|
this->velocity = raylib::Vector2(0,0);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void updatePosition() {
|
void updatePosition() {
|
||||||
@@ -38,7 +48,6 @@ public:
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void draw() {
|
void draw() {
|
||||||
updatePosition();
|
|
||||||
this->rectangle.Draw(this->color);
|
this->rectangle.Draw(this->color);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user