So I have a small game written in python, if you saw my previous question you know it is a “space invaders” clone.
So nearly everything is running smoothly right now except for a random error popping out once in a while. This is completely random, it can occur after some bullets have been fired, or it may not occur at all.
I have this code:
for bullet in bullets:
bullet.attack()
if bullet.posy<=-20:
bullet_draw=False
if bullet_draw==True:
bullet.draw()
for enemy in enemies:
if bullet.sprite.rect.colliderect(enemy.sprite.rect):
enemy.health-=1
bullets.remove(bullet)
bullet_draw=False
else:
bullet_draw=True
Sometimes it gives me the following error.
Traceback (most recent call last):
File "\Programming\space invaders\space.py", line 280, in <module>
bullets.remove(bullet)
ValueError: list.remove(x): x not in list
Please note that this error is completely random; even if it is not, I cannot trace its origin though. Any help as to how to eliminate it?
Your bullet is hitting multiple enemies. You need to
breakout of theenemiesloop.