You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.2 KiB

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