-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheck_collision.cpp
More file actions
54 lines (41 loc) · 1.28 KB
/
Copy pathCheck_collision.cpp
File metadata and controls
54 lines (41 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Collision check in raylib
#include <raylib.h>
int main()
{
InitWindow(800, 600, "Rectangle Collision Detection");
Rectangle player = {200, 200, 50, 50}; // Player rectangle
Rectangle obstacle = {400, 300, 100, 100}; // Static obstacle
SetTargetFPS(60);
// Main game loop
while (!WindowShouldClose())
{
// Move the player rectangle with arrow keys
if (IsKeyDown(KEY_RIGHT))
player.x += 5.0f;
if (IsKeyDown(KEY_LEFT))
player.x -= 5.0f;
if (IsKeyDown(KEY_UP))
player.y -= 5.0f;
if (IsKeyDown(KEY_DOWN))
player.y += 5.0f;
// Check for collision between player and obstacle
bool isColliding = CheckCollisionRecs(player, obstacle);
BeginDrawing();
ClearBackground(RAYWHITE);
if (isColliding)
DrawRectangleRec(player, RED);
else
DrawRectangleRec(player, GREEN);
DrawRectangleRec(obstacle, BLUE);
// Display collision status on screen
if (isColliding)
DrawText("Collision Detected!", 10, 10, 20, RED);
else
DrawText("No Collision", 10, 10, 20, GREEN);
// End drawing
EndDrawing();
}
// Close window
CloseWindow();
return 0;
}