Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
////////////////////////////////////////////////////////////////////////////////////////////////
////PLANT CARE 101 (interactive toy) (by Bernice Wong)                                      ////
//// Click on fertilizer or waterpot and carry them over the plants to see what happens!    ////
////////////////////////////////////////////////////////////////////////////////////////////////

int x5, y5, w5, h5;                  // dimensions of waterpot's rect
float x1, y1,x2, y2, x3, y3, x4, y4; // dimensions of plantpot 1's quad
float r1;                            // x of plant's pot rect
float fX, fY;

boolean doFertilizerGrow = false;      // boolean for fertilizer "growing" when mouse hovers over it
boolean doWaterPotGrow = false;        // boolean for waterpot "growing" when mouse hovers over it
boolean waterPotCircle = false;      // boolean for waterpot's "circles" 

// variables for Plant 1 (Cactus)
int P1BodyRectY = 270; 
int  P1BodyRectH = 70;
float  P1BodyEllipseY = 235;

// variables for Plant 2 (Flower)
int P2BodyRectY = 260;          // P2 body rectangle Y
float P2BodyRectH = 120;        // P2 body rectangle Height
float P2BodyEllipse1Y = 210;    // P2 body ellipse Y for ellipse 1
float P2BodyEllipse2Y = 228;    // P2 body ellipse Y for ellipse 2

// variables for Plant 1, 2, and 3's colours
int colourCounter1 = 0;
int colourCounter2 = 0;
int colourCounter3 = 0;
color c = color(200, 173, 144);  // plant 1 colour  
color f = color(210, 173, 144);  // plant 2 colour
color o = color(190, 173, 144);  // Plant 3 colour

void setup() {
  size (400, 400);
}

void draw() {  
  background(115, 131, 175);       // fill background
  drawGrid();                      // draw grid
  drawWindow();                    // draw window
  drawTable();                     // draw table
  shelf(0, 100);                   // draw shelves
  shelf(275, 100);
  
  fertilizer(70, 65, 75, 70);      // draw fertilizer
  waterPot(333, 65, 66, 70);       // draw waterpot
  
  plantCactus(P1BodyRectY, P1BodyRectH, P1BodyEllipseY, c);                      // draw plant 1 (cactus)
  plantFlower(P2BodyRectY, P2BodyRectH, P2BodyEllipse1Y, P2BodyEllipse2Y, f);    // draw plant 2 (flower)
  plantOther(o);                                                                 // draw plant 3 (other)
  plantChange();            // function for when plants change states either due to fertilizer or waterpot
  
  plantPot(30, 290, 110, 290, 95, 340, 45, 340, 20);        //draw plant pots
  plantPot(160, 290, 240, 290, 225, 340, 175, 340, 150);
  plantPot(290, 290, 370, 290, 355, 340, 305, 340, 280);
  
  fertilizerGrow();    // function for when fertilizer "grows" due to mouse hover
  waterPotGrow();      // function for when waterpot "grows" due to mouse hover
}

////////BACKGROUND RELATED FUNCTIONS ////////
// function to draw grid
void drawGrid(){    
int gridX1 = 25;        // strarting vertical line X value
int gridY1 = 0;         // starting vertical line Y value
int gridSpacing = 75;   // spacing between each line
int len1 = 330;         // ending value of vertical lines

int gridY2 = 25;        // starting horizontal line X value
int gridX2 = 0;         // starting horizontal line Y value
int len2 = 400;         // ending value of horizontal lines

int endLegsX = 400;     // maximum length of vertical lines
int endLegsY = 400;     // maximum length of horizontal lines

    while (gridX1 <= endLegsX) {  // when X value of first vertical line of grid is less than or equal to the end length of legs, a new line is drawn
      stroke(91, 101, 130);
      strokeWeight(3);
      line (gridX1, gridY1, gridX1, gridY1 + len1);
      gridX1 = gridX1 + gridSpacing;
  }
    while (gridY2 <= endLegsY){  // when Y value of horizonal first line of grid is less than or equal to the length of legs, a new line is drawn
      stroke(91, 101, 130);
      line (gridX2, gridY2, gridX2 + len2, gridY2);
      gridY2 = gridY2 + gridSpacing;
    }
}
// function to draw table
void drawTable(){  
  fill(103, 114, 145);            
  rectMode(CORNER); 
  rect(0, 330, 400, 70);
}
// function to draw window
void drawWindow(){  
int sunX = 200;     // starting X and Y position of sun
int sunY = 100;
int x = 110;        // corner of window (100, 60)
int y = 60;
int w = 180;        // width of window
int h = 200;        // height of window
  noStroke();
  fill(182, 232, 239);
  rectMode(CORNER);
  rect(x, y, w,h);
  fill(255);
  if (mouseX == 200 && mouseY == 100){  // if mouseX is equal to 200 and mouseY is equal to 100, sun starts at (200, 100)
    sunX = 200;
    sunY = 100;
    ellipse(sunX, sunY, 40, 40);
  } else if (mouseX >= x+20 && mouseX <= x+h-40 && mouseY >= y && mouseY <= y+w){  // if mouseX is less than the distance of the window to the wall...
    sunX = mouseX;
    sunY = 100;
    ellipse(sunX, sunY, 40, 40);
  } else if (mouseX >= x+20 && mouseX <= x+h-40 && mouseY >= 0 && mouseY <= y){
    sunX = mouseX;
    sunY = 100;
    ellipse(sunX, sunY, 40, 40); 
  } else if (mouseX >= x+20 && mouseX <= x+h-40 && mouseY >= y+h && mouseY <=400){  
    sunX = mouseX;
    sunY = 100;
    ellipse(sunX, sunY, 40, 40);
  } else if (mouseX <= 130){     // if mouseX is less than or equal to 130, sun's position is 130, 100
    sunX = 130;  
    sunY = 100;
    ellipse(sunX, sunY, 40, 40);
  } else if (mouseX >=270){      // if mouseX is more than or equal to 270, sun's position is 270, 100
    sunX = 270;
    sunY = 100;
    ellipse(sunX, sunY, 40, 40);
  } 
  
  fill(153, 163, 193);      // window frame rectangles
  rectMode(CORNER);
  rect(110, 50, 15, 200);
  rect(275, 50, 15, 200);
  rect(98, 250, 200, 15);
  rect(110, 50, 175, 15);
  rect(110, 120, 175, 10);
  rect(110, 180, 175, 10);
  rect(195, 50, 10, 200);
}
// function to draw shelf
void shelf(float x, float y) {  
  fill(99, 113, 153);
  rectMode(CORNER);
  rect(x, y, 125, 10);
}
// function to draw plant pots
void plantPot(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4, float r1){   
  noStroke();
  fill (81, 88, 107);
  quad(x1, y1, x2, y2, x3, y3, x4, y4);
  rectMode(CORNER);
  rect(r1, 280, 100, 10);
}

////////WATER POT RELATED FUNCTIONS ////////
// function to draw waterpot pot
void waterPot(float x5, float y5 , float w5, float h5) {  
int quadX1 = 300;  int quadY1 = 50;  // variables for water pot's spout
int quadX2 = 300;  int quadY2 = 80;
int quadX3 = 280;  int quadY3 = 30;
int quadX4 = 290;  int quadY4 = 30;
  noStroke();
  fill(168, 174, 193);
  rectMode(CENTER);
  rect(x5, y5, w5, h5);
  quad(quadX1, quadY1, quadX2, quadY2, quadX3, quadY3, quadX4, quadY4);
}

void waterPotGrow(){  // make water pot bigger when mouse hovers over it
int quadX1 = 300;  int quadY1 = 50;  // variables for water pot's spout
int quadX2 = 300;  int quadY2 = 80;
int quadX3 = 280;  int quadY3 = 30;
int quadX4 = 290;  int quadY4 = 30;
int x = 333;    // water pot rect x value
int y = 65;     // water pot rect y value
float w = 66;   // water pot width
float h = 70;   // water pot height

int waterX = mouseX;  // water droplet x value
int waterY = mouseY;  // water droplet y value

  if (mousePressed && mouseX >= quadX1 && mouseX <= quadX1 + w && mouseY >= quadY3 && mouseY <=quadY3 + h){ //water pot follows mouse when mouse presses on it
    fill(168, 174, 193);
    w = w + 5;
    h = h + 5;
    waterPot(x, y, w, h);
    quad(quadX1, quadY1, quadX2, quadY2, quadX3, quadY3, quadX4, quadY4);
    doWaterPotGrow = true;
  }else if (mouseX >= quadX1 && mouseX <= quadX1 + w && mouseY >= quadY3 && mouseY <=quadY3 + h){ // if mouse hovers water pot, water pot it enlarges
    w = w + 4;
    h = h + 4;
    waterPot(x -2, y -2, w, h);
    quad(quadX1 -4, quadY1 , quadX2 -4, quadY2, quadX3 -4, quadY3, quadX4 -4, quadY4);
    doWaterPotGrow = false;
  }
  if (doWaterPotGrow == true && mouseX >= 0 && mouseX <= 400 && mouseY >= 0 && mouseY <= 400){ // if water pot is clicked, water falls
    x = mouseX;  // water pot X follows mouse when water pot is clicked
    y = mouseY;  // water pot Y follows mouse when water pot is clicked
    waterPot(x, y, w, h);
    quad(quadX1 - 333 + mouseX, quadY1 - 65 + mouseY, quadX2 - 333 + mouseX, quadY2 - 65 + mouseY, quadX3 - 333 + mouseX, quadY3 - 65 + mouseY, quadX4 - 333 + mouseX, quadY4 - 65 + mouseY);
    fill(168, 174, 193);
    waterY = waterY/5 + mouseY++;  // water droplet Y is water droplet's value divided by 5, with mouseY + 1 value added 
    fill(255);
    ellipseMode(CENTER);
    ellipse(waterX - 40 , waterY, 10, 10);
    ellipse(waterX - 45 , waterY - 50, 10, 10);
    ellipse(waterX + 5 - 40, waterY + 10, 10, 10);
    ellipse(waterX - 15 - 40, waterY + 30, 10, 10);
  }
  if (doFertilizerGrow == true){  // if fertilizer is already selected, waterpot isn't allowed to be selected
    doWaterPotGrow = false;
  }
}

////////FERTILIZER RELATED FUNCTIONS ////////
void fertilizer(float fX, float fY, float w, float h) { // draw basic fertilizer
  fill(112, 111, 119);
  noStroke();
  rectMode(CENTER);
  rect(fX, fY, w, h);
}

void fertilizerGrow(){  
float x = 70;  // fertilizer rect's x
float y = 65;  // fertilizer rect's y 
float w = 75;  // fertilizer width
float h = 70;  // fertilizer height

int fcX = mouseX;  // fertilizer cube x
int fcY = mouseY;  // fertilizer cube y

  if (mousePressed && mouseX >= 25 && mouseX <= 25 + w && mouseY >= 30 && mouseY <= 30 + h){ 
    doFertilizerGrow = true;
    w = 80;
    h = 75;
    fertilizer (x, y, w, h);
  } else if (mouseX >= 25 && mouseX <= 25 + w && mouseY >= 30 && mouseY <= 30 + h){ // when mouse hovers fertilizer, it grows
    fill(112, 111, 119);
    x = 70;
    y = 65;
    w = 80;
    h = 75;
    fertilizer(x, y, w, h);
    doFertilizerGrow = false;
  }
  if (doFertilizerGrow == true && mouseX >= 0 && mouseX <= 400 && mouseY >=0 && mouseY <=400){
    x = mouseX;
    y = mouseY;
    fertilizer(x, y, w, h);
    fill(186, 196, 209);
    fcY = mouseY/5 + mouseY++;
    rectMode(CENTER);
    rect(fcX, fcY, 10,10); // small cube
    rect(fcX + 5, fcY + 10, 10,10);
    rect(fcX - 15, fcY + 30, 10,10);
  }
  if (doWaterPotGrow == true){  // if water pot is already selected, fertilizer isn't allowed to be selected
    doFertilizerGrow = false;
  }
}

////////PLANT RELATED FUNCTIONS ////////
// function for 1st plant (cactus)
void plantCactus(int P1BodyRectY, int P1BodyRectH, float P1BodyEllipseY, color c) {
  noStroke();
  fill(c);
  rectMode(CENTER);
  rect(70, P1BodyRectY, 40,  P1BodyRectH);  // main body of cactus
  ellipseMode(CENTER);
  ellipse(70,  P1BodyEllipseY, 40, 30);  // main top of cactus
  rectMode(CORNER);
  rect(90, 260, 20, 10);
  rect(100, 250, 10, 10);
  ellipseMode(CENTER);
  ellipse(105, 250, 10, 10);
  rectMode(CORNER);
  rect(30, 245, 20, 10);
  rect(30, 235, 10, 10);
  ellipseMode(CENTER);
  ellipse(35, 235, 10, 10);
}

// function for 2nd plant (flower)
void plantFlower(int  P2BodyRectY, float P2BodyRectH, float P2BodyEllipse1Y, float P2BodyEllipse2Y, color f) {
  noStroke();
  fill(f);
  rectMode(CENTER);
  rect(200, P2BodyRectY, 5, P2BodyRectH);
  ellipseMode(CENTER);
  ellipse(200, P2BodyEllipse1Y, 20, 20);
  ellipse(200, P2BodyEllipse2Y, 15, 15);
}

// function for 3rd plant (other)
void plantOther(color o) {
  stroke(o);
  strokeWeight(10);
  line(300, 230, 335, 325);
  line(320, 210, 330, 325);
}
// functions dealing with changing states of plants 
void plantChange(){
int Ax = 0;    // area above plant 1 (from 0 - 132)
int Bx = 133;  // area above plant 2 (from 133 - 266)
int Cx = 267;  // area above plant 3 (from 267 - 400)
int y = 129;   // y value areas above plants
int w = 133;   // widths of areas above plants
int h = 150;   // heights of areas above plants 

  noStroke();  // Area A (area 1)
  noFill();
  rectMode(CORNER);
  rect(Ax, y, w, h);
  
  noFill();    // Area B (area 2)
  rectMode(CORNER);
  rect(Bx, y, w + 1, h);
  
  noFill();    // Area C (area 3)
  rectMode(CORNER);
  rect(Cx, y, w, h);
  ///// when fertilizer is over certain areas, colours change //////
  if (doFertilizerGrow == true && mouseX >= Ax && mouseX <= Bx && mouseY >=y && mouseY <= y+h){       // When fertilizer is above Area A, colour of plant 1 changes
    color c = color(130, 173, 144);
    plantCactus(P1BodyRectY,  P1BodyRectH,  P1BodyEllipseY, c);
      }
  if (doFertilizerGrow == true && mouseX >= Bx && mouseX <= Cx && mouseY >= y && mouseY <=y+h){       // When fertilizer is above Area B, colour of plant 2 changs 
    color f = color(110, 173, 144);
    plantFlower( P2BodyRectY,  P2BodyRectH,  P2BodyEllipse1Y,  P2BodyEllipse2Y, f);
  }
  if (doFertilizerGrow == true && mouseX >= Cx && mouseX <=Ax+Bx+Cx && mouseY >= y && mouseY <=y+h){  // When fertilizer is above Area C, colour of plant 3 changes
    color o = color(130, 173, 144);
    stroke(o);
    plantOther(o);
  }
  //// when water is over certain areas, y and heights of plants change//
  if (doWaterPotGrow == true && mouseX >= Ax && mouseX <= Bx && mouseY >=y && mouseY <= y+h){         // When waterpot is above Area A, height of plant changes
     P1BodyRectH = constrain( P1BodyRectH, 0, 100);          // 
     P1BodyEllipseY = constrain( P1BodyEllipseY, 220, 235);
    for (int i = 0; i<300; i++);
      P1BodyRectH =  P1BodyRectH + 1;
      P1BodyEllipseY =  P1BodyEllipseY - 0.5;
      plantCactus(P1BodyRectY,  P1BodyRectH,  P1BodyEllipseY, c);
      y--;
  }
  if (doWaterPotGrow == true && mouseX >=Bx && mouseX <= Cx && mouseY >= y && mouseY <= y + h){      // When water pot is above Area B, height of plant 2 changes 
    P2BodyRectH = constrain( P2BodyRectH, 0, 155);
     P2BodyEllipse1Y = constrain ( P2BodyEllipse1Y, 165, 210);
     P2BodyEllipse2Y = constrain ( P2BodyEllipse2Y, 178, 228);
    for (int i = 0; i<300; i++);
      P2BodyRectH =  P2BodyRectH + 1;
      P2BodyEllipse1Y =  P2BodyEllipse1Y - 0.5;
      P2BodyEllipse2Y =  P2BodyEllipse2Y - 0.5;
      plantFlower(P2BodyRectY,  P2BodyRectH,  P2BodyEllipse1Y,  P2BodyEllipse2Y, f);
  } 
   if (doWaterPotGrow == true && mouseX >=Cx && mouseX <=Ax+Bx+Cx && mouseY >=y && mouseY <y+h){    // When water pot is above Area C, plant 3 is changed to a random colour
    color o = color(random(255), random (255), random (255));
    stroke(o);
    plantOther(o);
  }
}