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.
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))
if time.time() >= enemy1time:
enemy1.append( Enemy1( 100, 500 ) )
enemy1time = time.time() + 13
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:
What I have tried:
Tutorials, asking on stack-overflow. None have worked