Help game code question

I am working on a game in processing for my university project but I have a problem. when the game patterns change their positions wont reset. if you let the pattern loop you will see what I mean can anyone help me?

code:

float X, Y, Xc, Yc, Xm, Ym, Xl, Xr, Yt, Yb, Xm2, Ym2;
float H1, H2, H3, H1x, H1y, Ev;
float Hbar=3;
float Pdamx=100, pdamy=100;
color H1G, H1B;
int EM =1;
int ap =3;


boss1 B1;
enemy em1;
enemy em2;
enemy em3;
enemy em4;
enemy em5;
enemy em6;
enemy em7;
enemy em8;
enemy e1m8;
endenemy eb1;

upenemy uem1;
upenemy uem2;
upenemy uem3;
upenemy uem4;
upenemy uem5;
upenemy uem6;
upendenemy umd;

enemy2 em9;
enemy2 em10;
enemy2 em11;
enemy2 em12;
enemy2 em13;
enemy2 em14;
enemy2 em15;
enemy2 em16;
enemy2 em17;
enemy2 em18;
enemy2 em19;
enemy2 em20;
endenemy eb2;

void setup() {
  //pattern 1
  pushMatrix();
  em1 = new enemy(color(255, 0, 0), width, 300, -10);
  em2 = new enemy(color(255, 0, 0), width, 700, -10);
  em3 = new enemy(color(255, 0, 0), width+400, 500, -10);
  em4 = new enemy(color(255, 0, 0), width+800, 300, -10);
  em5 = new enemy(color(255, 0, 0), width+800, 700, -10);
  em6 = new enemy(color(255, 0, 0), width+1200, 500, -10);
  em7 = new enemy(color(255, 0, 0), width+1600, 700, -10);
  em8 = new enemy(color(255, 0, 0), width+1600, 300, -10);
    e1m8 = new enemy(color(255, 0, 0), width+1900, 500, -10);
  eb1 = new endenemy(color(0, 255, 0), width+1900, 500, -10);
popMatrix();
pushMatrix();
//pattern 2
uem1 = new upenemy(color(255, 0, 0),300,0, +10);
uem2 = new upenemy(color(255, 0, 0),700,0, +10);
uem3 = new upenemy(color(255, 0, 0),500,-300, +10);
uem4 = new upenemy(color(255, 0, 0),700,-600, +10);
uem5 = new upenemy(color(255, 0, 0),300,-600, +10);
uem6 = new upenemy(color(255, 0, 0),500,-900, +10);
umd = new upendenemy(color(0, 255, 0),500,-1800, +10);
 popMatrix();
 pushMatrix();
 //pattern 3
   em9 = new enemy2(color(255, 0, 0), width-500, 300, -10);
   em10 = new enemy2(color(255, 0, 0), width-500, 500, -10);
   em11 = new enemy2(color(255, 0, 0), width+500, 500, -10);
   em12 = new enemy2(color(255, 0, 0), width+500, 300, -10);
   em13 = new enemy2(color(255, 0, 0), width+1000, 500, -10);
   em14 = new enemy2(color(255, 0, 0), width+1000, 700, -10);
   em15 = new enemy2(color(255, 0, 0), width+1500, 500, -10);
   em16 = new enemy2(color(255, 0, 0), width+1500, 300, -10);
      em17 = new enemy2(color(255, 0, 0), width, 500, -10);
   em18 = new enemy2(color(255, 0, 0), width, 700, -10);
         em19 = new enemy2(color(255, 0, 0), width+2000, 500, -10);
   em20 = new enemy2(color(255, 0, 0), width+2000, 700, -10);
   eb2 = new endenemy(color(0, 255, 0), width+2000, 500, -10);
popMatrix();
   
  B1 = new boss1();
  H1G= color(255, 0, 0, 255);
  H1B = color(0, 0, 0, 0);
  Ev=255;
  size(2000, 1000);
  X=2;
  Y=2;
  Yc=500;
  Xc=500;
  Xl=300;
  Xr=700;
  Yt=700;
  Yb=300;
}

void draw() {

  background(150);
  rectMode(CENTER);
  if (X==2) {
    Xm=Xc;
  }
  if (X==1) {
    Xm=Xl;
  }
  if (X==3) {
    Xm=Xr;
  }
  if (Y==2) {
    Ym=Yc;
  }
  if (Y==1) {
    Ym=Yb;
  }
  if (Y==3) {
    Ym=Yt;
  }

  //health
  print(ap);
  if (Hbar==3) {
    fill(H1G);
    rect(25, 25, 50, 50);
  }
  if (Hbar==2) {
    fill(0, 255, 0);
    rect(25, 25, 50, 50);
  }
  if (Hbar==1) {
    fill(255, 150, 255);
    rect(25, 25, 50, 50);
  }
  if (Hbar==0) {
    fill(0);
    rect(25, 25, 50, 50);
  }


  //board
  fill(255);
  rect(500, 500, 200, 200);
  rect(700, 500, 200, 200);
  rect(500, 700, 200, 200);
  rect(700, 700, 200, 200);
  rect(500, 300, 200, 200);
  rect(300, 500, 200, 200);
  rect(300, 300, 200, 200);
  rect(700, 300, 200, 200);
  rect(300, 700, 200, 200);
  //boss board
  fill(100, 0, 0);
  rect(1500, 500, 200, 200);
  rect(1700, 500, 200, 200);
  rect(1500, 700, 200, 200);
  rect(1700, 700, 200, 200);
  rect(1500, 300, 200, 200);
  rect(1300, 500, 200, 200);
  rect(1300, 300, 200, 200);
  rect(1700, 300, 200, 200);
  rect(1300, 700, 200, 200);
  //player
  fill(0);
  rect(Xm, Ym, 100, 100);

  //enamy




  if (ap==1) {
    //pattern 1
    em1.drive();
    em1.display();
    em1.hit();
    em2.drive();
    em2.display();
    em2.hit();
    em3.drive();
    em3.display();
    em3.hit();
    em4.drive();
    em4.display();
    em4.hit();
    em5.drive();
    em5.display();
    em5.hit();
    em6.drive();
    em6.display();
    em6.hit();
    em7.drive();
    em7.display();
    em7.hit();
    em8.drive();
    em8.display();
    em8.hit();
    e1m8.drive();
    e1m8.display();
    e1m8.hit();
      eb1.drive();
  eb1.display();
  eb1.hit();
  }
if(ap==2){
  //pattern 2
    uem1.drive();
    uem1.display();
    uem1.hit();
    uem2.drive();
    uem2.display();
    uem2.hit();
    uem3.drive();
    uem3.display();
    uem3.hit();
    uem4.drive();
    uem4.display();
    uem4.hit();
    uem5.drive();
    uem5.display();
    uem5.hit();
    uem6.drive();
    uem6.display();
    uem6.hit();
    umd.drive();
    umd.display();
    umd.hit();
}
if(ap==3){
  //pattern 3
    em9.drive();
    em9.display();
    em9.hit();
    em10.drive();
    em10.display();
    em10.hit();
    em11.drive();
    em11.display();
    em11.hit();
    em12.drive();
    em12.display();
    em12.hit();
    em13.drive();
    em13.display();
    em13.hit();
    em14.drive();
    em14.display();
    em14.hit();
    em15.drive();
    em15.display();
    em15.hit();
    em16.drive();
    em16.display();
    em16.hit();
    em17.drive();
    em17.display();
    em17.hit();
    em18.drive();
    em18.display();
    em18.hit();
    em19.drive();
    em19.display();
    em19.hit();
    em20.drive();
    em20.display();
    em20.hit();
eb2.drive();
  eb2.display();
  eb2.hit();

}
  }

//movement
void keyReleased() {
  if (key == 'a') {
    X=X-1;
  }
  if (key == 'd') {
    X=X+1;
  }
  if (key == 'w') {
    Y=Y-1;
  }
  if (key == 's') {
    Y=Y+1;
  }
}
void pattern1() {
}
class boss1{
 float X,Y,Xm,Ym;
 float Rmove;
float Yt=700,Yb=300,Yc=500,Xl=1300,Xr=1700,Xc=1500;
  void move(){
    Xm=random(1,4);
   if(X==2){Xm=Xc;}
 if(X==1){Xm=Xl;}
 if(X==3){Xm=Xr;}
 if(Y==2){Ym=Yc;}
 if(Y==1){Ym=Yb;}
 if(Y==3){Ym=Yt;}
  }
  void hit(){
    
  }
  void display(){
  rect(Xm,Ym,100,100);
  }
}
class enemy { 
  color c;
  float xpos;
  float ypos;
  float xspeed;
  int boxX=200;
  int boxY=100;
  int thing=0;
  float rectX =xpos-boxX/2;
    float rectY =ypos-boxY/2;
  // The Constructor is defined with arguments.
  enemy(color tempC, float tempXpos, float tempYpos, float tempXspeed) { 
    c = tempC;
    xpos = tempXpos;
    ypos = tempYpos;
    xspeed = tempXspeed;
  }

  void display() {
    stroke(0);
    fill(c);
    rectMode(CENTER);
    rect(xpos,ypos,boxX,boxY);
  }

  void drive() {
    xpos = xpos + xspeed;
    if (xpos <0-200) {
      xpos = width;
      thing=0;
    }
  }
  void hit(){
   //if(xpos==Xm&&ypos==Ym){
    //Hbar=Hbar-1; 
    //xpos=0;
    //xspeed=0;
   //}
      if (Xm > rectX+xpos && Xm < rectX+xpos + boxX && Ym > rectY+ypos && Ym < rectY+ypos + boxY && thing==0) {
thing=1;

    Hbar=Hbar-1; 
    //xpos=width;
    //xspeed=0;

   }
      
  }
}
class enemy2 { 
  color c;
  float xpos;
  float ypos;
  float xspeed;
  int boxX=200;
  int boxY=100;
  int thing=0;
  float rectX =xpos-boxX/2;
    float rectY =ypos-boxY/2;
  // The Constructor is defined with arguments.
  enemy2(color tempC, float tempXpos, float tempYpos, float tempXspeed) { 
    c = tempC;
    xpos = tempXpos;
    ypos = tempYpos;
    xspeed = tempXspeed;
  }

  void display() {
    stroke(0);
    fill(c);
    rectMode(CENTER);
    rect(xpos,ypos,boxX,boxY);
  }

  void drive() {
    xpos = xpos + xspeed;
    if (xpos <0-1000) {
      xpos = width;
      thing=0;
    }
  }
  void hit(){
   //if(xpos==Xm&&ypos==Ym){
    //Hbar=Hbar-1; 
    //xpos=0;
    //xspeed=0;
   //}
      if (Xm > rectX+xpos && Xm < rectX+xpos + boxX && Ym > rectY+ypos && Ym < rectY+ypos + boxY && thing==0) {
thing=1;

    Hbar=Hbar-1; 
    //xpos=width;
    //xspeed=0;

   }
      
  }
}

class upenemy { 
  color c;
  float xpos;
  float ypos;
  float yspeed;
  int boxX=100;
  int boxY=150;
  int thing=0;
  float rectX =xpos-boxX/2;
    float rectY =ypos-boxY/2;
  // The Constructor is defined with arguments.
  upenemy(color tempC, float tempXpos, float tempYpos, float tempYspeed) { 
    c = tempC;
    xpos = tempXpos;
    ypos = tempYpos;
    yspeed = tempYspeed;
  }

  void display() {
    stroke(0);
    fill(c);
    rectMode(CENTER);
    rect(xpos,ypos,boxX,boxY);
  }

  void drive() {
    ypos = ypos + yspeed;
    if (ypos >height+300) {
      ypos = 0;
      thing=0;
    }
  }
  void hit(){
   //if(xpos==Xm&&ypos==Ym){
    //Hbar=Hbar-1; 
    //xpos=0;
    //xspeed=0;
   //}
      if (Xm > rectX+xpos && Xm < rectX+xpos + boxX && Ym > rectY+ypos && Ym < rectY+ypos + boxY && thing==0) {
thing=1;

    Hbar=Hbar-1; 
    //xpos=width;
    //xspeed=0;

   }
      
  }
}


  class endenemy { 
  color c;
  float xpos;
  float ypos;
  float xspeed;
  int boxX=200;
  int boxY=100;
  float rectX =xpos-boxX/2;
    float rectY =ypos-boxY/2;
  // The Constructor is defined with arguments.
  endenemy(color tempC, float tempXpos, float tempYpos, float tempXspeed) { 
    c = tempC;
    xpos = tempXpos;
    ypos = tempYpos;
    xspeed = tempXspeed;
  }

  void display() {
    stroke(0);
    fill(c);
    rectMode(CENTER);
    rect(xpos,ypos,boxX,boxY);
  }

  void drive() {
    xpos = xpos + xspeed;
    if (xpos <0-200) {
      xpos = width;
     ap =(int) random(1,4);
    }
  }
  void hit(){
   //if(xpos==Xm&&ypos==Ym){
    //Hbar=Hbar-1; 
    //xpos=0;
    //xspeed=0;
   //}
      if (Xm > rectX+xpos && Xm < rectX+xpos + boxX && Ym > rectY+ypos && Ym < rectY+ypos + boxY) {
        ;      
   
    //Hbar=Hbar-1; 
    //xpos=0;
    //xspeed=0;
   }
      
  }
}
class upendenemy { 
  color c;
  float xpos;
  float ypos;
  float yspeed;
  int boxX=100;
  int boxY=150;
  float rectX =xpos-boxX/2;
    float rectY =ypos-boxY/2;
  // The Constructor is defined with arguments.
  upendenemy(color tempC, float tempXpos, float tempYpos, float tempYspeed) { 
    c = tempC;
    xpos = tempXpos;
    ypos = tempYpos;
    yspeed = tempYspeed;
  }

  void display() {
    stroke(0);
    fill(c);
    rectMode(CENTER);
    rect(xpos,ypos,boxX,boxY);
  }

  void drive() {
    ypos = ypos + yspeed;
    if (ypos >height+300) {
      ypos = 0;
     ap =(int) random(1,4);
    }
  }
  void hit(){
   //if(xpos==Xm&&ypos==Ym){
    //Hbar=Hbar-1; 
    //xpos=0;
    //xspeed=0;
   //}
      if (Xm > rectX+xpos && Xm < rectX+xpos + boxX && Ym > rectY+ypos && Ym < rectY+ypos + boxY) {
        ;      
   
    //Hbar=Hbar-1; 
    //xpos=0;
    //xspeed=0;
   }
      
  }
}
1 Like

not sure what you mean

they go left or down

I mean is the classes dont start at their default position, when it changes from pattern one to pattern two then back to pattern one, pattern one isn’t in its default positions so the bullets can spawn on the player

So when you change the direction tell that class to reset

how do I do tI reset the class?

Your code is hard to read

please try to put your objects into an array of objects

see https://github.com/Kango/Processing-snippets/wiki/Variables,-Arrays-and-object-oriented-programming

then to reset, for loop over the array and call a function reset() in the class. You have to write the function reset() in the class.

1 Like

I tried converting to an array and it didnt work and I dont understand how a resetting a for loop will help

I just wanna reset the enemys to their defalt position once their if statement changes

nvm I figured it out

1 Like