Skip to content

Commit

Permalink
🐬 score points
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeFTN committed Jul 17, 2023
1 parent 08e593a commit a352c42
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/ball/ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ void Ball::Draw() {
DrawTextureRec(ball, ballRec, ballPos, WHITE);
}

void Ball::DrawScore(int x, int y) {
if (score <= 0) { enabled = false; };
DrawText(std::to_string(score).c_str(), x, y, 40, GRAY);
}

void Ball::Move(float deltaTime) {
// Movement
ballPos.x += GetSpeed().x * deltaTime;
Expand Down Expand Up @@ -69,10 +74,12 @@ void Ball::Collision(Ball *ball, bool wall_0, bool wall_1) {
if (GetX() > screenWidth || GetX() < - 20) {
SetPosition(GetInitialPosition());
SetSpeed(Vector2{0.f, 0.f});
score -= 1;
}
if (GetY() > screenHeight || GetY() < - 20) {
SetPosition(GetInitialPosition());
SetSpeed(Vector2{0.f, 0.f});
score -= 1;
}

if (GetSpeed().x > 0.f || GetSpeed().x < 0.f && IsKeyUp(KEY_A) && IsKeyUp(KEY_D)) { ballSpeed.x -= stopDragSpeed * GetSpeed().x; }
Expand Down
4 changes: 4 additions & 0 deletions src/ball/ball.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ class Ball {
void Draw();
void Move(float deltaTime);
void Collision(Ball *ball, bool wall_0, bool wall_1);
void DrawScore(int x, int y);

Vector2 GetInitialPosition();
Rectangle GetCollisionRec();
Vector2 GetSpeed();
float GetHeight();
float GetWidth();
int GetScore();
float GetX();
float GetY();

Expand All @@ -34,5 +36,7 @@ class Ball {
float stopDragSpeed = 0.01f;
Vector2 ballPos { 50, 50 };
Rectangle ballRec { 0, 0, GetWidth(), GetHeight() };
bool enabled = true;
int score = 3;
};
#endif
4 changes: 4 additions & 0 deletions src/ball/getter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Vector2 Ball::GetSpeed() {
return ballSpeed;
}

int Ball::GetScore() {
return score;
}

Vector2 Ball::GetInitialPosition() {
return ballInitialPos;
}
Expand Down
17 changes: 12 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,18 @@ int main() {
map.Wall(&wall_0, show_wall_0);
map.Wall(&wall_1, show_wall_1);

ball_0.Draw();
ball_0.Move(GetFrameTime());

ball_1.Draw();
ball_1.Move(GetFrameTime());
ball_0.DrawScore(screenWidth - 75, 50);
ball_1.DrawScore(50, screenHeight - 75);

if (ball_0.GetScore() > 0) {
ball_0.Draw();
ball_0.Move(GetFrameTime());
} else { DrawText("Player 2 Won!", (screenWidth / 2) - 60, screenHeight / 2, 20, LIGHTGRAY); }

if (ball_1.GetScore() > 0) {
ball_1.Draw();
ball_1.Move(GetFrameTime());
} else { DrawText("Player 1 Won!", (screenWidth / 2) - 60, screenHeight / 2, 20, LIGHTGRAY); }

EndDrawing();
}
Expand Down

0 comments on commit a352c42

Please sign in to comment.