# Trying to make a SpaceInvaders-esque game and running into some problems, would appreciate help!

Hello, in school I have an IT class and am pretty new to programming, I can pretty much only do the basics. In any case, I’m making a Space Invaders-esque game and have a pretty annoying problem. Here is the (I think) relevant code (I apologize as the variables are in German but I hope one can understand it):

``````for(int a=0; a<=399; a++){
if(sq(x1-AstX[a]) + sq(y1-AstY[a])<100 || sq(x2-AstX[a]) + sq(y2-AstY[a])<100 || sq(x3-AstX[a]) + sq(y3-AstY[a])<100){
getroffen=true;
}
for(int t=0; t<=5; t++){
if(sq(SchussX[t]-AstX[a]) + sq(SchussY[t]-AstY[a])<100 && schussanzahl<6){
asteroid[a]=false;
Schuss[t]=false;
AstX[a]=0;
AstY[a]=0;
SchussX[t]=0;
SchussY[t]=0;
schussanzahl--;
println(schussanzahl);
}
}
}
``````

So the first part of the intertwined loop just says that if an asteroid hits the ship, the game is over. There isn’t a problem here. But in the second part there is a problem. It programs that once a shot hits an asteroid, the asteroid gets destroyed and all the variables get reset. The variable -schussanzahl- measures how many shots have been fired, there is a maximum of five; unless -schussanzahl<6-, one cannot shoot another projectile. So it would only make sense, I thought, that once a shot hits an asteroid, that -schussanzahl- gets reduced since one shot less is being drawn. The problem is that -schussanzahl- gets reduced indefinitely, going into negative infinity. This I don’t understand, since it should only happen if an asteroid is hit by a shot.
The only way around the error that I found is changing the code so the shot travels through the asteroids, however I don’t really want to code the game this way, it would make it too easy in my opinion. Help would be greatly appreciated, and if anyone requires more information, I will gladly comply!

1 Like

The problem is here

``````AstX[a]=0;
AstY[a]=0;
SchussX[t]=0;
SchussY[t]=0;
``````

This reduces the distance to zero so the first part of the if statement would be true and so the shot count will be reduced. Change it so that the distance between then is > 100 e.g.

``````AstX[a]= -1000;
AstY[a]= -1000;
SchussX[t]= 1000;
SchussY[t]=1000;
``````
2 Likes