Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been working on the attacking in my RPG. The problem I have recently encountered is, I have to be in Exactly the same x coordinate as the Enemy. When I want to attack the enemy this is what happens:


    elif event.key == pygame.K_SPACE: 
                    attack = True

if attack == True:
    if enx == x: #this is the line my qeustion is about
        endead = True

attack = False # I put this in so attack doesn't stay true forever   


if endead == False:
            screen.blit(Enemy, [enx, eny])

How do I make it work if the enemy's position (enx) is equal to within a certain number of pixels to the player's position (x)?

So if I'm close but not Exactly the same place it works

(I know this only takes the x coords into account I'll add y later)

share|improve this question
Your title includes this text: "(How to say + or - in python)". Plus-or-minus is another name people give to a specific mathematical function. Do you know what the name of that function is? – michael.bartnett May 2 '13 at 17:00
By the way if you want to see what I mean you can download it at the games website (Many Many Many Bugs!) – Cnorwood7641 May 2 '13 at 18:09
up vote 4 down vote accepted

You are probably looking for the absolute value!

You can implement it like this:

def abs(value):
    if value < 0:
        return -value
        return value

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:

Player distance to enemy inside of a radius

The calculation 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!

share|improve this answer
It's top down... how does that work – Cnorwood7641 May 2 '13 at 17:17
@Cagentdog you are concerned if enx is within range of x correct? you want to follow Zolomon's second reference replacing player and enemy position with your values, and radius with the range you want to check. – UnderscoreZero May 2 '13 at 17:33
@UnderscoreZero Do I need to import anything? I get an error... ` if value < 0: return -value elif: # I get an error on this line... ?? return value – Cnorwood7641 May 2 '13 at 18:37
@Zolomon Maybe add a bit to your answer about how you can save on the cost of a sqrt call by comparing against the square of the radius? – michael.bartnett May 2 '13 at 18:53
@Zolomon I love your sad face enemy. +1 :P – UnderscoreZero May 2 '13 at 20:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.