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