MCPcopy Create free account
hub / github.com/RobLoach/raylib-cpp / main

Function main

examples/shapes/shapes_collision_area.cpp:16–102  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14#include <cmath> // NOLINT
15
16int main(void)
17{
18 // Initialization
19 //---------------------------------------------------------
20 const int screenWidth = 800;
21 const int screenHeight = 450;
22
23 raylib::Window window(screenWidth, screenHeight, "raylib [shapes] example - collision area");
24
25 // Box A: Moving box
26 raylib::Rectangle boxA(10, GetScreenHeight()/2 - 50, 200, 100);
27 int boxASpeedX = 4;
28
29 // Box B: Mouse moved box
30 raylib::Rectangle boxB(GetScreenWidth()/2 - 30, GetScreenHeight()/2 - 30, 60, 60);
31
32 raylib::Rectangle boxCollision(0); // Collision rectangle
33
34 int screenUpperLimit = 40; // Top menu limits
35
36 bool pause = false; // Movement pause
37 bool collision = false; // Collision detection
38
39 SetTargetFPS(60); // Set our game to run at 60 frames-per-second
40 //----------------------------------------------------------
41
42 // Main game loop
43 while (!window.ShouldClose()) { // Detect window close button or ESC key
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 = boxA.CheckCollision(boxB);
65
66 // Get collision rectangle (only on collision)
67 if (collision) boxCollision = boxA.GetCollision(boxB);
68
69 // Pause Box A movement
70 if (IsKeyPressed(KEY_SPACE)) pause = !pause;
71 //-----------------------------------------------------
72
73 // Draw

Callers

nothing calls this directly

Calls 8

IsKeyPressedFunction · 0.85
MeasureTextFunction · 0.85
ShouldCloseMethod · 0.80
DrawRectangleFunction · 0.50
DrawTextFunction · 0.50
CheckCollisionMethod · 0.45
GetCollisionMethod · 0.45
DrawMethod · 0.45

Tested by

no test coverage detected