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.

80 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-2022 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. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - bouncing ball");
  24. Vector2 ballPosition = { GetScreenWidth()/2.0f, GetScreenHeight()/2.0f };
  25. Vector2 ballSpeed = { 5.0f, 4.0f };
  26. int ballRadius = 20;
  27. bool pause = 0;
  28. int framesCounter = 0;
  29. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  30. //----------------------------------------------------------
  31. // Main game loop
  32. while (!WindowShouldClose()) // Detect window close button or ESC key
  33. {
  34. // Update
  35. //-----------------------------------------------------
  36. if (IsKeyPressed(KEY_SPACE)) pause = !pause;
  37. if (!pause)
  38. {
  39. ballPosition.x += ballSpeed.x;
  40. ballPosition.y += ballSpeed.y;
  41. // Check walls collision for bouncing
  42. if ((ballPosition.x >= (GetScreenWidth() - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f;
  43. if ((ballPosition.y >= (GetScreenHeight() - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -1.0f;
  44. }
  45. else framesCounter++;
  46. //-----------------------------------------------------
  47. // Draw
  48. //-----------------------------------------------------
  49. BeginDrawing();
  50. ClearBackground(RAYWHITE);
  51. DrawCircleV(ballPosition, (float)ballRadius, MAROON);
  52. DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, LIGHTGRAY);
  53. // On pause, we draw a blinking message
  54. if (pause && ((framesCounter/30)%2)) DrawText("PAUSED", 350, 200, 30, GRAY);
  55. DrawFPS(10, 10);
  56. EndDrawing();
  57. //-----------------------------------------------------
  58. }
  59. // De-Initialization
  60. //---------------------------------------------------------
  61. CloseWindow(); // Close window and OpenGL context
  62. //----------------------------------------------------------
  63. return 0;
  64. }