So I'm trying to re-create a spaceInvaders game with python. Here is where my aliens are stored:
aliens = dict()
and each time we increase the game level then a new alien is added with it's id as a key and his positions and health(each alien has 2 health and dies after two shots which deal one damage each) as his value, so the aliens dictionary can look like that:
aliens = {
0: [(100, 50), 2],
1: [(50, 200), 1],
etc...
}
now each time an alien dies I set it's health to -1
and set his positions to (-100, -100)
to put him out of the view, now the problem occurs when I call a cleanAlien()
function which iterates through the alien array and pops every alien that has a health equal to -1
or an out of bounds position, here is that function:
def cleanAliens():
global aliens
for i in range(len(aliens)):
if(aliens[i][0] == (-100, -100) or aliens[i][1] < 0):
aliens.pop(i);
i -= i
However when I try to run that I get an error:
if(aliens[i][0] == (-100, -100) or aliens[i][1] < 0):
KeyError: 0
Any ideas?
Thanks in advance.