I'm Creating a Game where the player have to kill 6 similar enemies using bullets. So i tried to make a list that contains the 6 enemies called enemies,when i run the game everything work fine but when the bullets hit enemies i get the error
if bullet.y - bullet.radius < enemies[i].hitbox[1] + enemies[i].hitbox[0] and bullet.y + bullet.radius > enemies[i].hitbox[1]: IndexError: list index out of range
i don't understand because i use the len(enemies) as a range in the loop so there is no out of the range ... please help me this is the sections of code that matter
enemies = [Enemy(random.randint(0,424),424,False,False) for i in range(6)]
def redrawGameWindow():
global counter
win.blit(bg, (0,0))
win.blit(warrior,(player1.x,player1.y))
Player.redrawplayer1(player1)
for i in range(len(enemies)):
Enemy.drawenemy(enemies[i])
for bullet in bullets :
Projectile.drawprojectile(bullet)
pygame.display.update()
while run:
for i in range(len(enemies)):
for bullet in bullets :
if bullet.y - bullet.radius < enemies[i].hitbox[1] + enemies[i].hitbox[0] and bullet.y + bullet.radius > enemies[i].hitbox[1]:
if bullet.x + bullet.radius > enemies[i].hitbox[0] and bullet.x - bullet.radius < enemies[i].hitbox[0] + enemies[i].hitbox[2]:
bullets.pop(bullets.index(bullet))
enemies[i].hit()
def hit(self):
self.visible = False
enemies.remove(self)
print("hit")