Hi, I have updated a code little bit with some if loops inside while loops, also disabled the regular Rect (Axe) movement. And you should try this too. Its nicely chasing.
#include "raylib.h"
int ScreenWidth = 800;
int ScreenHeight = 450;
int CircleSize = 25;
//Circle Positions
Vector2 CirclePos{ScreenWidth / 2, ScreenHeight / 2};
//Rect Positions
Vector2 RectPos{100, 0};
Vector2 RectSize{50, 50};
int RectMoveSpeed = 3;
//Game Over stat
bool CollisionWithEnemy = false;
//Circle Bounries
int LeftCircleEdge = CirclePos.x - CircleSize;
int RightCircleEdge = CirclePos.x + CircleSize;
int UpCircleEdge = CirclePos.y - CircleSize;
int BottomCircleEdge = CirclePos.y + CircleSize;
//Rect Boundries
int LeftRectEdge = RectPos.x;
int RightRectEdge = RectPos.x + RectSize.x;
int UpperRectEdge = RectPos.y;
int BottomRectEdge {RectPos.y + RectSize.y};
//Timer
int counter = 1;
int timer = 0;
int main()
{
CollisionWithEnemy =
(BottomRectEdge >= UpCircleEdge) &&
(UpperRectEdge <= BottomCircleEdge) &&
(RightRectEdge >= LeftCircleEdge) &&
(LeftRectEdge <= RightCircleEdge);
SetTargetFPS(60);
InitWindow(ScreenWidth, ScreenHeight, "AxeGameProject");
while (WindowShouldClose() == false)
{
BeginDrawing();
//avoiding issue of screen flickering due to Double buffer rendering process
ClearBackground(WHITE);
if (CollisionWithEnemy)
{
DrawText("Game Over!", 400, 200, 20, RED);
}
else
{
//Updating Edges
LeftCircleEdge = CirclePos.x - CircleSize;
RightCircleEdge = CirclePos.x + CircleSize;
UpCircleEdge = CirclePos.y - CircleSize;
BottomCircleEdge = CirclePos.y + CircleSize;
LeftRectEdge = RectPos.x;
RightRectEdge = RectPos.x + RectSize.x;
UpperRectEdge = RectPos.y;
BottomRectEdge = RectPos.y + RectSize.y;
//updating collision
CollisionWithEnemy =
(BottomRectEdge >= UpCircleEdge) &&
(UpperRectEdge <= BottomCircleEdge) &&
(RightRectEdge >= LeftCircleEdge) &&
(LeftRectEdge <= RightCircleEdge);
//Creating a circle
DrawCircle(CirclePos.x, CirclePos.y, CircleSize, RED);
//Creating Rectangle
DrawRectangle(RectPos.x, RectPos.y, RectSize.x, RectSize.y, RED);
/*
//Move Rect
RectPos.y += RectMoveSpeed;
if (RectPos.y > ScreenHeight || RectPos.y < 0)
{
RectMoveSpeed = -RectMoveSpeed;
}
*/
//INteract with circle to Move that doesnt go outside screen size
if(IsKeyDown(KEY_D) && CirclePos.x < ScreenWidth - CircleSize) //Move Right till screen size
{
CirclePos.x = CirclePos.x + 10;
}
else if (IsKeyDown(KEY_A) && CirclePos.x > 0 + CircleSize) //Move Left till screen size
{
CirclePos.x = CirclePos.x - 10;
}
else if (IsKeyDown(KEY_W) && CirclePos.y > 0 + CircleSize) //Move Up till screen size
{
CirclePos.y = CirclePos.y - 10;
}
else if (IsKeyDown(KEY_S) && CirclePos.y < ScreenHeight - CircleSize) //Move Down till screen size
{
CirclePos.y = CirclePos.y + 10;
}
//Chasing Player.....
//X
if (RectPos.x > CirclePos.x)
{
if (RectPos.x != CirclePos.x)
{
RectPos.x -= RectMoveSpeed;
}
}
else if (RectPos.x < CirclePos.x)
{
if (RectPos.x != CirclePos.x)
{
RectPos.x += RectMoveSpeed;
}
}
//Y
if (RectPos.y > CirclePos.y)
{
if (RectPos.y != CirclePos.y)
{
RectPos.y -= RectMoveSpeed;
}
}
else if (RectPos.y < CirclePos.y)
{
if (RectPos.y != CirclePos.y)
{
RectPos.y += RectMoveSpeed;
}
}
}
EndDrawing();
}
}