Beginners_-_RectangleCollision.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "raylib.h"
  2. int main(void)
  3. {
  4. // Initialization
  5. //--------------------------------------------------------------------------------------
  6. const int screenWidth = 800;
  7. const int screenHeight = 450;
  8. InitWindow(screenWidth, screenHeight, "raylib example.");
  9. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  10. //--------------------------------------------------------------------------------------
  11. // Main game loop
  12. while (!WindowShouldClose()) // Detect window close button or ESC key
  13. {
  14. // Update
  15. //----------------------------------------------------------------------------------
  16. Vector2 mousePos = GetMousePosition();
  17. //----------------------------------------------------------------------------------
  18. // Draw
  19. //----------------------------------------------------------------------------------
  20. BeginDrawing();
  21. ClearBackground(RAYWHITE);
  22. // Create our first rectangle
  23. Rectangle r1;
  24. r1.x = 100;
  25. r1.y = 100;
  26. r1.width = 80;
  27. r1.height = 50;
  28. // Create our second rectangle
  29. Rectangle r2;
  30. r2.x = mousePos.x;
  31. r2.y = mousePos.y;
  32. r2.width = 50;
  33. r2.height = 50;
  34. // Draw our rectangles
  35. DrawRectangleRec(r1,BLUE);
  36. DrawRectangleRec(r2,RED);
  37. // Check collision between rectangles.
  38. if (CheckCollisionRecs(r1,r2))
  39. {
  40. DrawText("Collision!!",0,0,20,RED);
  41. }
  42. DrawText("Example of how use rect collision.", 100, 180, 40, LIGHTGRAY);
  43. EndDrawing();
  44. //----------------------------------------------------------------------------------
  45. }
  46. // De-Initialization
  47. //--------------------------------------------------------------------------------------
  48. CloseWindow(); // Close window and OpenGL context
  49. //--------------------------------------------------------------------------------------
  50. return 0;
  51. }