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.

107 lines
3.8 KiB

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