Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
boolean bulletActive;
boolean zombieShot;
int xPos;
int[] ZXPos = new int [20]; //array


void setup() {
  size(600, 200);
  background(255);
  for (int i = 0; i < ZXPos.length; i++) {
    ZXPos[i]=600+i*25;
  }
  quad(0, 170, 600, 170, 600, 200, 0, 200);//base
}

//zombies

void zombies(int num) {
  ZXPos[num]--;
  if (xPos >= ZXPos[num]-6 && xPos <= ZXPos[num]+6) { //&& checks if both functions are true
    ZXPos[num] = width;
    bulletActive = false;
    xPos = -15;
  }
  if (mouseX >= ZXPos[num]-6 && mouseX <= ZXPos[num]+6) {
    setup(); //resets zombie positions
    bulletActive=false;
  }
  fill(0);
  rect(ZXPos[num], 155, random(4,6), 15);//legs
  rect(ZXPos[num]-2.5, 145, 10, 15);//body
  rect(ZXPos[num], 140, 5, 5);//neck
  ellipse(ZXPos[num]+2.5, 138, 10, 10);//head
}

void draw() {
  background(255);
  noStroke();
  fill(0);
  quad(0, 170, 600, 170, 600, 200, 0, 200);//base

  if (bulletActive) {
    bulletMove();
  }

  for (int i=0; i<ZXPos.length; i++) {
    zombies (i);
  }

  //human


  rect(mouseX, 155, 5, 15);//legs
  rect(mouseX-2.5, 145, 10, 15);//body
  rect(mouseX, 140, 5, 5);//neck
  ellipse(mouseX+2.5, 138, 10, 10);//head
}


void mouseReleased() {
  if (bulletActive == false) {
    spawnBullet(mouseX);
  }
}

void spawnBullet(int xCoord) {
  bulletActive = true;
  xPos = xCoord;
}

void bulletMove() {
  fill(0);
  ellipse(xPos+10, 150, 5, 5);
  xPos=xPos+4;
  if (xPos > width) {
    bulletActive = false;
  }
}