I made a simple program with only one class Bullet....
do you want to see many bullets or would one at a time be enough?
to break the class Bullet up, you would to have
parallel arrays again for x,y etc. ......
- //much simplified!!!!!!!!!!!!!!!!!!!!!!
- //
- //
- //
- // overall structure:
- // A player (only a white rectangle)
- // shoots bullets (see class Bullet), when
- // they hit the wall.
- // 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
- //
- // where is the gun / player
- PVector position; // contains x and y...
- PVector velocity; // contains speed for x and y...
- ArrayList <Bullet> bullets;
- //
- // 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;
- boolean hit=false;
- // ------------------------------------------------------------------
- void setup() {
- size(600, 480);
- // Create an empty ArrayList
- bullets = new ArrayList();
- position = new PVector(333, 333);
- // velocity = new PVector();
- // velocity.x = 0;
- // velocity.y = 0;
- cursor(CROSS);
- }
- void draw() {
- background(111);
- drawPlayerAndBullets ();
- // blue rect
- noStroke();
- if (hit)
- fill(0, 255, 0); // green
- else
- fill(0, 0, 255);
- rect (100, 100, 40, 40);
- }
- void mousePressed() {
- fire();
- }
- void fire() {
- 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
- } //
- //
- 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 drawPlayerAndBullets () {
- //
- // 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
- // println ("************************");
- 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);
- }
- }
- }
- // ================================================================
- // 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;
- hit=true;
- }
- //
- if (life==-1) {
- // explode () ;
- }
- } // method
- boolean finished() {
- // bullet dead?
- if (life < 0) {
- return true;
- }
- else {
- return false;
- }
- }
- void display() {
- // Display the circle / bullet
- fill(244, 2, 2);
- noStroke();
- //stroke(0,life);
- ellipse(x, y, w, w);
- // image( ammo_fired, x, y );
- }
- } // class
- //