diff --git a/game.c b/game.c new file mode 100644 index 0000000000000000000000000000000000000000..b2de0e49c3b36f028ea7035b27fd161a75a4d119 --- /dev/null +++ b/game.c @@ -0,0 +1,40 @@ +#include "game.h" +#include <stdlib.h> + +Paddle *CreatePaddle(float x, float y, float width, float height, int speed) { + Paddle *p = (Paddle *)malloc(sizeof(Paddle)); + p->rect = (Rectangle){x, y, width, height}; + p->speed = speed; + return p; +} + +Ball *CreateBall(float x, float y, int radius, float speedX, float speedY) { + Ball *b = (Ball *)malloc(sizeof(Ball)); + b->position = (Vector2){x, y}; + b->speed = (Vector2){speedX, speedY}; + b->radius = radius; + return b; +} + +void UpdateBall(Ball *ball, Paddle *paddle) { + ball->position.x += ball->speed.x; + ball->position.y += ball->speed.y; + + if (ball->position.y <= 0 || ball->position.y >= 450) { + ball->speed.y *= -1; + } + + if (CheckCollisionCircleRec(ball->position, ball->radius, paddle->rect)) { + ball->speed.x *= -1; + } +} + +void DrawGame(Paddle *paddle, Ball *ball) { + DrawRectangleRec(paddle->rect, BLUE); + DrawCircleV(ball->position, ball->radius, RED); +} + +void FreeGame(Paddle *paddle, Ball *ball) { + free(paddle); + free(ball); +}