You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.9 KiB

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