Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
    /*CATCH THE EGGS FOR THE EASTER BUNNY!
      Catch the eggs as they fall from the screen by matching the egg colour
      to the basket colour! */

int basCol = #DE242A; //initializing colour for basket
int EggCols = 0; //initilizing colours for eggs
int counter = 0; //keeping track of score

float Xpos = random(10,390); //for randomizing positions
float Ypos = 0; //y position for eggs

boolean drawnEgg = false; //controlling colours

void setup() { //startup
  size(400,400);
  smooth();
  frameRate(60);
  //explaining controls
  println("Click to change the basket between Red, Blue, and Green!");
  println("Match the Eggs with the Basket to catch them!");
}

void draw() {
  background(#EDF8FF);
  ellipseMode(CENTER);
  rectMode(CORNER);
  noStroke();
  
  if (keyPressed && key == 'r') { //reset score to 0 when R is presed
      counter = 0;
  }
  
  //draw striped background
  for (int stripe = 0; stripe < 15; stripe += 2) {
    fill(#B9E5FF);
    rect(0,stripe*40,400,40);
  }
  
  //draw text score at the top left
  textSize(20);
  fill(255);
  text("Score:",10,30);
  text(counter,70,30);
  
  drawBunny(); //drawing the background bunny
  
  //draw sun
  ellipseMode(CENTER);
  fill(#FFFFB9);
  stroke(2);
  stroke(#FF8C52);
  ellipse(100,100,80,80);
  
  //draw hills
  noStroke();
  ellipseMode(CENTER);
  fill(#84CB84);
  ellipse(0,400,600,280);
  fill(#A2E8A2);
  ellipse(400,450,780,280);
  
  drawRandomEgg(); //draw random eggs falling down the screen
  
  matchColors(); // check if colours match
  
  //draw basket
  rectMode(CENTER);
  stroke(0);
  fill(basCol);
  rect(mouseX,375,60,40);
}

  /* mousePressed instead of mouseClicked because it will not work if basket
  is moving*/
void mousePressed() { //basket will change colour when mouse is pressed
  if (basCol == #DE242A) { //change to blue if red
    basCol = #4578E3;
  } else if (basCol == #4578E3) { //change to green if blue
    basCol = #5CF222;
  } else { //change to red if green
    basCol = #DE242A;
  }
}

void drawBunny() {
  //draw bunny head
  fill(255);
  ellipseMode(CORNER);
  ellipse(150,210,240,160);
  ellipse(230,80,30,40);
  ellipse(350,80,30,40);
  quad(230,100,260,100,250,220,220,220);
  quad(350,100,380,100,350,230,320,220);
  //bunny eyes
  stroke(#00aef0);
  ellipse(190,260,60,60);
  ellipse(280,270,60,60);
  
  //floats to constrain pupils to sclera
  float eyeX1 = constrain(mouseX,195,215);
  float eyeX2 = constrain(mouseX,285,305);
  float eyeY1 = constrain(mouseY,265,285);
  float eyeY2 = constrain(mouseY,275,295);
  //pupils that move with the mouse
  fill(#00aef0);
  ellipse(eyeX1,eyeY1,30,30);
  ellipse(eyeX2,eyeY2,30,30);
}

void drawRandomEgg() {
  //randomizing colour for eggs 
  if(drawnEgg == false){
    EggCols = int(random(0, 3)); //randomizing colours using numbers
    if (EggCols == 0) { //if 0 then colour is red
      EggCols = #DE242A;
    } else if (EggCols == 1) { //if 1 then colour is blue
      EggCols = #4578E3;
    } else { //if 2 then colour is green
      EggCols = #5CF222;
    }
    drawnEgg = true; //stop from constantly randomizing colour
  }
      
  fill(EggCols); //drawing the egg
  stroke(0);
  ellipse(Xpos,Ypos,30,40);
  if (counter <= 10) {
    Ypos = Ypos + 5; 
  } else if (counter >= 10) { //increase Y speed after 10 eggs
      Ypos = Ypos + 8;
  }
    
  if (Ypos >= height) { //when egg hits the bottom
    drawnEgg = false;
    Ypos = 0;
    println("NOOOOoooooo!!"); //print if egg is missed
    Xpos = random(10,390); //bring egg back to top
  }
}

void matchColors() { //collision if colours match
  if (abs(mouseX - Xpos) <= 30 && Ypos >= 375 && EggCols == basCol) {
    drawnEgg = false; //bring back to top
    Ypos = 0;
    Xpos = random(10,390);
    counter = counter + 1; //increase score by 1
  }
}