Skip to content

Commit 0e2e8ce

Browse files
authored
[examples] Update shapes_bouncing_ball with gravity (#5217)
* [examples] Add shapes_bouncing_ball with gravity * gravity could be enabled/disabled
1 parent 5d9352a commit 0e2e8ce

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

examples/shapes/shapes_bouncing_ball.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
*
55
* Example complexity rating: [★☆☆☆] 1/4
66
*
7-
* Example originally created with raylib 2.5, last time updated with raylib 2.5
7+
* Example originally created with raylib 2.5, last time updated with raylib 5.6
88
*
9+
* Example contributed by Jopestpe (@jopestpe)
10+
*
911
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
1012
* BSD-like license that allows static linking with closed source software
1113
*
@@ -31,7 +33,9 @@ int main(void)
3133
Vector2 ballPosition = { GetScreenWidth()/2.0f, GetScreenHeight()/2.0f };
3234
Vector2 ballSpeed = { 5.0f, 4.0f };
3335
int ballRadius = 20;
36+
float gravity = 0.2f;
3437

38+
bool useGravity = true;
3539
bool pause = 0;
3640
int framesCounter = 0;
3741

@@ -43,16 +47,19 @@ int main(void)
4347
{
4448
// Update
4549
//-----------------------------------------------------
50+
if (IsKeyPressed(KEY_G)) useGravity = !useGravity;
4651
if (IsKeyPressed(KEY_SPACE)) pause = !pause;
47-
52+
4853
if (!pause)
4954
{
5055
ballPosition.x += ballSpeed.x;
5156
ballPosition.y += ballSpeed.y;
5257

58+
if (useGravity) ballSpeed.y += gravity;
59+
5360
// Check walls collision for bouncing
5461
if ((ballPosition.x >= (GetScreenWidth() - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f;
55-
if ((ballPosition.y >= (GetScreenHeight() - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -1.0f;
62+
if ((ballPosition.y >= (GetScreenHeight() - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -0.95f;
5663
}
5764
else framesCounter++;
5865
//-----------------------------------------------------
@@ -65,12 +72,15 @@ int main(void)
6572

6673
DrawCircleV(ballPosition, (float)ballRadius, MAROON);
6774
DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, LIGHTGRAY);
75+
76+
if (useGravity) DrawText("GRAVITY: ON (Press G to disable)", 10, GetScreenHeight() - 50, 20, DARKGREEN);
77+
else DrawText("GRAVITY: OFF (Press G to enable)", 10, GetScreenHeight() - 50, 20, RED);
6878

6979
// On pause, we draw a blinking message
7080
if (pause && ((framesCounter/30)%2)) DrawText("PAUSED", 350, 200, 30, GRAY);
7181

7282
DrawFPS(10, 10);
73-
83+
7484
EndDrawing();
7585
//-----------------------------------------------------
7686
}

0 commit comments

Comments
 (0)