1
\$\begingroup\$

I have enemies that move left and right. I was wonder how I could make my particles show up when the enemy is killed?

Here is my particle class

    class explode:
        
        def __init__(self,x,y,height,width,color):
            self.x = x
            self.y = y
            self.height = height
            self.width = width
            self.explode = [
            pygame.image.load("spark_01.png"),
            pygame.image.load("spark_02.png"),
            pygame.image.load("spark_03.png"),
            pygame.image.load("spark_04.png"),
            pygame.image.load("spark_05.png"),
            pygame.image.load("spark_06.png"),
            pygame.image.load("spark_07.png")]

            self.explode = [pygame.transform.scale(image,(image.get_width()//7,image.get_height()//7)) for image in self.explode]

            self.rect = pygame.Rect(x,y,height,width)
            self.direction = "blobright"
            self.anim_index = 0
        def draw(self):
             
            self.rect.topleft = (self.x,self.y)
            if self.direction == "blobright":
                window.blit(self.explode[self.anim_index], self.rect)
                self.anim_index += 1
                if self.anim_index == len(self.explode):
                    self.anim_index = 0
            



    black = (0,0,0)
    explode1 = explode(400,450,50,50,black)
    explodes = [explode1]

This is where I delete my enemies when they are killed. How can I add my particles after it's deleted and remove the particles afterward?


        #--------------------------------------------------------------------------------------------------------- enemys

                # enemys 2
            for enemys in enemying:
                for bullet in bullets:
                    if bullet.rect.colliderect(enemys.hitbox):
                        if enemys.health > -4:
                            enemys.health -= 1
                            minusenemyhealthtext()
                            bullets.pop(bullets.index(bullet))
                        else:
                            enemyscore += 1
                            del enemying[one]
                            enemyors = font.render("" + str(enemyscore), True, (0,0,0))
                            enemydors.center = ((130,125))



\$\endgroup\$
0
\$\begingroup\$

What I've done is add a delete method to the Enemy class, in this method you will create the explosion using the enemy's position, and call it just before deleting the enemy.

\$\endgroup\$

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.