0
\$\begingroup\$

I am trying to make my enemies shoot at my player rect where ever the position the player is and when it collides with the rect it should remove the -1 health ***my problem is that when the enemy shoots its bullet dont attack the player it just hoover over its head

        for shootss in shootsright:
            if shootss.x < 1000 and shootss.x > 0:
                if shootss.x < playerman.rect.x:
                    shootss.x += 7
                else:
                    shootss.x -= 7
            else:
                shootsright.pop(shootsright.index(shootss))
        if len(shootsright) < 2:
            shootsright.append(Bools(round(enemyshoots1.x+enemyshoots1.width-107),round(enemyshoots1.y + enemyshoots1.height-50),(0,0,0)))




this part its suppose to remove -1 of the player health when the enemy bullets collides with it

     for shootss in shootsright:
            if playerman.rect.colliderect(shootss.hitbox):
                playerman.health -=1
                shootsright.pop(shootsright.index(shootss))



and this is my enemy class

shotsright = pygame.image.load("shooting2.png")
shotsleft = pygame.image.load("shooting1.png")
class enemyshoot:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y =y
        self.height = height
        self.width = width
        self.color = color
        self.shootsright = pygame.image.load("shooting2.png")
        self.shotsleft = pygame.image.load("shooting1.png")
        self.shootsright = pygame.transform.scale(self.shootsright,(self.shootsright.get_width()//3,self.shootsright.get_height()//3))
        self.shotsleft = pygame.transform.scale(self.shotsleft,(self.shotsleft.get_width()//3,self.shotsleft.get_height()//3))

        self.rect = pygame.Rect(x,y,height,width)
        self.health = 10
        self.hitbox = (self.x + -20, self.y + 30, 31, 57)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        window.blit(self.shootsright,self.rect)
        self.hits = (self.x + 20, self.y, 28,60)
        pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 60, 50, 10)) # NEW
        pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 60, 50 - (5 * (10 - self.health)), 10))
        self.hitbox = (self.x + 60, self.y + 80, 81, 87)

black = (0,0,0)
enemyshoots1 = enemyshoot(1100,240,100,100,black)        
enemyshooting = [enemyshoots1]




my bullet class

# enemys bullets
ksud = pygame.image.load("heart.png")
class Bools(object):
   def __init__(self, x, y,color):
       self.x = x
       self.y = y
       self.ksud = pygame.image.load("heart.png")
       self.hitbox  = self.ksud.get_rect()
       self.rect  = self.ksud.get_rect()
       self.rect.topleft = (self.x,self.y)
       self.speed = 10
       self.color = color
       self.hitbox = (self.x + 57, self.y + 33, 29, 52) # NEW
   def draw(self, window):
        self.rect.topleft = (self.x,self.y)
        player_rect = self.ksud.get_rect(center = self.rect.center) 
        player_rect.centerx += 0 # 10 is just an example
        player_rect.centery += 0 # 15 is just an example
        window.blit(self.ksud, player_rect)
        self.hitbox = (self.x + 97, self.y + 33, 10, 10) # NEW
        window.blit(self.ksud,self.rect)




```
\$\endgroup\$
2
  • \$\begingroup\$ I’m not 100% sure this is the issue, but I find it odd that in the bullet class you set the hitbox to (self.x + 57, self.y + 33, 29, 52) in the init method, but then you set it to (self.x + 97, self.y + 33, 10, 10) in the draw method. \$\endgroup\$ – Ryan1729 Jun 10 '20 at 18:47
  • \$\begingroup\$ that isnt the issue but thanks for letting me know \$\endgroup\$ – Habib Ismail Jun 10 '20 at 19:35

Your Answer

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

Browse other questions tagged or ask your own question.