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)

  • 2
    \$\begingroup\$ 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? \$\endgroup\$ May 2 '13 at 17:00
  • \$\begingroup\$ By the way if you want to see what I mean you can download it at the games website Cagentdog.net/CRPG (Many Many Many Bugs!) \$\endgroup\$ May 2 '13 at 18:09

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!

  • \$\begingroup\$ It's top down... how does that work \$\endgroup\$ May 2 '13 at 17:17
  • \$\begingroup\$ @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. \$\endgroup\$ May 2 '13 at 17:33
  • \$\begingroup\$ @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 \$\endgroup\$ May 2 '13 at 18:37
  • 1
    \$\begingroup\$ @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? \$\endgroup\$ May 2 '13 at 18:53
  • 1
    \$\begingroup\$ @Zolomon I love your sad face enemy. +1 :P \$\endgroup\$ May 2 '13 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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