From a352c42a9d6e6623b9c41eb7f25e34f1c773ecb6 Mon Sep 17 00:00:00 2001 From: FelipeFTN Date: Mon, 17 Jul 2023 00:44:38 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=AC=20score=20points?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ball/ball.cpp | 7 +++++++ src/ball/ball.h | 4 ++++ src/ball/getter.cpp | 4 ++++ src/main.cpp | 17 ++++++++++++----- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/ball/ball.cpp b/src/ball/ball.cpp index 3f18f48..0bcfedb 100644 --- a/src/ball/ball.cpp +++ b/src/ball/ball.cpp @@ -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; @@ -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; } diff --git a/src/ball/ball.h b/src/ball/ball.h index 9ae6b06..65d025e 100644 --- a/src/ball/ball.h +++ b/src/ball/ball.h @@ -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(); @@ -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 diff --git a/src/ball/getter.cpp b/src/ball/getter.cpp index 4880d81..3287c6f 100644 --- a/src/ball/getter.cpp +++ b/src/ball/getter.cpp @@ -20,6 +20,10 @@ Vector2 Ball::GetSpeed() { return ballSpeed; } +int Ball::GetScore() { + return score; +} + Vector2 Ball::GetInitialPosition() { return ballInitialPos; } diff --git a/src/main.cpp b/src/main.cpp index abd47d8..41ddcf1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(); }