diff --git a/README.md b/README.md index 6e85e62..1de2eac 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Game is a simple C++ game built using Raylib. It is a two-players game where the To install the game, follow these steps: -Clone the repository to your local machine.
+Clone the repository to your local machine or download the latest game version from the [releases page](https://github.com/FelipeFTN/Game/releases).
Open the project in your preferred C++ IDE.
Make sure you have the Raylib library installed on your machine.
Build and run the game.
diff --git a/src/ball/ball.cpp b/src/ball/ball.cpp index b9b784d..1e83e54 100644 --- a/src/ball/ball.cpp +++ b/src/ball/ball.cpp @@ -25,3 +25,54 @@ void Ball::Move() { ballSpeed.x += 0.2f; } } + +void Ball::SetSpeed(Vector2 speed) { + ballSpeed = {speed.x, speed.y}; +} + +void Ball::Collision() { + bool canCollide = true; + float blockSpeed = 2.f; + + if(GetY() >= 110.f && GetY() <= screenHeight - 125.f) { + canCollide = false; + } else { canCollide = true; } + + if(GetX() >= screenWidth - 30 && canCollide) { + SetSpeed(Vector2{GetSpeed().x - blockSpeed * GetSpeed().x, GetSpeed().y}); + } + if(GetY() >= screenHeight - 30 && canCollide) { + SetSpeed(Vector2{GetSpeed().x, GetSpeed().y - blockSpeed * GetSpeed().y}); + } + if(GetY() <= 15 && canCollide) { + SetSpeed(Vector2{GetSpeed().x, GetSpeed().y - blockSpeed * GetSpeed().y}); + } + if(GetX() <= 15 && canCollide) { + SetSpeed(Vector2{GetSpeed().x - blockSpeed * GetSpeed().x, GetSpeed().y}); + } + + if(GetX() > screenWidth || GetX() < -20) { + SetPosition(GetInitialPosition()); + SetSpeed(Vector2{0.f, 0.f}); + } +} + +void Ball::SetPosition(Vector2 position) { + ballPos = {position.x, position.y}; +} + +float Ball::GetX() { + return ballPos.x; +} + +float Ball::GetY() { + return ballPos.y; +} + +Vector2 Ball::GetSpeed() { + return ballSpeed; +} + +Vector2 Ball::GetInitialPosition() { + return ballInitialPos; +} diff --git a/src/ball/ball.h b/src/ball/ball.h index b561d3d..55d5e78 100644 --- a/src/ball/ball.h +++ b/src/ball/ball.h @@ -5,6 +5,16 @@ class Ball { Ball(int sWidth, int sHeight); void Draw(); void Move(); + void Collision(); + + + Vector2 GetInitialPosition(); + float GetX(); + float GetY(); + + Vector2 GetSpeed(); + void SetSpeed(Vector2 speed); + void SetPosition(Vector2 position); private: Texture2D ball = LoadTexture("assets/ball.png"); @@ -12,6 +22,7 @@ class Ball { int screenHeight = 512; int ballWidth = ball.width; int ballHeight = ball.height; + Vector2 ballInitialPos { 50, 50 }; Vector2 ballSpeed { 0.f, 0.f }; Vector2 ballPos { 50, 50 }; Rectangle ballRec { 0, 0, (float)ballWidth, (float)ballHeight }; diff --git a/src/main.cpp b/src/main.cpp index 361792a..faddea9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,8 @@ -#include "raylib.h" #include "ball/ball.h" #include "map/map.h" +#include "raylib.h" +#include +#include int main() { const int screenWidth = 512; @@ -8,14 +10,16 @@ int main() { InitWindow(screenWidth, screenHeight, "Game"); - Map map{ }; - Ball ball{ screenWidth, screenHeight }; + Map map{}; + Ball ball{screenWidth, screenHeight}; SetTargetFPS(60); - while(!WindowShouldClose()) { + ClearBackground(BLACK); + + while (!WindowShouldClose()) { - ClearBackground(BLACK); + ball.Collision(); BeginDrawing(); @@ -30,3 +34,5 @@ int main() { return 0; } +/* DEBUGGING TEST */ +/* DrawText(std::to_string(ball.GetX()).c_str(), 190, 200, 20, LIGHTGRAY); */