Loading...
Logo
Processing Forum
I am new at Processing and have to make a gun and a moving target. When the gun is clicked, a projectile must fire from the gun towards the right side of the screen, and if it's in the target, it must expand. Please help!!


// - - - -
float gravity_x;
float gravity_y;
// - - - -
float tyPos;
float txPos;
float targetVelocity;
float diam;
// - - - -
float gunVelocity;
float gxPos;
float gyPos;

boolean shot;

ArrayList<Particle> particles = new ArrayList<Particle>();

void setup(){
  size(500, 500);
  
  targetVelocity= -3;
  gunVelocity = 2;
  gravity_x = 0.023;
  gravity_y = 3;
  tyPos = 400;
  txPos = 300;
  
  gxPos = 10;
  gyPos = 200;
  diam = 100;
  
  for (int i = 0; i < 2; i++) {
   
    Particle newParticle = new Particle(gxPos, gyPos, 10, 5);
    particles.add(newParticle);
  }

}

class Particle {
  
  float xPos, yPos, xVel, yVel;
  
  Particle(float xPos, float yPos, float xVel, float yVel) {
    this.xPos = xPos; 
    this.yPos = yPos; 
    this.xVel = xVel; 
    this.yVel = yVel;
  }
  
  void shootBullet(){
    xPos += xVel;
    xPos += -gravity_x;
    yPos += gravity_y;
    

    if (abs(xPos - width/2 - 30) > width/2 - 16) {
      xPos = -30;
    }
    if (abs(yPos - height/2 - 30) > height/2 - 16) {
      yPos = -30;
    }
 }
 
  void drawBullet(){
   pushMatrix();
   fill(0);
   translate(xPos, yPos);
   ellipse(0, 0, 32, 32);
   popMatrix(); 
 }
}


void draw() {
  background(255);
  
  drawTarget();
  drawGun();
  
}
  


void drawGun() {
  
  fill(0);
  rect(gxPos, gyPos, 60, 20);
  rect(gxPos, gyPos, 20, 50);
  gyPos += gunVelocity;
  
   if (gyPos <= 0) {
     gunVelocity = -gunVelocity;
   } 
   
   if (gyPos + 50 >= 499) {
     gunVelocity = -gunVelocity;
   }   
}


void drawTarget(){

   noStroke();
   fill(255, 0, 0);
   ellipse(txPos, tyPos, diam, diam);
   fill(255);
   ellipse(txPos,tyPos, 80, 80);
   fill(255, 0, 0);
   ellipse(txPos, tyPos, 60, 60);
   fill(255);
   ellipse(txPos, tyPos, 40, 40);
   
   tyPos += targetVelocity;
   
   if (tyPos - 50 <= 0) {
     targetVelocity = -targetVelocity;
   } 
   
   if (tyPos + 50 >= 499) {
     targetVelocity = -targetVelocity;
   }   
}



void shouldShoot(){

}

void mouseClicked() {
  
  println("clicked");
  
  for (int i = 0; i < particles.size(); i++) {
 
    Particle curParticle = particles.get(i);

    curParticle.shootBullet();
 
    curParticle.drawBullet();
  }
  
  if(isInTarget(mouseX, mouseY, txPos, tyPos, diam/2)) {
    //EXPLODE (expand and fade to invisibility at a constant rate, reappear after)
    diam++;
    shot = true;
  }
}

boolean isInTarget(float x, float y, float a, float b, float r) {
  if (dist(x, y, a, b) <=r) {
    return true;
  } 
  else {
    return false;
  }
}

   

Replies(7)




Hello,

you need to re-think the process.

The general idea is:

you have a class particles (or bullets)
the arraylist has these particles stored.


Shooting
When you shoot (fire), a new particle is started. Add one particle to the arraylist. You don't have this yet:
   
Copy code
  1.     Particle newParticle = new Particle(gxPos, gyPos, 10, 5);   
  2.     particles.add(newParticle);

This happens once when you shoot.
It happens here: void mouseClicked()
(You only have this in setup starting 2 bullets where you should remove it.)


While the bullets fly
While the bullets fly (this is not identical with shooting) you have to paint them and calculate new positions (and check if they hit). Again and again. Therefore what you have in
mouseClicked() now have a in another function showBullets() and call it from draw().


The end of a bullet
When a bullet hit the screen border or the ground just remove it from the list.

Greetings, Chrisir   


these lines are bad (so I commented them out)

Copy code
  1.     // ????
  2.     //    if (abs(xPos - width/2 - 30) > width/2 - 16) {
  3.     //      xPos = -30;
  4.     //    }
  5.     //    if (abs(yPos - height/2 - 30) > height/2 - 16) {
  6.     //      yPos = -30;
  7.     //    }



Another line in your code
Another line in your code must look like this
Copy code
  1.     if (isInTarget(curParticle.xPos, curParticle.yPos, txPos, tyPos, diam/2)) {

(you did check the mouse instead of a bullet).


Also, you need to check each bullet. Each bullet means do it in this for-loop
Copy code
  1.   for (int i = 0; i < particles.size(); i++) {
(which again doesn't belong in mouseClicked() but in draw()) )


I have a working version here (named shootGun).

You can do it too!   

Greetings, Chrisir     






Hi, thanks for your quick reply!!
..but-
I tried to follow your help and implemented the changes..I come out with the error
in my code
  if (isInTarget(curParticle.xPos, curParticle.yPos, txPos, tyPos, diam/2)) {

Cannot find anything named "curParticle"

even though I have specified curParticle a few times before in my code. 
Did I miss something important?><
I will paste my code below this time..

/------------------------------------

// - - - -
float gravity_x;
float gravity_y;
// - - - -
float tyPos;
float txPos;
float targetVelocity;
float diam;
// - - - -
float gunVelocity;
float gxPos;
float gyPos;

boolean shot;

ArrayList<Particle> particles = new ArrayList<Particle>();

void setup() {
  size(500, 500);

  targetVelocity= -3;
  gunVelocity = 2;
  gravity_x = 0.023;
  gravity_y = 3;
  tyPos = 400;
  txPos = 300;

  gxPos = 10;
  gyPos = 200;
  diam = 100;
}

class Particle {

  float xPos, yPos, xVel, yVel;

  Particle(float xPos, float yPos, float xVel, float yVel) {
    this.xPos = xPos; 
    this.yPos = yPos; 
    this.xVel = xVel; 
    this.yVel = yVel;
  }

  void shootBullet() {
    xPos += xVel;
    xPos += -gravity_x;
    yPos += gravity_y;


    //    if (abs(xPos - width/2 - 30) > width/2 - 16) {
    //      xPos = -30;
    //    }
    //    if (abs(yPos - height/2 - 30) > height/2 - 16) {
    //      yPos = -30;
  }




  void drawBullet() {
    pushMatrix();
    fill(0);
    translate(xPos, yPos);
    ellipse(0, 0, 32, 32);
    popMatrix();
  }
}




void draw() {
  background(255);

  drawTarget();
  drawGun();
  showBullets(); 
  

}

void showBullets() {
  
  for (int i = 0; i < particles.size(); i++) {

    Particle curParticle = particles.get(i);

    curParticle.shootBullet();

    curParticle.drawBullet();
  }
}

void drawGun() {

  fill(0);
  rect(gxPos, gyPos, 60, 20);
  rect(gxPos, gyPos, 20, 50);
  gyPos += gunVelocity;

  if (gyPos <= 0) {
    gunVelocity = -gunVelocity;
  } 

  if (gyPos + 50 >= 499) {
    gunVelocity = -gunVelocity;
  }
}


void drawTarget() {

  noStroke();
  fill(255, 0, 0);
  ellipse(txPos, tyPos, diam, diam);
  fill(255);
  ellipse(txPos, tyPos, 80, 80);
  fill(255, 0, 0);
  ellipse(txPos, tyPos, 60, 60);
  fill(255);
  ellipse(txPos, tyPos, 40, 40);

  tyPos += targetVelocity;

  if (tyPos - 50 <= 0) {
    targetVelocity = -targetVelocity;
  } 

  if (tyPos + 50 >= 499) {
    targetVelocity = -targetVelocity;
  }
}



void shouldShoot() {
}

void mouseClicked() {

  println("clicked");

  for (int i = 0; i < particles.size(); i++) {

    Particle curParticle = particles.get(i);

    curParticle.shootBullet();

    curParticle.drawBullet();



    Particle newParticle = new Particle(gxPos, gyPos, 10, 5);
    particles.add(newParticle);
  }

  if (isInTarget(curParticle.xPos, curParticle.yPos, txPos, tyPos, diam/2)) {
    //EXPLODE (expand and fade to invisibility at a constant rate, reappear after)
    diam++;
    shot = true;
  }
}

boolean isInTarget(float x, float y, float a, float b, float r) {
  if (dist(x, y, a, b) <=r) {
    return true;
  } 
  else {
    return false;
  }
}


/----------------


sorry its soo long! I'm really not good at this sigh..

You have this error because you define curParticle in the if statement.
Like Chrisir said you must rethink the process.
You have to verify the goal in the update position method.
Look at the ArrayListClass example it is clear.
Copy code
  1. // - - - -
  2. float gravity_x;
  3. float gravity_y;
  4. // - - - -
  5. float tyPos;
  6. float txPos;
  7. float targetVelocity;
  8. float diam;
  9. // - - - -
  10. float gunVelocity;
  11. float gxPos;
  12. float gyPos;

  13. boolean shot;

  14. ArrayList<Particle> particles = new ArrayList<Particle>();

  15. void setup() {
  16.   size(500, 500);

  17.   targetVelocity= -3;
  18.   gunVelocity = 2;
  19.   gravity_x = 0.023;
  20.   gravity_y = 3;
  21.   tyPos = 400;
  22.   txPos = 300;

  23.   gxPos = 10;
  24.   gyPos = 200;
  25.   diam = 100;
  26. }

  27. class Particle {

  28.   float xPos, yPos, xVel, yVel;

  29.   Particle(float xPos, float yPos, float xVel, float yVel) {
  30.     this.xPos = xPos; 
  31.     this.yPos = yPos; 
  32.     this.xVel = xVel; 
  33.     this.yVel = yVel;
  34.   }

  35.   void shootBullet() {
  36.     xPos += xVel;
  37.     xPos += -gravity_x;
  38.     yPos += gravity_y;
  39.     //    if (abs(xPos - width/2 - 30) > width/2 - 16) {
  40.     //      xPos = -30;
  41.     //    }
  42.     //    if (abs(yPos - height/2 - 30) > height/2 - 16) {
  43.     //      yPos = -30;
  44.   }

  45.   void drawBullet() {
  46.     pushMatrix();
  47.     fill(0);
  48.     translate(xPos, yPos);
  49.     ellipse(0, 0, 32, 32);
  50.     popMatrix();
  51.   }
  52. }

  53. void draw() {
  54.   background(255);

  55.   drawTarget();
  56.   drawGun();
  57.   showBullets();
  58. }

  59. void showBullets() {
  60.   for (int i = 0; i < particles.size(); i++) {
  61.     Particle curParticle = particles.get(i);
  62.     curParticle.shootBullet();
  63.     curParticle.drawBullet();
  64.     if (isInTarget(curParticle.xPos, curParticle.yPos, txPos, tyPos, diam)) {
  65.       println("Yeah !");
  66.       particles.remove(i);
  67.     }
  68.   }
  69. }

  70. void drawGun() {
  71.   fill(0);
  72.   rect(gxPos, gyPos, 60, 20);
  73.   rect(gxPos, gyPos, 20, 50);
  74.   gyPos += gunVelocity;
  75.   if (gyPos <= 0) {
  76.     gunVelocity = -gunVelocity;
  77.   } 
  78.   if (gyPos + 50 >= 499) {
  79.     gunVelocity = -gunVelocity;
  80.   }
  81. }

  82. void drawTarget() {
  83.   noStroke();
  84.   fill(255, 0, 0);
  85.   ellipse(txPos, tyPos, diam, diam);
  86.   fill(255);
  87.   ellipse(txPos, tyPos, 80, 80);
  88.   fill(255, 0, 0);
  89.   ellipse(txPos, tyPos, 60, 60);
  90.   fill(255);
  91.   ellipse(txPos, tyPos, 40, 40);
  92.   tyPos += targetVelocity;
  93.   if (tyPos - 50 <= 0) {
  94.     targetVelocity = -targetVelocity;
  95.   } 
  96.   if (tyPos + 50 >= 499) {
  97.     targetVelocity = -targetVelocity;
  98.   }
  99. }

  100. void shouldShoot() {
  101. }

  102. void mouseClicked() {
  103.   println("clicked");
  104.   Particle newParticle = new Particle(gxPos, gyPos, 10, 5);
  105.   particles.add(newParticle);
  106. }

  107. boolean isInTarget(float x, float y, float a, float b, float r) {
  108.   if (dist(x, y, a, b) <=r) {
  109.     return true;
  110.   } 
  111.   else {
  112.     return false;
  113.   }
  114. }
Thank you all!
I got my game working.
You guys are really smart^o^

-Turnedbarley

that's great!

Thanks for your feedback!



Just for the sake of completeness I post my old version I was talking about.

Copy code
  1. // - - - -
  2. float gravity_x;
  3. float gravity_y;
  4. // - - - -
  5. float tyPos;
  6. float txPos;
  7. float targetVelocity;
  8. float diam;
  9. // - - - -
  10. float gunVelocity;
  11. float gxPos;
  12. float gyPos;
  13. boolean shot;
  14. ArrayList<Particle> particles = new ArrayList<Particle>();
  15. void setup() {
  16.   size(500, 500);
  17.   targetVelocity= -3;
  18.   gunVelocity = 2;
  19.   gravity_x = 0.023;
  20.   gravity_y = 3;
  21.   tyPos = 400;
  22.   txPos = 300;
  23.   gxPos = 10;
  24.   gyPos = 200;
  25.   diam = 100;
  26.   //  for (int i = 0; i < 2; i++) {
  27.   //    Particle newParticle = new Particle(gxPos, gyPos, 10, 5);
  28.   //    particles.add(newParticle);
  29.   //  }
  30. }
  31. // ===================================================================
  32. class Particle {
  33.   float xPos, yPos, xVel, yVel;
  34.   Particle(float xPos, float yPos, float xVel, float yVel) {
  35.     this.xPos = xPos;
  36.     this.yPos = yPos;
  37.     this.xVel = xVel;
  38.     this.yVel = yVel;
  39.   }
  40.   void shootBullet() {
  41.     xPos += xVel/1.0;
  42.     xPos += -gravity_x; // ???
  43.     yPos += gravity_y;
  44.     // ????
  45.     //    if (abs(xPos - width/2 - 30) > width/2 - 16) {
  46.     //      xPos = -30;
  47.     //    }
  48.     //    if (abs(yPos - height/2 - 30) > height/2 - 16) {
  49.     //      yPos = -30;
  50.     //    }
  51.   }
  52.   void drawBullet() {
  53.     pushMatrix();
  54.     fill(0);
  55.     translate(xPos, yPos);
  56.     ellipse(0, 0, 32, 32);
  57.     popMatrix();
  58.   }
  59.   //
  60. } // class
  61. // ===================================================
  62. void draw() {
  63.   background(255);
  64.   // show bullets
  65.   for (int i = 0; i < particles.size(); i++) {
  66.     Particle curParticle = particles.get(i);
  67.     curParticle.shootBullet();
  68.     curParticle.drawBullet();
  69.     if (isInTarget(curParticle.xPos, curParticle.yPos, txPos, tyPos, diam/2)) {
  70.       //EXPLODE (expand and fade to invisibility at a constant rate, reappear after)
  71.       diam++;
  72.       shot = true;
  73.     } // if
  74.   } // for
  75.   //
  76.   drawTarget();
  77.   drawGun();
  78. }
  79. void drawGun() {
  80.   fill(0);
  81.   rect(gxPos, gyPos, 60, 20);
  82.   rect(gxPos, gyPos, 20, 50);
  83.   gyPos += gunVelocity;
  84.   if (gyPos <= 0) {
  85.     gunVelocity = -gunVelocity;
  86.   }
  87.   if (gyPos + 50 >= 499) {
  88.     gunVelocity = -gunVelocity;
  89.   }
  90. }
  91. void drawTarget() {
  92.   noStroke();
  93.   fill(255, 0, 0);
  94.   ellipse(txPos, tyPos, diam, diam);
  95.   fill(255);
  96.   ellipse(txPos, tyPos, 80, 80);
  97.   fill(255, 0, 0);
  98.   ellipse(txPos, tyPos, 60, 60);
  99.   fill(255);
  100.   ellipse(txPos, tyPos, 40, 40);
  101.   tyPos += targetVelocity;
  102.   if (tyPos - 50 <= 0) {
  103.     targetVelocity = -targetVelocity;
  104.   }
  105.   if (tyPos + 50 >= 499) {
  106.     targetVelocity = -targetVelocity;
  107.   }
  108. }
  109. //
  110. //void shouldShoot() {
  111. //}
  112. void mouseClicked() {
  113.   println("clicked");
  114.   Particle newParticle = new Particle(gxPos, gyPos, 10, 5);
  115.   particles.add(newParticle);
  116. }
  117. boolean isInTarget(float x, float y, float a, float b, float r) {
  118.   if (dist(x, y, a, b) <=r) {
  119.     return true;
  120.   }
  121.   else {
  122.     return false;
  123.   }
  124. }
  125. //