shapes_bouncing_ball.c 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - bouncing ball
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 2.5, last time updated with raylib 2.5
  8. *
  9. * Example contributed by Ramon Santamaria (@raysan5), reviewed by Jopestpe (@jopestpe)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2013-2025 Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. //------------------------------------------------------------------------------------
  19. // Program main entry point
  20. //------------------------------------------------------------------------------------
  21. int main(void)
  22. {
  23. // Initialization
  24. //---------------------------------------------------------
  25. const int screenWidth = 800;
  26. const int screenHeight = 450;
  27. SetConfigFlags(FLAG_MSAA_4X_HINT);
  28. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - bouncing ball");
  29. Vector2 ballPosition = { GetScreenWidth()/2.0f, GetScreenHeight()/2.0f };
  30. Vector2 ballSpeed = { 5.0f, 4.0f };
  31. int ballRadius = 20;
  32. float gravity = 0.2f;
  33. bool useGravity = true;
  34. bool pause = 0;
  35. int framesCounter = 0;
  36. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  37. //----------------------------------------------------------
  38. // Main game loop
  39. while (!WindowShouldClose()) // Detect window close button or ESC key
  40. {
  41. // Update
  42. //-----------------------------------------------------
  43. if (IsKeyPressed(KEY_G)) useGravity = !useGravity;
  44. if (IsKeyPressed(KEY_SPACE)) pause = !pause;
  45. if (!pause)
  46. {
  47. ballPosition.x += ballSpeed.x;
  48. ballPosition.y += ballSpeed.y;
  49. if (useGravity) ballSpeed.y += gravity;
  50. // Check walls collision for bouncing
  51. if ((ballPosition.x >= (GetScreenWidth() - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f;
  52. if ((ballPosition.y >= (GetScreenHeight() - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -0.95f;
  53. }
  54. else framesCounter++;
  55. //-----------------------------------------------------
  56. // Draw
  57. //-----------------------------------------------------
  58. BeginDrawing();
  59. ClearBackground(RAYWHITE);
  60. DrawCircleV(ballPosition, (float)ballRadius, MAROON);
  61. DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, LIGHTGRAY);
  62. if (useGravity) DrawText("GRAVITY: ON (Press G to disable)", 10, GetScreenHeight() - 50, 20, DARKGREEN);
  63. else DrawText("GRAVITY: OFF (Press G to enable)", 10, GetScreenHeight() - 50, 20, RED);
  64. // On pause, we draw a blinking message
  65. if (pause && ((framesCounter/30)%2)) DrawText("PAUSED", 350, 200, 30, GRAY);
  66. DrawFPS(10, 10);
  67. EndDrawing();
  68. //-----------------------------------------------------
  69. }
  70. // De-Initialization
  71. //---------------------------------------------------------
  72. CloseWindow(); // Close window and OpenGL context
  73. //----------------------------------------------------------
  74. return 0;
  75. }