0

I am programming a little 2D game and ran into the error 'ValueError: list.remove(x): x not in list' multiple times, I tried fixing it but nothing would work. The problem is, when i try to remove the enemy with 'enemies.remove(enemy)', it just show the error i mentioned.

enemy_width, enemy_height = 170, 125
enemy = pygame.Rect(100, 830, enemy_width, enemy_height)
bullets_left = []
bullets_right = []
enemies = []


def handle_bullets(bullets_left, bullets_right, enemies):
    global health_enemy
    for bullet in bullets_left:
        if not(bullet.colliderect(enemy)):
            bullet.x -= BULLET_VEL
        if bullet.x < 0:
            bullets_left.remove(bullet)
        if bullet.colliderect(enemy):
            if health_enemy > 0:
                health_enemy -= 10
                bullets_left.remove(bullet)
            elif health_enemy == 0:
                enemies.remove(enemy)
                pygame.display.update()

    for bullet in bullets_right:
        if not(bullet.colliderect(enemy)):
            bullet.x += BULLET_VEL
        if bullet.x > WIDTH:
            bullets_right.remove(bullet)
        if bullet.colliderect(enemy):
            if health_enemy > 0:
                health_enemy -= 10
                bullets_right.remove(bullet)
            elif health_enemy == 0:
                enemies.remove(enemy)
                pygame.display.update()

main():
  enemies.append(enemy)
  handle_bullets(bullets_left, bullets_right, enemies)
Taminator
  • 7
  • 1
  • The code you have posted cannot compile, `def main():` is needed, your indentation is incorrect throughout, `BULLET_VEL`, `WIDTH` etc are not defined. Please make your code compile. – JamesT Oct 08 '22 at 21:14

0 Answers0