Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

75 wiersze
2.6 KiB

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