How to limit number of bullets in shooting game?

edited July 2015 in How To...

Hello,

I'm just learning processing and trying to create a very simple shooting game. I need to know how to fire bullets from the location of a mouse click to random positions, and limit the number of bullets to 10. What is the best way to approach this?

Answers

  • Answer ✓
    class Bullet {
     float px,py,vx,vy;
      Bullet(){
        px = mouseX;
        py = mouseY;
        float r = random(TWO_PI);
        vx = 5*cos(r);
        vy = 5*sin(r);
      }
      boolean draw(){
        px+=vx;
        py+=vy;
        ellipse(px,py,10,10);
        return(!(px>-20&&px<width+20&&py>-20&&px<height+20));
      }
    }
    
    ArrayList<Bullet> bullets = new ArrayList();
    
    void setup(){
      size(440,440);
    }
    
    void draw(){
      background(0);
      for(int i=0;i<bullets.size();i++)if(bullets.get(i).draw())bullets.remove(i);
    }
    
    void mousePressed(){
      if(bullets.size()<10) bullets.add(new Bullet());
    }
    
Sign In or Register to comment.