Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
Title: Bird Shooter
By: Zhe Yue
Click on birds to shoot them
*/

//Bird array
Bird[] birds = new Bird[10];

//Crosshair object
Crosshair crosshair;

void setup() {
  noCursor();
  size(600, 600);
  smooth();
  rectMode(CORNERS);
  ellipseMode(RADIUS);

  //Initialize birds
  for (int i=0; i<birds.length; i++) {
    birds[i]= new Bird();
  }

  //Initialize crosshair
  crosshair = new Crosshair();
}

void draw() {
  frameRate(60);
  noStroke();
  background(171, 188, 230);
  drawClouds();

  //draw and move birds, reset if bird goes off screen or is clicked on
  for (int i=0; i<birds.length; i++) {
    birds[i].update();
    birds[i].display();
  }

  //draw crosshair at mouse coordinates
  crosshair.display(mouseX, mouseY);
}

void drawClouds() {
  //draw clouds
  fill(234, 234, 234);
  ellipse(65, 280, 70, 60);
  ellipse(155, 330, 50, 50);
  ellipse(230, 330, 40, 50);
  ellipse(260, 410, 50, 50);
  ellipse(310, 450, 30, 40);
  triangle(250, 430, 250, 500, 400, 500);
  rect(0, 300, 250, 500);

  ellipse(570, 100, 40, 40);
  ellipse(550, 170, 30, 50);
  ellipse(510, 210, 40, 40);
  triangle(420, 250, 540, 250, 540, 190);
  rect(540, 100, 600, 250);
}
class Bird {
  //bird variables
  PVector position = new PVector();
  PVector velocity = new PVector();
  color birdColor;

  Bird() {
    initial();
  }

  //reference: gone fishing
  void initial() {
    //set initial bird values
    position.x = width+random(0, 100);
    position.y = random(0, height);
    velocity.x = random(-10, -0.5);
    birdColor = color(random(50, 200), random(50, 200), random(50, 200));
  }

  void update() {
  //move bird from right to left
  position.add(velocity);

  //if bird is off screen, reset bird
  if (position.x<-100) {
    initial();
  }
  //if mouse is over bird when mousepressed, reset bird
  if (mousePressed && mouseX > position.x && mouseX < position.x+150 && mouseY > position.y-50 && mouseY < position.y+30) {
    initial();
  }
  }
  
  void display() {
    stroke(0);
    strokeWeight(1);
    fill(birdColor);
    //beak
    triangle(position.x, position.y, position.x+20, position.y-10, position.x+20, position.y+10);
    //tail
    triangle(position.x+70, position.y+10, position.x+150, position.y-10, position.x+120, position.y+20);
    //back wing
    triangle(position.x+50, position.y, position.x+60, position.y-60, position.x+70, position.y-10);
    //body
    ellipse(position.x+70, position.y+5, 30, 25);
    //head
    ellipse(position.x+30, position.y, 20, 20);
    //front wing
    triangle(position.x+50, position.y, position.x+110, position.y-60, position.x+110, position.y-10);
    //eye
    fill(255);
    ellipse(position.x+30, position.y, 10, 10);
    fill(0);
    ellipse(position.x+30, position.y, 2, 2);
  }
}
class Crosshair {
  //reference: http://www-acad.sheridanc.on.ca/PROG14998/2014/oo-toy/ootoy_71/index.html
  void display(float crosshairX, float crosshairY) {
    strokeWeight(2);
    stroke(255, 0, 0);
    fill(255, 0, 0);
    ellipse(crosshairX, crosshairY, 5, 5);
    noFill();
    ellipse(crosshairX, crosshairY, 15, 15);
    noStroke();
  }
}