aswartzell.net Defenders

defender fds;
defender jkl;

ArrayList attackers = new ArrayList();
int speed = 2;
boolean gameOver;
boolean pause = true;
boolean keyS = false;
boolean keyF = false;
boolean keyJ = false;
boolean keyL = false;
int invaders = 10;

void setup() {
  size(455, 455);
  background(255);
  fds = new defender(15, 1);
  jkl = new defender(width-15, 2);
  invasion();
}

void draw() {
  fill(0);
  if (gameOver) {
    if (frameCount % 50 < 25)
      background(255);
    else {
      background(0);
      fill(255);
    }
  } 
  else if (pause) {
    //do nothing
  }
  else {
    background(255);
    fds.render();
    jkl.render();
    drawProjectiles();
    drawAttackers();
    if (keyS)
      fds.move(-speed, jkl);
    if (keyF)
      fds.move(speed, jkl);
    if (keyJ)
      jkl.move(-speed, fds);
    if (keyL)
      jkl.move(speed, fds);
    writeScore();
  }
}

void writeScore() {
  textSize(12);
  textAlign(LEFT);
  text("Hits: "+fds.hits, 15, height-15);
  textAlign(RIGHT);
  text("Hits: "+jkl.hits, width-15, height-15);
}

void drawProjectiles() {

  for (int i = 0; i < fds.projectiles.size(); i++) {
    projectile p = (projectile) fds.projectiles.get(i);
    if (p.move()) 
      p.render();
    else
      fds.projectiles.remove(p);
  }
  for (int i = 0; i < jkl.projectiles.size(); i++) {
    projectile p = (projectile) jkl.projectiles.get(i);
    if (p.move()) 
      p.render();
    else
      jkl.projectiles.remove(p);
  }
}

void drawAttackers() {

  if (attackers.size() == 0)
    invasion();

  for (int i = 0; i < attackers.size(); i++) {
    projectile p = (projectile) attackers.get(i);

    if (p.move()) {
      p.render();
      if (p.collide(fds.projectiles)) {
        fds.hits+=p.diameter;
        attackers.remove(p);
      }
      else if (p.collide(jkl.projectiles)) {
        jkl.hits+=p.diameter;
        attackers.remove(p);
      }
    }
    else
      gameOver = true;
  }
}

void invasion() {
  for (int i=0; i < invaders; i++) {
    attackers.add(new projectile(random(20, 40), random(speed/3, speed/2.8)));
  }
  speed+=.1;
  invaders++;
}

void mouseClicked() {
  pause=!pause;
}

void keyPressed() {
  if (key==' ') {
    if (gameOver) {
      gameOver = false;
      attackers.clear();
      fds.projectiles.clear();
      jkl.projectiles.clear();
    }
    else
      pause=!pause;
  }
  if (key=='s' || key=='S')
    keyS=true;
  if (key=='f' || key=='F')
    keyF=true;
  if (key=='j' || key=='J')
    keyJ=true;
  if (key=='l' || key=='L')
    keyL=true;
  if (key=='d') {
    keyS=false;
    keyF=false;
    fds.shoot();
  }
  if (key=='k') {
    keyJ=false;
    keyL=false;
    jkl.shoot();
  }
}

void keyReleased() {
  if (key=='s' || key=='S')
    keyS=false;
  if (key=='f' || key=='F')
    keyF=false;
  if (key=='j' || key=='J')
    keyJ=false;
  if (key=='l' || key=='L')
    keyL=false;
}
class defender {

  int x;
  int hits = 0;
  int shots = 0;
  ArrayList projectiles = new ArrayList();
  int player = 0;

  defender(int _x, int _player) {
    x = _x;
    player = _player;
  }

  void move(int move, defender other) {
    if (x+move > 1 && x+move < width-1) {
      x += move;
      if (player==1 && x+move > other.x)
        other.move(move,this);
      else if (player==2 && x+move < other.x) 
        other.move(move,this);
    }
  }

  void render() {
    strokeWeight(10);
    stroke(0);
    ellipse(x, height, 10, 10);
  }

  int pos() {
    return x;
  }

  void shoot() {
    projectiles.add(new projectile(x, height, 4));
    shots++;
  }
}
class projectile {

  float x;
  float y;
  float diameter;
  float speed;

//construct attackers
  projectile(float _diameter, float _speed) {
    x = parseInt(random(0, width));
    y = 1;
    diameter = _diameter;
    speed = _speed;
  }

//construct bullets
  projectile(float _x, float _y, float _diameter) {
    x = _x;
    y = _y;
    diameter = _diameter;
    speed = -10;
  }

  boolean move() {
    if (y+speed > 0 && y+speed < height) {
      y += speed;
      return(true);
    }
    else
      return(false);
  }

  void render() {
    strokeWeight(diameter);
    stroke(0);
    point(x, y);
  }

  boolean collide(ArrayList others) {

    boolean test = false;

    for (int i=0; i < others.size(); i++) {
      projectile o = (projectile) others.get(i);
      float dist = sqrt((x-o.x)*(x-o.x)+(y-o.y)*(y-o.y));
      if (dist < diameter/2 + o.diameter/2)
        test = true;
    }

    return(test);
  }
}