2

I am trying to make my bullets collide with my enemy and then dis-pear but it just passes throw them for some reason

    for bullet in bullets[:]:
        if bullet.y + bullet.x < enemys1.hitbox[1] + enemys1.hitbox[3] and bullet.y + bullet.x > enemys1.hitbox[1]:
            if bullet.x - bullet.y > enemys1.hitbox[0] and bullet.x - bullet.y < enemys1.hitbox[0] + enemys1.hitbox[2]:
                enemys1.hit()
                bullets.pop(bullets.index(bullet))

this is my projectile class


class projectile(object):
   def __init__(self, x, y, dirx, diry, color):
       self.x = x
       self.y = y
       self.dirx = dirx
       self.diry = diry
       self.slash = pygame.image.load("heart.png")
       self.rect  = self.slash.get_rect()
       self.rect.topleft = ( self.x, self.y )
       self.speed = 10
       self.color = color

   def move(self):
       self.x += self.dirx * self.speed
       self.y += self.diry * self.speed

   def draw(self, window):
       self.rect.topleft = (round(self.x), round(self.y))

       window.blit(slash, self.rect)


any help is appreciated Thanks!

Habib Ismail
  • 69
  • 5
  • 16
  • 3
    What do the conditions `bullet.y + bullet.x < enemys1.hitbox[1] + enemys1.hitbox[3] ` and `bullet.y + bullet.x > enemys1.hitbox[1]` are supposed to mean, in plain language ? (same with the next `if`, it's not very obvious at first sight) – Pac0 May 30 '20 at 00:35
  • they both check if the objects have collided the x and y coordinate of the bullets sit inside the hitbox of the enemy – Habib Ismail May 30 '20 at 01:00
  • and if they did then we are going to pop the bullets – Habib Ismail May 30 '20 at 01:00
  • but it seems like they are just moving throw the enemy without poping nor colliding – Habib Ismail May 30 '20 at 01:01
  • 1
    I'm not sure that the sum or the diffference between `x` and `y` coordinates is doing what you are describing. Take a pencil and a paper, and draw the situation, this will help you. Maybe it makes sense, but then please share the drawing with us. – Pac0 May 30 '20 at 02:10
  • 1
    to be clear, doing any calculations between x and y doesn't 'smell good'. There should be differences between different x coordinates together. Or between y cooridnates together. But not between x and y. Or maybe you are not using x and y as I suppose (x for horizontal, y for vertical) – Pac0 May 30 '20 at 02:20
  • 1
    thank you for clearing things out I figured something else I said ` for bullet in bullets: if bullet.rect.colliderect(enemys1.hitbox): bullets.pop(bullets.index(bullet)) ` – Habib Ismail May 30 '20 at 03:04
  • That sounds both much more correct, and more easily readable indeed. – Pac0 May 30 '20 at 07:34

1 Answers1

2

I recommend to use a pygame.Rect object for the hitbox and colliderect() to find the intersection:

for bullet in bullets[:]:
    hit_rect = pygame.Rect(*enemys1.hitbox)
    if bullet.rect.colliderect(hit_rect):
        enemys1.hit()

Rabbid76
  • 202,892
  • 27
  • 131
  • 174