Someone please help solving a problem : shooting bullets

Here is a shooting example with an explosion where the bullets hit a surface or wall

Click the mouse in the upper screen area.

This Sketch has multiple tabs originally.

Chrisir


// ********************************************************************************
//         joined pde-file of folder  ShootTest3
// ********************************************************************************


// ********************************************************************************
// tab: ShootTest3.pde main file (file name is folder name)



/* OpenProcessing Tweak of *@*http://www.openprocessing.org/sketch/77863*@* */
/* !do not delete the line above, required for linking your tweak if you upload again */



// overall structure: The tab ShootTest1 is the main program.
// A player (only a white rectangle) 
// in tab player shoots bullets (in tab Bullet), when 
// they hit the wall, they explode (tab Explosion).
// Bullet and Explosion are really similar. 
// The tabs Player, Bullet and Explosion each contain only 
// the class.
// The tower can only shoot upwards, follows mouse, click mouse. 
//
// idea from 
// http://forum.processing.org/topic/need-help-adding-shooting
// with help from 
// http://forum.processing.org/topic/have-eyes-follow-mouse
//
// Player
Player thePlayer = new Player();
// for explosions (these are actually Shrapnels)
ArrayList <Explosion> missiles; 
//
// bullets
final float bulletFlySpeed = 4.2;  // how fast bullet flies
final int fireSpeed=200; // how often you fire / distance between bullets
int lastFired=millis();  // timer to determine when next bullet starts 
// ammo not in use 
int ammoCount = 70; 

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

void setup() {
  size(600, 480);
  // explosions: Create an empty ArrayList
  missiles = new ArrayList<Explosion>();
  cursor(CROSS);
}

void draw() {
  background(111);
  thePlayer.draw();
  // blue rect
  noStroke();
  fill(0, 0, 255);
  rect (100, 100, 40, 40);
  ExplosionManager();
}

void mousePressed() {
  thePlayer.fire();
}

void keyPressed() {
  thePlayer.fire();
}

void ExplosionManager() {
  // this is a new for loop.
  // Actually for the Shrapnels.
  for (Explosion m : missiles) {
    m.decreaseLife(); // call the decrease life (for explosion)
    m.fly();          // call the "fly" method of the missile
    m.display();      // call the "display" method of the missile
  }
  //
  // remove dead ones (you need to have a conventional for-loop here)
  for (int i=missiles.size()-1; i>=0; i--) {
    Explosion m = (Explosion) missiles.get(i);
    if (m.dead) {
      missiles.remove(i);
    }
  }
} // func
//

// ********************************************************************************
// tab: Bullet.pde



// ================================================================
// Bullet - Simple class for bullets
// http://www.processing.org/learning/topics/arraylistclass.html

class Bullet {

  float x;
  float y;
  float speedX ;  
  float speedY;
  float w;
  float life = 255;

  Bullet(float tempX, float tempY, // new2
    float tempSpeedX, float tempSpeedY, 
    float tempW) {
    x = tempX;
    y = tempY;
    w = tempW;
    speedX = tempSpeedX; // 4.2; // new2 
    speedY = tempSpeedY; // 0;
  }

  void move() {
    // Add speed to location
    x = x + speedX; 
    y = y + speedY;
    //
    // kill bullet when outside screen
    if (x<4) 
      life=-1;
    if (y<4) 
      life=-1;
    if (x>width-4) 
      life=-1;
    if (y>width-4) 
      life=-1;

    // blue rect
    if (x>=100 && y>=100 && x<=140 && y<=140)   
      life=-1;
    //
    if (life==-1) 
      explode () ;
  } // method 

  void explode () {
    // explode!
    int maxMissiles= int(random(4, 222));
    for (int i=0; i<maxMissiles; i+=1) {   
      // this is where explode missile/shrapnel object is created
      // and added to the missiles arrayList.
      // It is small (4) and life decrease is .72 (how fast it dies),
      // no Line (false).
      missiles.add( new Explosion(
        random(x-3, x+3), random(y+9, y+12), 
        random(-1.3, 1.3), random(-2.7, .6), 
        4, .72, false)); //
    }
  } // method

  boolean finished() {
    // bullet dead?
    if (life < 0) {
      return true;
    } else {
      return false;
    }
  }

  void display() {
    // Display the circle
    fill(244, 2, 2);
    noStroke();
    //stroke(0,life);
    ellipse(x, y, w, w);
    // image( ammo_fired, x, y );
  }
} // class 
//


// ********************************************************************************
// tab: Explosion.pde



// ===============================================================
// the tab for Explosion class

class Explosion {
  float startlocX, startlocY; // start pos
  float x, y;          // current pos
  float xvel, yvel;    // velocity
  float sizeMissile ;  // size for rect
  float life=20;       // how much lifes does it have
  float lifeDecrease;  // remove lifes
  boolean dead=false;  // is it alive?
  boolean withLine;    // draw line? Explosion has none.
  //
  // contructor
  Explosion(
    float _startlocX, float _startlocY, 
    float _xvel, float _yvel, 
    float _size, float _lifeDecrease, 
    boolean _withLine)
  {
    startlocX = _startlocX;
    startlocY = _startlocY;   
    x = startlocX;
    y = _startlocY;
    sizeMissile = _size;
    lifeDecrease=_lifeDecrease;
    xvel = _xvel;
    yvel = _yvel;
    withLine=_withLine;
  }  // contructor
  //
  void display() {
    //stroke(255, 2, 2);
    fill(255, 2, 2);
    noStroke();
    if (withLine) {
      line(startlocX, startlocY, x, y);
    }
    sizeMissile-=.07;
    rect(x, y, sizeMissile, sizeMissile);
  } // method
  //
  void fly() {
    x += xvel;
    y += yvel;
  } // method
  //
  void decreaseLife() {
    life-=lifeDecrease;

    // check wall collision
    if (x<0) 
      dead=true;
    if (y<0) 
      dead=true;
    if (x>width) 
      dead=true;
    if (y>width) 
      dead=true;

    if (life<=0 || sizeMissile<=0) {
      dead=true;
    }
  } // method
  //
} // class
//
// ======================================================================


// ********************************************************************************
// tab: player.pde



// =====================================================================
// the Player class

class Player {

  ArrayList<Bullet> bullets;   
  PVector position, velocity; // contains x and y...

  Player() {
    bullets  = new ArrayList();   
    position = new PVector(333, 333);
    velocity = new PVector();
    velocity.x = 0;
    velocity.y = 0;
  }

  void fire() {     // new2
    if (fireSpeed<=millis()-lastFired) {
      if (ammoCount>0) {
        // ammoCount--;
        lastFired=millis();

        float xSpeed ;
        float ySpeed ; 
        float angle = update(mouseX, mouseY);
        xSpeed = cos(angle);
        ySpeed = sin(angle);
        xSpeed*= bulletFlySpeed;
        ySpeed*= bulletFlySpeed;
        if (ySpeed>0)
          ySpeed=0;
        bullets.add ( new Bullet( 
          position.x+12, position.y-14, 
          xSpeed, ySpeed, 
          5 ));
      } // if
    } // if
  } // method 

  float update(int mx, int my) {
    //determines angle to mouse
    float angle = atan2(float(my)-(position.y-14), float(mx)-(position.x+12));
    return angle;
  }

  void draw() {
    // draw player & bullets
    //
    // draw player 
    pushMatrix();
    translate(position.x, position.y);
    noStroke();
    fill(255);
    rect (0, 0, 20, 40);
    popMatrix(); // undoes all

    // draw bullets
    for (Bullet currentBullet : bullets ) {     // new1 
      currentBullet.move();
      currentBullet.display();
    }

    // With an array, we say length, with an ArrayList, we say size()
    // The length of an ArrayList is dynamic
    // Notice how we are looping through the ArrayList backwards
    // This is because we are deleting elements from the list  
    for (int i = bullets.size()-1; i >= 0; i--) { 
      // An ArrayList 
      Bullet currentBullet = bullets.get(i);
      if (currentBullet.finished()) {
        // Items can be deleted with remove()
        bullets.remove(i);
      }
    }
  }
}


// End of joined file. ****************************************************************************
1 Like