shapes_bouncing_ball.bmx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 - bouncing ball")
  8. Local ballPosition:RVector2 = New RVector2(GetScreenWidth()/2, GetScreenHeight()/2)
  9. Local ballSpeed:RVector2 = New RVector2(5.0, 4.0)
  10. Local ballRadius:Int = 20
  11. Local pause:Int = 0
  12. Local framesCounter:Int = 0
  13. SetTargetFPS(60) ' Set our game to run at 60 frames-per-second
  14. '----------------------------------------------------------
  15. ' Main game loop
  16. While Not WindowShouldClose() ' Detect window close button or ESC key
  17. ' Update
  18. '-----------------------------------------------------
  19. If IsKeyPressed(KEY_SPACE) Then
  20. pause = Not pause
  21. End If
  22. If Not pause Then
  23. ballPosition.x :+ ballSpeed.x
  24. ballPosition.y :+ ballSpeed.y
  25. ' Check walls collision for bouncing
  26. If (ballPosition.x >= (GetScreenWidth() - ballRadius)) Or (ballPosition.x <= ballRadius) Then
  27. ballSpeed.x :* -1.0
  28. End If
  29. If (ballPosition.y >= (GetScreenHeight() - ballRadius)) Or (ballPosition.y <= ballRadius) Then
  30. ballSpeed.y :* -1.0
  31. End If
  32. Else
  33. framesCounter :+ 1
  34. End If
  35. '-----------------------------------------------------
  36. ' Draw
  37. '-----------------------------------------------------
  38. BeginDrawing()
  39. ClearBackground(RAYWHITE)
  40. DrawCircleV(ballPosition, ballRadius, MAROON)
  41. DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, LIGHTGRAY)
  42. ' On pause, we draw a blinking message
  43. If pause And ((framesCounter/30) Mod 2) Then
  44. DrawText("PAUSED", 350, 200, 30, GRAY)
  45. End If
  46. DrawFPS(10, 10)
  47. EndDrawing()
  48. '-----------------------------------------------------
  49. Wend
  50. ' De-Initialization
  51. '---------------------------------------------------------
  52. CloseWindow() ' Close window and OpenGL context
  53. '----------------------------------------------------------