I’m currently trying to develop a game using pygame and I’m having some problems with some of my lists. Quite simple really, I want the shot to be deleted when going outside of the screen. My current code works perfect till I shoot more than one.
Current code:
#ManageShots
for i in range (len(ShotArray)):
ShotArray[i].x += 10
windowSurface.blit(ShotImage, ShotArray[i])
if(ShotArray[i].x > WINDOWWIDTH):
ShotArray.pop(i)
Error message:
ShotArray[i].x += 10
IndexError: list index out of range
Popping an item from the list moves everything after that item up one place. Thus, you end up with an index
ithat is easily outside of the range.Remove items from your list after the loop, or loop over the list in reverse: