shapes_collision_area.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - collision area
  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) 2013-2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. //------------------------------------------------------------------------------------
  17. // Program main entry point
  18. //------------------------------------------------------------------------------------
  19. int main(void)
  20. {
  21. // Initialization
  22. //---------------------------------------------------------
  23. const int screenWidth = 800;
  24. const int screenHeight = 450;
  25. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - collision area");
  26. // Box A: Moving box
  27. Rectangle boxA = { 10, GetScreenHeight()/2.0f - 50, 200, 100 };
  28. int boxASpeedX = 4;
  29. // Box B: Mouse moved box
  30. Rectangle boxB = { GetScreenWidth()/2.0f - 30, GetScreenHeight()/2.0f - 30, 60, 60 };
  31. Rectangle boxCollision = { 0 }; // Collision rectangle
  32. int screenUpperLimit = 40; // Top menu limits
  33. bool pause = false; // Movement pause
  34. bool collision = false; // Collision detection
  35. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  36. //----------------------------------------------------------
  37. // Main game loop
  38. while (!WindowShouldClose()) // Detect window close button or ESC key
  39. {
  40. // Update
  41. //-----------------------------------------------------
  42. // Move box if not paused
  43. if (!pause) boxA.x += boxASpeedX;
  44. // Bounce box on x screen limits
  45. if (((boxA.x + boxA.width) >= GetScreenWidth()) || (boxA.x <= 0)) boxASpeedX *= -1;
  46. // Update player-controlled-box (box02)
  47. boxB.x = GetMouseX() - boxB.width/2;
  48. boxB.y = GetMouseY() - boxB.height/2;
  49. // Make sure Box B does not go out of move area limits
  50. if ((boxB.x + boxB.width) >= GetScreenWidth()) boxB.x = GetScreenWidth() - boxB.width;
  51. else if (boxB.x <= 0) boxB.x = 0;
  52. if ((boxB.y + boxB.height) >= GetScreenHeight()) boxB.y = GetScreenHeight() - boxB.height;
  53. else if (boxB.y <= screenUpperLimit) boxB.y = (float)screenUpperLimit;
  54. // Check boxes collision
  55. collision = CheckCollisionRecs(boxA, boxB);
  56. // Get collision rectangle (only on collision)
  57. if (collision) boxCollision = GetCollisionRec(boxA, boxB);
  58. // Pause Box A movement
  59. if (IsKeyPressed(KEY_SPACE)) pause = !pause;
  60. //-----------------------------------------------------
  61. // Draw
  62. //-----------------------------------------------------
  63. BeginDrawing();
  64. ClearBackground(RAYWHITE);
  65. DrawRectangle(0, 0, screenWidth, screenUpperLimit, collision? RED : BLACK);
  66. DrawRectangleRec(boxA, GOLD);
  67. DrawRectangleRec(boxB, BLUE);
  68. if (collision)
  69. {
  70. // Draw collision area
  71. DrawRectangleRec(boxCollision, LIME);
  72. // Draw collision message
  73. DrawText("COLLISION!", GetScreenWidth()/2 - MeasureText("COLLISION!", 20)/2, screenUpperLimit/2 - 10, 20, BLACK);
  74. // Draw collision area
  75. DrawText(TextFormat("Collision Area: %i", (int)boxCollision.width*(int)boxCollision.height), GetScreenWidth()/2 - 100, screenUpperLimit + 10, 20, BLACK);
  76. }
  77. // Draw help instructions
  78. DrawText("Press SPACE to PAUSE/RESUME", 20, screenHeight - 35, 20, LIGHTGRAY);
  79. DrawFPS(10, 10);
  80. EndDrawing();
  81. //-----------------------------------------------------
  82. }
  83. // De-Initialization
  84. //---------------------------------------------------------
  85. CloseWindow(); // Close window and OpenGL context
  86. //----------------------------------------------------------
  87. return 0;
  88. }