shapes_easings_ball.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - easings ball
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 2.5, last time updated with raylib 2.5
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2014-2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include "reasings.h" // Required for easing functions
  17. //------------------------------------------------------------------------------------
  18. // Program main entry point
  19. //------------------------------------------------------------------------------------
  20. int main(void)
  21. {
  22. // Initialization
  23. //--------------------------------------------------------------------------------------
  24. const int screenWidth = 800;
  25. const int screenHeight = 450;
  26. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - easings ball");
  27. // Ball variable value to be animated with easings
  28. int ballPositionX = -100;
  29. int ballRadius = 20;
  30. float ballAlpha = 0.0f;
  31. int state = 0;
  32. int framesCounter = 0;
  33. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  34. //--------------------------------------------------------------------------------------
  35. // Main game loop
  36. while (!WindowShouldClose()) // Detect window close button or ESC key
  37. {
  38. // Update
  39. //----------------------------------------------------------------------------------
  40. if (state == 0) // Move ball position X with easing
  41. {
  42. framesCounter++;
  43. ballPositionX = (int)EaseElasticOut((float)framesCounter, -100, screenWidth/2.0f + 100, 120);
  44. if (framesCounter >= 120)
  45. {
  46. framesCounter = 0;
  47. state = 1;
  48. }
  49. }
  50. else if (state == 1) // Increase ball radius with easing
  51. {
  52. framesCounter++;
  53. ballRadius = (int)EaseElasticIn((float)framesCounter, 20, 500, 200);
  54. if (framesCounter >= 200)
  55. {
  56. framesCounter = 0;
  57. state = 2;
  58. }
  59. }
  60. else if (state == 2) // Change ball alpha with easing (background color blending)
  61. {
  62. framesCounter++;
  63. ballAlpha = EaseCubicOut((float)framesCounter, 0.0f, 1.0f, 200);
  64. if (framesCounter >= 200)
  65. {
  66. framesCounter = 0;
  67. state = 3;
  68. }
  69. }
  70. else if (state == 3) // Reset state to play again
  71. {
  72. if (IsKeyPressed(KEY_ENTER))
  73. {
  74. // Reset required variables to play again
  75. ballPositionX = -100;
  76. ballRadius = 20;
  77. ballAlpha = 0.0f;
  78. state = 0;
  79. }
  80. }
  81. if (IsKeyPressed(KEY_R)) framesCounter = 0;
  82. //----------------------------------------------------------------------------------
  83. // Draw
  84. //----------------------------------------------------------------------------------
  85. BeginDrawing();
  86. ClearBackground(RAYWHITE);
  87. if (state >= 2) DrawRectangle(0, 0, screenWidth, screenHeight, GREEN);
  88. DrawCircle(ballPositionX, 200, (float)ballRadius, Fade(RED, 1.0f - ballAlpha));
  89. if (state == 3) DrawText("PRESS [ENTER] TO PLAY AGAIN!", 240, 200, 20, BLACK);
  90. EndDrawing();
  91. //----------------------------------------------------------------------------------
  92. }
  93. // De-Initialization
  94. //--------------------------------------------------------------------------------------
  95. CloseWindow(); // Close window and OpenGL context
  96. //--------------------------------------------------------------------------------------
  97. return 0;
  98. }