shapes_easings_ball_anim.bmx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. SuperStrict
  2. Framework Ray.Lib
  3. ' Initialization
  4. '--------------------------------------------------------------------------------------
  5. Const screenWidth:Int = 800
  6. Const screenHeight:Int = 450
  7. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - easings ball anim")
  8. ' Ball variable value to be animated with easings
  9. Local ballPositionX:Int = -100
  10. Local ballRadius:Int = 20
  11. Local ballAlpha:Float = 0.0
  12. Local state:Int = 0
  13. Local framesCounter:Int = 0
  14. SetTargetFPS(60) ' Set our game to run at 60 frames-per-second
  15. '--------------------------------------------------------------------------------------
  16. ' Main game loop
  17. While Not WindowShouldClose() ' Detect window close button or ESC key
  18. ' Update
  19. '----------------------------------------------------------------------------------
  20. If state = 0 Then ' Move ball position X with easing
  21. framesCounter :+ 1
  22. ballPositionX = EaseElasticOut(framesCounter, -100, screenWidth/2 + 100, 120)
  23. If framesCounter >= 120 Then
  24. framesCounter = 0
  25. state = 1
  26. End If
  27. Else If state = 1 Then ' Increase ball radius with easing
  28. framesCounter :+ 1
  29. ballRadius = EaseElasticIn(framesCounter, 20, 500, 200)
  30. If framesCounter >= 200 Then
  31. framesCounter = 0
  32. state = 2
  33. End If
  34. Else If state = 2 Then ' Change ball alpha with easing (background color blending)
  35. framesCounter :+ 1
  36. ballAlpha = EaseCubicOut(framesCounter, 0.0, 1.0, 200)
  37. If framesCounter >= 200 Then
  38. framesCounter = 0
  39. state = 3
  40. End If
  41. Else If state = 3 Then ' Reset state to play again
  42. If IsKeyPressed(KEY_ENTER) Then
  43. ' Reset required variables to play again
  44. ballPositionX = -100
  45. ballRadius = 20
  46. ballAlpha = 0.0
  47. state = 0
  48. End If
  49. End If
  50. If IsKeyPressed(KEY_R) Then
  51. framesCounter = 0
  52. End If
  53. '----------------------------------------------------------------------------------
  54. ' Draw
  55. '----------------------------------------------------------------------------------
  56. BeginDrawing()
  57. ClearBackground(RAYWHITE)
  58. If state >= 2 Then
  59. DrawRectangle(0, 0, screenWidth, screenHeight, GREEN)
  60. End If
  61. DrawCircle(ballPositionX, 200, ballRadius, Fade(RED, 1.0 - ballAlpha))
  62. If state = 3 Then
  63. DrawText("PRESS [ENTER] TO PLAY AGAIN!", 240, 200, 20, BLACK)
  64. End If
  65. EndDrawing()
  66. '----------------------------------------------------------------------------------
  67. Wend
  68. ' De-Initialization
  69. '--------------------------------------------------------------------------------------
  70. CloseWindow() ' Close window and OpenGL context
  71. '--------------------------------------------------------------------------------------