Skip to content
Snippets Groups Projects
Commit ffc78dc8 authored by Lalit2.Dangi@live.uwe.ac.uk's avatar Lalit2.Dangi@live.uwe.ac.uk
Browse files

Edit game.c

parent 9c5516d9
Branches
No related tags found
No related merge requests found
......@@ -13,29 +13,49 @@ Ball *CreateBall(float x, float y, int radius, float speedX, float speedY) {
b->position = (Vector2){x, y};
b->speed = (Vector2){speedX, speedY};
b->radius = radius;
b->score = 0;
b->lives = 3;
b->isGameOver = false;
return b;
}
b->score = 0; // ✅ Start score at 0
void UpdateBall(Ball *ball, Paddle *paddle) {
ball->position.x += ball->speed.x;
ball->position.y += ball->speed.y;
// Bounce off top and bottom walls
if (ball->position.y <= 0 || ball->position.y >= 450) {
ball->speed.y *= -1;
}
// Ball hits paddle
if (CheckCollisionCircleRec(ball->position, ball->radius, paddle->rect)) {
ball->speed.x *= -1;
ball->score++; // ✅ Increase score when ball hits paddle
}
ball->speed.x *= -1;
ball->score++;
}
// Ball missed paddle
if (ball->position.x < 0) {
ball->lives--;
ball->position = (Vector2){400, 225};
if (ball->lives <= 0) {
ball->isGameOver = true;
}
}
}
void DrawGame(Paddle *paddle, Ball *ball) {
DrawRectangleRec(paddle->rect, BLUE);
DrawCircleV(ball->position, ball->radius, RED);
DrawText(TextFormat("Score: %d", ball->score), 10, 10, 20, DARKGRAY);
DrawText(TextFormat("Lives: %d", ball->lives), 10, 40, 20, MAROON);
if (ball->isGameOver) {
DrawText("GAME OVER", 300, 200, 40, RED);
DrawText("Press ESC to quit", 300, 250, 20, DARKGRAY);
}
}
void FreeGame(Paddle *paddle, Ball *ball) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment