You are probably looking for the absolute value!
You can implement it like this:
if value < 0:
You can use it like this:
radius = 20
if abs(playerPositionX - enemyPositionX) < radius:
However, if your game is in 2D, and you wish to find the circular radius you can calculate the Euclidean distance (distance from origin to point A) to your target like this:
radius = 20 # value is in pixels, but you can modify it to fit your needs
if (sqrt((playerPositionX - enemyPositionX)**2 +
(playerPositionY - enemyPositionY)**2)) < radius:
This is what is used in the above code:
sqrt((playerX - enemyX)**2 + (playerY - enemyY)**2) above is the distance between the player object and the enemy object (it is actually an application of the Pythagorean Theorem!). The green circle is the
radius, it can be represented as a single number (the length from the origin to the border of the circle). After we have calculated the distance we can check if it is less than the radius. If it is, we know there is a collision between the two objects!
For an easy to understand explanation you can read this!
If you want to optimize your code, you can do the calculation like this instead:
radius = 20
if ((playerX - enemyX)**2 + (playerY-enemyY)**2) < (radius**2):
which saves the number of instructions that the computer has to perform because you don't have to use the somewhat costly sqrt() function!