Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
///////////////
/*Catch Flies*/
///// by //////
//*Bora Ocal*//
///////////////



float xInsideSpeed = 3;
float yInsideSpeed = 3;

float randomJarX = random(150, 250);
float randomJarY = random(240, 333);
float bugPosX = 100;
float bugPosY = 100 ;
float xSpeed = 3;
float ySpeed = 3;

float randomSpeedX = random(1, 4);
float randomSpeedY = random(1, 4);

boolean spawnFireFly =true ;
boolean killFireFly ;

boolean increaseY = true;
boolean increaseX = true;

boolean spawnFireFlyInJar = true;


boolean  changeBugPos = false;

boolean flashlightKill = false;

float bugHit = 0;

float lightX = 100;
float lightY = 100;
int score;

void setup() {

  size(400, 400);
  frameRate(120);
  noCursor();
  informationPanel();
  noStroke();
}

void draw() {
  background(0);

  drawStars();
  drawFences();
  drawTable();
  lightShow();
  scoreBoard();


  //change bug positon when I press the mouse 
  if (changeBugPos == false) {
    drawABug();
  } else {

    DrawABugInsideJar();
  }

  drawJar();
  showFlashlight();
}



void drawJar() {  
  ////jar
  stroke(0);
  fill(#D6BA2E, 155);
  rectMode(CORNERS);
  rect(150, 240, 250, 340);

  //top
  stroke(0);
  fill(#D6BA2E, 155);
  ellipseMode(CORNERS);
  ellipse(150, 230, 250, 250);

  //bottom
  stroke(0);
  fill(#D6BA2E, 155);
  ellipseMode(CORNERS);
  ellipse(150, 320, 250, 360);
}

void drawStars() {
  stroke(random(122, 245));
  point(4, 40);
  stroke(random(122, 245));
  point(23, 46);
  stroke(random(122, 245));
  point(50, 110);
  stroke(random(122, 245));
  point(43, 224);
  stroke(random(122, 245));
  point(250, 650);
  stroke(random(100, 245));
  point(300, 118);
  stroke(random(100, 245));
  point(200, 80);
  stroke(random(100, 245));
  point(223, 50);
  stroke(random(100, 245));
  point(231, 20);
  stroke(random(100, 245));
  point(320, 30);
  stroke(random(100, 245));
  point(60, 30);
  stroke(random(100, 245));
  point(232, 100);
  stroke(random(100, 245));
  point(320, 250);
  stroke(random(100, 245));
  point(340, 240);
  stroke(random(100, 245));
  point(320, 60);
  stroke(random(122, 245));
  point(344, 40);
  stroke(random(122, 245));
  point(323, 230);
  stroke(random(122, 245));
  point(250, 200);
  stroke(random(122, 245));
  point(9, 200);
  stroke(random(122, 245));
  point(10, 220);
  stroke(random(100, 245));
  point(30, 220);
  stroke(random(100, 245));
  point(56, 245);
  stroke(random(100, 245));
  point(76, 235);
  stroke(random(100, 245));
  point(43, 225);
  stroke(random(100, 245));
  point(330, 215);
  stroke(random(100, 245));
  point(60, 105);
  stroke(random(100, 245));
  point(232, 125);
  stroke(random(100, 245));
  point(344, 124);
  stroke(random(100, 245));
  point(245, 165);
  stroke(random(100, 245));
  point(370, 6);
}

void drawTable() {
  stroke(0);
  fill(#44702C, 210);
  rect(0, 320, 400, 400);
}

void drawFences() {
  ////fences
  stroke(0);
  fill(142, 60, 13);
  rect(0, 348, 400, 310);

//loop for fences
  for (int i = 0; i<width; i++) {
    stroke(0);
    fill(142, 60, 13);
    rect(i*40-5, 400, i*40+20, 275);
    triangle(i*40-5, 275, i*40+8, 250, i*40+20, 275);
  }
}
void DrawABugInsideJar() {

  //body
  noStroke();
  fill(0);
  ellipseMode(CENTER);
  ellipse(randomJarX, randomJarY, 12, 12);

  ////wings

  //left 
  noStroke();
  fill(122);
  ellipseMode(CENTER);
  ellipse(randomJarX -13, randomJarY, 17, 12);

  //right
  noStroke();
  fill(122);
  ellipseMode(CENTER);
  ellipse(randomJarX +14, randomJarY, -17, 12);



  ////spawn firefly and add speed to it

  //if it the X positon bigger than 250 decrease speed
  if (spawnFireFlyInJar == true) {
    if (randomJarX > 250) {

      randomJarX = randomJarX - xInsideSpeed;
      increaseX = false;
    }

    //if it the X positon smaller than 150 increase speed
    if (randomJarX < 150) {

      randomJarX = randomJarX + xInsideSpeed;
      increaseX = true;
    }
    //if it the x positon is between increase and decrease
    if (randomJarX > 150 &&  randomJarX < 250) {
      if (increaseX) {
        randomJarX = randomJarX + xInsideSpeed;
      } else {
        randomJarX = randomJarX - xInsideSpeed;
      }
    }

    //bugPosY = bugPosY + ySpeed;

    //if it the Y positon bigger than 340 decrease speed
    if (randomJarY > 340) {

      randomJarY = randomJarY - yInsideSpeed;
      increaseY = false;
    }

    //if it the Y positon smaller than 240 increase speed
    if (randomJarY < 240) {

      randomJarY = randomJarY + yInsideSpeed;
      increaseY = true;
    }

    //if it the Y positon is between increase and decrease
    if (randomJarY > 240 &&  randomJarY < 340) {
      if (increaseY) {
        randomJarY = randomJarY + yInsideSpeed;
      } else {
        randomJarY = randomJarY - yInsideSpeed;
      }
    }
  }
}

void drawABug() {


  ////firefly
  //body
  noStroke();
  fill(0);
  ellipseMode(CENTER);
  ellipse(bugPosX, bugPosY, 12, 12);

  ////wings

  //left
  noStroke();
  fill(122);
  ellipseMode(CENTER);
  ellipse(bugPosX -13, bugPosY, 17, 12);

  //right
  noStroke();
  fill(122);
  ellipseMode(CENTER);
  ellipse(bugPosX +14, bugPosY, -17, 12);


  bugPosX = bugPosX + xSpeed;

  ////if bug hits the edge flip the bug
  if (spawnFireFly == true) {
    if (bugPosX > width) {

      xSpeed = -xSpeed;
    }


    if (bugPosX < 0) {

      xSpeed = -xSpeed;
    }

    bugPosY = bugPosY + ySpeed;

    if (bugPosY > 230) {

      ySpeed = -ySpeed;
    }


    if (bugPosY < 0) {

      ySpeed = -ySpeed;
    }
  }
}


void mousePressed() {
  //if distance is less then 50 kill bug
  if (dist(mouseX, mouseY, bugPosX, bugPosY)<50) {
    changeBugPos = true;
    score ++;
  }
}

void keyPressed() {
  //if I press space restart the game
  if (key == ' ') {
    changeBugPos = false;
  }
}

void showFlashlight() 
{

  if (mouseX>0 && mouseX<150) {
    ////draw flashlight on the left

    //draw main part
    stroke(0);
    fill(122, 122, 122);
    quad(35, 285, 75, 292, 60, 380, 20, 375);

    //draw top part
    stroke(0);
    fill(122, 122, 122);
    quad(30, 260, 90, 270, 75, 292, 35, 285);

    //draw switch
    stroke(0);
    fill(255, 0, 0);
    ellipseMode(CENTER);
    ellipse(50, 321, 15, 15);
  } 


  if (mouseX>150 && mouseX<250) {
    ////draw flashlight on middle

    //bottom part
    stroke(0);
    fill(122, 122, 122);
    rectMode(CORNERS);
    rect(180, 310, 220, 400);

    //switch
    stroke(0);
    fill(255, 0, 0);
    ellipseMode(CENTER);
    ellipse(200, 343, 15, 15);

    //top part
    stroke(0);
    fill(122, 122, 122);
    quad(170, 285, 230, 285, 220, 310, 180, 310);
  } 


  if (mouseX>250 && mouseX<400) {
    ////draw flashlight on right

    //bottom part
    stroke(0);
    fill(122, 122, 122);
    quad(365, 285, 325, 292, 340, 380, 380, 375);

    //switch
    stroke(0);
    fill(255, 0, 0);
    ellipseMode(CENTER);
    ellipse(350, 321, 15, 15);

    //top part
    stroke(0);
    fill(122, 122, 122);
    quad(310, 270, 370, 260, 365, 285, 325, 292 );
  }
}

void lightShow() {

  //light
  stroke(255);
  strokeWeight(2);
  fill(255, 255, 255, 190);
  ellipseMode(CENTER); 
  ellipse(mouseX, mouseY, lightX, lightY);

  fill(255);
  ellipseMode(CENTER); 
  ellipse(mouseX, mouseY, lightX/2, lightY/2);
}


void scoreBoard() {
  noStroke();
  fill(255, 0, 0);
  text("SCORE:", 330, 20);
  text(score, 380, 20);
}

void informationPanel() {
  println("Your objective is to collect bugs");
  println("Press Space to Reset");
}