Problem making dissapair the Space Invaders enemys

Hello there!

Im making a space invaders games, but im stuck trying to make dissapear the enemys of an a Array when they collision with the bullet, because i dont know how to number every enemy in the array to disapear. Any help is welcome :slight_smile:



/////Booleans/////
boolean recarrega;
boolean dreta;
boolean esquerra;
boolean tocat;


/////////////

//variables//
int x = 400;
int speed = 10;
int y = 600;
int x1 = 50;
int y1 = 50;
int vel = 1;
PImage img;
PImage img1;
PImage img2;
PImage img3;
PImage img4;
PImage imici;
PImage fons;
PImage bala;
PImage over;
////////


/////Bolean del inici /////////////
boolean inici;
boolean GMO;

//////////////////////////////////////

//////////////////SHOTS///////////////////
class bala {

int posBx;
int posBy;

bala (int X,int Y){
posBx= X;
posBy= Y;}

void dibuixa(){
posBy = posBy - 5;
image (bala,posBx,posBy);

if (posBy <= 0){
  recarrega = false;
}
if (tocat == true){
recarrega = false;
}

  if  (recarrega == false){
  posBy=710;
  posBx = x;
  tocat = false;
}

}

void actualitza(){
if (dreta == true){
  if  (recarrega == false){
posBy=710;
  posBx = x;
}
}
if (esquerra == true){
  if  (recarrega == false){
posBy=710;
  posBx = x;
}
}



}

int posi(){
return posBx;



}
int posy(){
return posBy;



}
}


/////////////////////////////

//arrays ENEMYS////
enemic[] enemicarray;
enemic[] enemi1carray;
enemic[] enemi2carray;
enemic[] enemi3carray;
///////////////////








//setup////
void setup(){
 size(700,800);
 inici = false;
 recarrega = false;
 GMO = false;
 tocat = false;
 
 
 
 
 /////LIST OF IMAGES/////////
       img= loadImage("Pro-enemic.png");
       img1 = loadImage("Pro-enemic2.png");
       img2 = loadImage("Pro-enemic3.png");
       img3 = loadImage("Pro-enemic4.png");
       img4 =  loadImage("Pro-Personatge.png");
       imici = loadImage("Pro_Inici.png");
       fons = loadImage ("fons.png");
       bala = loadImage ("Pro-Bala.png");
       over = loadImage ("GAME-OVER.png");
 
 
 /////////////////////////////////
 
 
 
 ////ARRAY DEFINE/////
 enemicarray = new enemic [6];
 enemi1carray = new enemic [6];
 enemi2carray = new enemic [6];
 enemi3carray = new enemic [6];
 ///////////////////////
   
   
   
   
   
 ////DRAW ENEMYS/////
 for(int i = 0; i<6; i++){
    enemicarray[i]= new enemic(i*100+160,100,100);
  }
   for(int i = 0; i<6; i++){
    enemi1carray[i]= new enemic(i*100+160,150,100);
  }
   for(int i = 0; i<6; i++){
    enemi2carray[i]= new enemic(i*100+160,200,100);
  }
   for(int i = 0; i<6; i++){
    enemi3carray[i]= new enemic(i*100+160,250,100);
  }
 /////////////////
 
////KEYS FOR MOVEMENT/////
}
void keyPressed(){
  if(key == CODED){
    if (keyCode == RIGHT)
    
    {dreta=true;}
    
    if (keyCode == LEFT)
    {esquerra = true;}
  
  }    
if (keyCode == ' ')
if (inici == true){ 
    if (recarrega == false){
         recarrega = true;}
         
    
} 


if (inici==false){
if (key == 'e')
 inici=true; }
 

  }



  ////////////
  void keyReleased(){
   if(key == CODED){
    if (keyCode == RIGHT){
    {dreta=false;}}
    if (keyCode == LEFT){
    {esquerra = false;}}
  }
   
  

    
}
//////////////////////////////////////////////////////////    

bala b = new bala(x,710);
/////void draw//////////
void draw(){
  
  image (fons,350,400);
  if (inici==false){
image (imici,00,0);


}
  

    
 //////////////////////////////////   
    
    
    
    
    
 ///// DRAW ENEMYS//////////   
 if (inici==true){
   
   for(int  i= 0; i<6; i++){
   if (enemicarray[i].posi()+25 > b.posi() && enemicarray[i].posi()-25 < b.posi())
   if (enemicarray[i].posy()+25 > b.posy() && enemicarray[i].posy()-25 < b.posy())
   {tocat=true;
         }
 }
 for(int  i= 0; i<6; i++){
   if (enemi1carray[i].posi()+25 > b.posi() && enemi1carray[i].posi()-25 < b.posi())
   if (enemi1carray[i].posy()+25 > b.posy() && enemi1carray[i].posy()-25 < b.posy())
   {tocat=true;
            
   }
 }
 for(int  i= 0; i<6; i++){
   if (enemi2carray[i].posi()+25 > b.posi() && enemi2carray[i].posi()-25 < b.posi())
   if (enemi2carray[i].posy()+25 > b.posy() && enemi2carray[i].posy()-25 < b.posy())
   {tocat=true;
            
   }
 }
 for(int  i= 0; i<6; i++){
   if (enemi3carray[i].posi()+25 > b.posi() && enemi3carray[i].posi()-25 < b.posi())
   if (enemi3carray[i].posy()+25 > b.posy() && enemi3carray[i].posy()-25 < b.posy())
   {tocat=true;
            
   }
 }
   
   
   
   
   
   
   
    for (int contador = 0; contador < 6; contador++){
    enemicarray[contador].visualitzador();
    enemi1carray[contador].visualitzador1();
    enemi2carray[contador].visualitzador2();
    enemi3carray[contador].visualitzador3();
    enemicarray[contador].moviment();
    enemi1carray[contador].moviment();
    enemi2carray[contador].moviment();
    enemi3carray[contador].moviment(); 
    
    }
    ////////////CHARACTER//////////////
    if (dreta==true){
      if (x + speed <= 660){
    x= x + speed;
    b.actualitza();
      }
    }
    
    if (esquerra==true){
      if (x - speed >= 40){
    x= x - speed;
    b.actualitza();
      }}
     image(img4,x,740);
     fill(14,14,74);
     stroke(0,255,0);
    strokeWeight(5);
    ////////////////////////
    if (recarrega == true){

b.dibuixa();
}


    //MOVEMENT OF ENEMYS//
    for(int contador = 0; contador<6; contador++){
    if(enemicarray[contador].x1+25 > 690 || enemicarray[contador].x1-25<10){
      for(int j=0; j < 6; j++){
        enemicarray[j].update();
      }
    }
  }
  for(int contador = 0; contador<6; contador++){
    if(enemi1carray[contador].x1+25 > 690 || enemi1carray[contador].x1-25<10){
      for(int j=0; j < 6; j++){
        enemi1carray[j].update();
      }
    }
  }
  for(int contador = 0; contador<6; contador++){
    if(enemi2carray[contador].x1+25 > 690 || enemi2carray[contador].x1-25<10){
      for(int j=0; j < 6; j++){
        enemi2carray[j].update();
      }
    }
  }
  for(int contador = 0; contador<6; contador++){
    if(enemi3carray[contador].x1+25 > 690 || enemi3carray[contador].x1-25<10){
      for(int j=0; j < 6; j++){
        enemi3carray[j].update();
      }
    }
  }
  for(int i = 0; i < 6; i++){
if(enemicarray[i].y1>690 || enemi1carray[i].y1>690 || enemi2carray[i].y1>690 ||
   enemi3carray[i].y1>690 ){
     GMO =true;
     
  
}
}
  
  
}
////GAME OVER////////////////
if (GMO==true){
image(over,350,400);
}

}
   ////////////////////////////







///// CLASS ENEMYS //////
class enemic{
  int x1, y1, dx,vel;
  PImage img;
  PImage img1;
  PImage img2;
  PImage img3;
  
      enemic(int startX, int startY, int startSpeed){
       x1 =  startX;
       y1 =  startY;
       dx = 1;
       vel = startSpeed;
       img = loadImage("Pro-enemic.png");
       img1 = loadImage("Pro-enemic2.png");
       img2 = loadImage("Pro-enemic3.png");
       img3 = loadImage("Pro-enemic4.png");
       
      }
        
      ///////////////
      
      
      
      
      
      
      
//ENEMYS MOVEMENTS//
void moviment(){
    x1 = x1 + dx;
}

  void esquerra(){
    x1 = x1 - vel;
  }

  void dreta(){
    x1 = x1 + vel;
  }

  void update(){
    dx = dx * -1;
    y1+=25;
  }
    int posi(){
    return x1;
 
  }
    int posy(){
    return y1;
 
  }
  
  
////////////TO SEE THE ENEMYS////////
     void visualitzador(){
       imageMode(CENTER);
       image(img,x1,y1,50,50);
  }
  void visualitzador1(){
       imageMode(CENTER);
       image(img1,x1,y1,50,50);
  }
  void visualitzador2(){
       imageMode(CENTER);
       image(img2,x1,y1,50,50);
  }
  void visualitzador3(){
       imageMode(CENTER);
       image(img3,x1,y1,50,50);
  }


}
1 Like

Your code has some problems.

First, move the PImage variables out of the Enemy class. You only need three images total - not three images for each enemy! This also means you will have to move your calls to loadImage() - move them to your setup() function.

Next, make better use of the Enemy class! Add two variables to it. The first should be a boolean that determines if that enemy is still alive and thus should be drawn. The second is a number that represents the type of your enemy. Make some changes to your Enemy constructor function: Pass the value of type in as a parameter, and set the alive variable to true.

class Enemy {
  int x1, y1, dx, vel;
  boolean alive;
  int type;
  Enemy(int startX, int startY, int startSpeed; int initial_type ){
    x1 =  startX;
    y1 =  startY;
    dx = 1;
    vel = startSpeed;
    alive = true;
    type = initial_type;
  }

Then you can make better use of these variables to have your enemy class draw itself:

void draw_me(){
  if( !alive ){ return; } // Dead Enemies are not drawn.
  if( type == 0 ){
    image( images[0], x1, y1 );
  } else if( type == 1 ){
    image( images[1], x1, y1 );
  } else /// etc...
  // ... } as nedeed
} // end draw_me()

Or, more simply:

void draw_me(){
  if( alive ) image( images[type], x1, y1 );
}

You can also use your class to determine things about a single enemy:

void was_i_hit(){
  for( each bullet ){
    if( that bullet collided with me ){
      alive = false;
    }
  }
}

The idea is that you move any and all logic that deals with your enemy into the class, so that it can maintain itself without you needing to worry about the details of it in your main body code.

Also, notice that because the type of enemy is now inside the class, you only really need ONE ARRAY of enemies!

2 Likes