Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
// Lawn Mower Toy
// Jesmin Hondell      Student ID: 991 358 697 
// 24/September/2015
//
// In this program you can cuz the man's lawn. By pressing the mouse on the left side of
// the screen, the lawn mower moves forward. If you presse the mouse on the right side
// the screen the lawn mower moves back. As soon as the lawn mower is far enough away,
// it will start to rain and the grass will grow back. Cut it again!

// Declaring  lawn mower x position variables
float xPos = 190; // X position of the lawn mower's red rectangle
float xPos2 = 220; // X position of the lawn mower's black rectangle
float xPos3 = 225; // X postion of the lawn mower's yellow ellipse

// Declaring man's x position variables
float armXPos = 230; // X position of the man's arm
float headXPos = 240; // X position of the man's head
float eyeXPos = 237; // X possition of the man's eye
float bodyXPos = 235; // X position of the man's body

// Declaring the variables for y coordinates of the the grass patches
float patchA1 = 230;
float patchA2 = 250;
float patchB1 = 230;
float patchB2 = 250;
float patchC1 = 230;
float patchC2 = 250;
float patchD1 = 230;
float patchD2 = 250;

// Declaring variables for the rain drop's x and y coordinates and speed
float rainXPos = 200;
float rainYPos = 0;
float rainSpeed = 2;

void setup() {
  
  // Set the size of the background
  size (400, 350);
}

void draw() {

  // Removes the outline from drawings
  noStroke() ;
  // Sets the background color to light blue
  background (176, 196, 222);
  noStroke();

  // Constraining the Lawn Mower's and man's X values so that they don't go off screen
  xPos = constrain(xPos, 0, 345); 
  xPos2 = constrain(xPos2, 30, 375);
  xPos3 = constrain(xPos3, 35, 380);
  armXPos = constrain(armXPos, 40, 385); 
  headXPos = constrain (headXPos, 50, 395) ; 
  eyeXPos = constrain (eyeXPos, 47, 392); 
  bodyXPos = constrain (bodyXPos, 45, 390);
  // Constraining the grass patch y values so that it doesnt go too high or low.
  patchA1 = constrain (patchA1, 230, 260);
  patchA2 = constrain (patchA2, 250, 280);
  patchB1 = constrain (patchB1, 230, 260);
  patchB2 = constrain (patchB2, 250, 280);
  patchC1 = constrain (patchC1, 230, 260);
  patchC2 = constrain (patchC2, 250, 280);
  patchD1 = constrain (patchD1, 230, 260);
  patchD2 = constrain (patchD2, 250, 280);

  // Draw House
  fill (200, 100, 60);
  rect (250, 150, 130, 100);
  // House Roof
  fill (100, 50, 10);
  triangle (315, 80, 240, 150, 390, 150);
  // House Door
  fill (100, 50, 10);
  rect (300, 210, 30, 40);
  stroke (255);
  line (300, 210, 300, 250);
  line (300, 210, 330, 210);
  line (330, 210, 330, 250);
  fill (255);
  ellipse (323, 235, 5, 5) ;
  // House Window 1
  fill (150, 200, 255);
  rect (260, 160, 20, 40) ;
  line (270, 160, 270, 200) ;
  line (260, 180, 280, 180);
  // House Window 2
  rect (290, 160, 50, 40);
  line (300, 160, 300, 200); 
  line (330, 160, 330, 200);
  line (315, 160, 315, 200);
  line (290, 180, 340, 180);
  // House Window 3
  rect (350, 160, 20, 40);
  line (360, 160, 360, 200);
  line (350, 180, 370, 180);
  // House Window 4
  rect (260, 210, 20, 30);
  line (270, 210, 270, 240);
  line (260, 225, 280, 225);
  //House Window 5
  rect (350, 210, 20, 30);
  line (360, 210, 360, 240);
  line (350, 225, 370, 225);

  // Draw Tall Grass Patches
  noStroke ();
  fill (190, 200, 0) ;
  triangle (170, patchA1, 165, patchA2, 175, patchA2);
  triangle (160, patchA1, 155, patchA2, 165, patchA2);
  triangle (150, patchA1, 145, patchA2, 155, patchA2);
  triangle (140, patchA1, 135, patchA2, 145, patchA2);
  triangle (130, patchB1, 125, patchB2, 135, patchB2);
  triangle (120, patchB1, 115, patchB2, 125, patchB2);
  triangle (110, patchB1, 105, patchB2, 115, patchB2);
  triangle (100, patchB1, 95, patchB2, 105, patchB2);
  triangle (90, patchC1, 85, patchC2, 95, patchC2);
  triangle (80, patchC1, 75, patchC2, 85, patchC2);
  triangle (70, patchC1, 65, patchC2, 75, patchC2);
  triangle (60, patchC1, 55, patchC2, 65, patchC2);
  triangle (50, patchC1, 45, patchC2, 55, patchC2);
  triangle (40, patchD1, 35, patchD2, 45, patchD2);
  triangle (30, patchD1, 25, patchD2, 35, patchD2);
  triangle (20, patchD1, 15, patchD2, 25, patchD2);
  triangle (10, patchD1, 5, patchD2, 15, patchD2);

  // Draw Lawn Mower
  noStroke();
  fill (200, 0, 0);
  rect (xPos, 230, 30, 20);
  fill (0);
  rect (xPos2, 220, 10, 30);
  fill (150, random (100, 255), 0);
  ellipse (xPos3, 225, 5, 5);

  // Draw Man
  fill (0, 50, 150); //Body Color
  rect (bodyXPos, 220, 10, 25); //Body
  fill (200, 200, 170); //Arms & Head Color
  rect (armXPos, 225, 10, 5); //Arms
  ellipse (headXPos, 210, 10, 20); //Head
  fill (0); // Shoes & Eyes Color
  rect (bodyXPos, 245, 10, 5); //Shoe
  ellipse (eyeXPos, 210, 3, 3); //Eye

  // Calls the rainDrop function to be executed at the same time the grass grows back.
  // Which is when the lawn mower's location is greater than 175 on the x axis.
  if (xPos > 175) {
    // Loop gives the rain a line effect, and speed up the rain function.
    for (int i = 0; i < 20; i++)
    {
      rainDrop();
    }
  }

  // Draw Clouds
  fill (200);
  ellipse (150, 23, 150, 50);
  fill (190);
  ellipse (230, 20, 180, 40);
  fill (210);
  ellipse (320, 20, 200, 40);
  fill (180);
  ellipse (50, 20, 150, 40);
  fill (170);
  ellipse (90, 50, 150, 40);
  fill (175);
  ellipse (270, 50, 200, 40);

  // Draw Grass 
  fill (100, 200, 0);
  rect (0, 250, 400, 150);

  // Calls the grassGrowth & lawnMowing function
  grassGrowth();
  lawnMowing();
}

// This function is what causes the lawn mower to move and "cut" the grass
void lawnMowing() {

  // If the mouse is being pressed on the left side, move the lawn mower and man forward
  // by a speed of 1.
  if (mousePressed && mouseX <= 200)
  {
    xPos = xPos - 1;
    xPos2 = xPos2 - 1;
    xPos3 = xPos3 - 1;
    armXPos = armXPos - 1; 
    headXPos = headXPos - 1; 
    eyeXPos = eyeXPos - 1; 
    bodyXPos =  bodyXPos - 1;
  } 
  // If the mouse is being pressed on the right side, move the lawn mower and man back
  // by a speed of 1.
  else if (mousePressed == true && mouseX > 200)
  {
    xPos = xPos + 1;
    xPos2 = xPos2 + 1;
    xPos3 = xPos3 + 1;
    armXPos = armXPos + 1; 
    headXPos = headXPos + 1; 
    eyeXPos = eyeXPos + 1; 
    bodyXPos =  bodyXPos + 1;
  }
}

// This Function is what causes the grass to skrink when the lawn mower is cutting it
// and grow back once the lawn mower leaves.
void grassGrowth () {
  // If the lawn moser passes a certain x coordinate each patch of grass will shrink
  // into the ground.
  if (xPos < 175 ) {
    patchA1 = patchA1 + 0.1;
    patchA2 = patchA2 + 0.1;
    if (xPos <135) {
      patchB1 = patchB1 + 0.1;
      patchB2 = patchB2 + 0.1;
    }
    if (xPos < 95) {
      patchC1 = patchC1 + 0.1;
      patchC2 = patchC2 + 0.1;
    }
    if (xPos < 45) {
      patchD1 = patchD1 + 0.1;
      patchD2 = patchD2 + 0.1;
    }
  } 
  // Otherwise if the lawn mower leaves the tall grass area, it will grow back.
  else {
    patchA1 = patchA1 - 0.1;
    patchA2 = patchA2 - 0.1;
    patchB1 = patchB1 - 0.1;
    patchB2 = patchB2 - 0.1;
    patchC1 = patchC1 - 0.1;
    patchC2 = patchC2 - 0.1;
    patchD1 = patchD1 - 0.1;
    patchD2 = patchD2 - 0.1;
  }
}

// Lines 249-256 was used from Tim Stapert's "Falling Ball" Code: 
// (http://www.openprocessing.org/sketch/196619)
// This function is responsable for the raindrops falling 
void rainDrop() {

  // Brings the rain drop back to the top once it goes off the screen at the bottom
  if (rainYPos > height + 5)
  {
    rainYPos = 0;
    rainXPos = random(175); // Provides a new x position for the rain drop
  }
  
  // Makes the rain drops fall by increasing the value of the rainYPos
  rainYPos = rainYPos + rainSpeed;

  // Draws the rain drop
  fill (0, 210, 255, 150);
  ellipse (rainXPos, rainYPos, 5, 5);
}