gravity could be enabled/disabled

This commit is contained in:
Jopestpe 2025-09-30 13:27:10 -03:00 committed by GitHub
parent fefbac4458
commit d6bcc9271d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -35,6 +35,7 @@ int main(void)
int ballRadius = 20; int ballRadius = 20;
float gravity = 0.2f; float gravity = 0.2f;
bool useGravity = true;
bool pause = 0; bool pause = 0;
int framesCounter = 0; int framesCounter = 0;
@ -46,6 +47,7 @@ int main(void)
{ {
// Update // Update
//----------------------------------------------------- //-----------------------------------------------------
if (IsKeyPressed(KEY_G)) useGravity = !useGravity;
if (IsKeyPressed(KEY_SPACE)) pause = !pause; if (IsKeyPressed(KEY_SPACE)) pause = !pause;
if (!pause) if (!pause)
@ -53,11 +55,11 @@ int main(void)
ballPosition.x += ballSpeed.x; ballPosition.x += ballSpeed.x;
ballPosition.y += ballSpeed.y; ballPosition.y += ballSpeed.y;
ballSpeed.y += gravity; if (useGravity) ballSpeed.y += gravity;
// Check walls collision for bouncing // Check walls collision for bouncing
if ((ballPosition.x >= (GetScreenWidth() - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f; if ((ballPosition.x >= (GetScreenWidth() - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f;
if ((ballPosition.y >= (GetScreenHeight() - ballRadius))) ballSpeed.y *= -0.95f; if ((ballPosition.y >= (GetScreenHeight() - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -0.95f;
} }
else framesCounter++; else framesCounter++;
//----------------------------------------------------- //-----------------------------------------------------
@ -71,6 +73,9 @@ int main(void)
DrawCircleV(ballPosition, (float)ballRadius, MAROON); DrawCircleV(ballPosition, (float)ballRadius, MAROON);
DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, LIGHTGRAY); DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, LIGHTGRAY);
if (useGravity) DrawText("GRAVITY: ON (Press G to disable)", 10, GetScreenHeight() - 50, 20, DARKGREEN);
else DrawText("GRAVITY: OFF (Press G to enable)", 10, GetScreenHeight() - 50, 20, RED);
// On pause, we draw a blinking message // On pause, we draw a blinking message
if (pause && ((framesCounter/30)%2)) DrawText("PAUSED", 350, 200, 30, GRAY); if (pause && ((framesCounter/30)%2)) DrawText("PAUSED", 350, 200, 30, GRAY);