Click here to Skip to main content
15,848,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Currently I am creating a top down shooter in Pygame and need the enemy to be removed after 1 second if its health is equal to 1. I have tried a tonne of different codes and tutorials but none of which have been helpful. I also need the enemies hitbox to be removed as-well so the player cant shoot the enemy when it is dying in that 1 second. What I mean by removing the enemy is after the enemy has been appended from the enemy1 list it is removed from the list when on 1 health after 1 second. Here is a bit of my code.

Python
# Code
class Enemy1(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.hit_box = (self.x-10, self.y -10, 70, 70)
        self.animation_images =[pygame.image.load("Enemy1_animation_0.png"), pygame.image.load("Enemy1_animation_1.png"),
        pygame.image.load("Enemy1_animation_2.png"), pygame.image.load("Enemy1_animation_3.png")]

        self.sprites = [pygame.image.load('explosion1.png'), pygame.image.load('explosion2.png'),  pygame.image.load('explosion3.png'), pygame.image.load('explosion4.png'), pygame.image.load('explosion5.png'), pygame.image.load('explosion6.png')]
        self.current_sprite = 0



        self.animation_count = 0
        self.reset_offset = 0
        self.offset_x = random.randrange(-150, 150)
        self.offset_y = random.randrange(-150, 150)
        self.health = 5

    def main(self, display):
        all_sprites = self.sprites
        if self.animation_count + 1 == 16:
            self.animation_count = 0
        self.animation_count += 1

        if self.current_sprite + 1 == 16:
            self.current_sprite = 0
        self.current_sprite += 1


        if self.reset_offset == 0:
            self.offset_x = random.randrange(-150, 150)
            self.offset_y = random.randrange(-150, 150)
            self.reset_offset = random.randrange(120, 150)
        else:
            self.reset_offset -= 1

        if player.x + self.offset_x > self.x-display_scroll[0]:
            self.x += 1
        elif player.x + self.offset_x < self.x-display_scroll[0]:
            self.x -= 1

        if player.y + self.offset_y > self.y-display_scroll[1]:
            self.y += 1
        elif player.y + self.offset_y < self.y-display_scroll[1]:
            self.y -= 1

        if self.health > 1:
            display.blit(pygame.transform.scale(self.animation_images[self.animation_count//4], (50, 50)), (self.x-display_scroll[0], self.y-display_scroll[1]))
        if self.health == 1:
            display.blit(pygame.transform.scale(self.sprites[self.current_sprite//4], (50, 50)), (self.x-display_scroll[0], self.y-display_scroll[1]))

        self.hit_box = (self.x-display_scroll[0]-10, self.y-display_scroll[1]-10, 70, 70)


enemy1 = [Enemy1(300, 400)]

while True:
    display.fill((0, 0, 0))

#Spawn enemy

if time.time() >= enemy1time:
    enemy1.append( Enemy1( 100, 500 ) )
    enemy1time = time.time() + 13


#Enemy lose health and meant to be removed

for enem in enemy1:
        for bullet in player_bullets:
            if pygame.Rect(enem.hit_box).collidepoint(bullet.x, bullet.y):
                player_bullets.remove(bullet)
                enem.health -= 1

                if enem.health == 1:
                    #After 1 second remove the enemy and also remove hitbox


What I have tried:

Tutorials, asking on stack-overflow. None have worked
Posted
Updated 31-Aug-22 15:54pm
v2

1 solution

Sigh... OK. If you want the enemy to disappear after 1 second, you need to track the enemies state, all the way up to the moment the enemy is to disappear. You cannot "delete after 1 second". There is no command to do that.

So, you need to sit down and think about the state of an enemy across it's entire lifespan, from the moment the enemy is spawned (and added to your list of enemies, to the moment it's removed from the enemies list.

In your case, the last state of the enemy is going to be "Dead", and that's where your drawing code for the enemy has to draw something that signifies that state. You're also going to need to track how long the enemy has been in that state, and after 1 second, tear down that enemy object and remove it from the enemies list.

You're not tracking either state or time since that state was changed. All you are tracking is health, and that's not enough.

Welcome to state machines...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900