Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
"Christmas Tree"
Made by Kent Anderson-Brettell

- Christmas is the time to be warm, happy, and surrounded by family. With a tree set up,
and presents all ready under the tree, it is time to decorate the tree with ornaments and
light the lights.
- Place an ornament by hovering over and and holding down the left mouse button.
While clicking, you can drag the ornament and place it on the tree by lifting the mouse button.
- When all 8 ornaments are on the tree, the christmas lights will turn on.
- WARNING: Sometimes the ornaments will eat each other. To avoid this, try to not hover over
an ornament while you are carrying one.
- If an ornament gets devoured, the default locations of all ornaments can be restored by
hitting BACKSPACE

Enjoy!

*/


//Set initial variables
float o1x, o2x, o3x, o4x, o5x, o6x, o7x, o8x;
float o1y, o2y, o3y, o4y, o5y, o6y, o7y, o8y;
float o1w, o2w, o3w, o4w, o5w, o6w, o7w, o8w;
float o1h, o2h, o3h, o4h, o5h, o6h, o7h, o8h;
boolean grab1 = false;
boolean grab2 = false;
boolean grab3 = false;
boolean grab4 = false;
boolean grab5 = false;
boolean grab6 = false;
boolean grab7 = false;
boolean grab8 = false;
boolean inUse = false;
boolean light1 = false;
boolean light2 = false;
boolean light3 = false;
boolean light4 = false;
boolean light5 = false;
boolean light6 = false;
boolean light7 = false;
boolean light8 = false;

void setup() {

  //Set canvas size
  size (400, 400); 

  //Set object modes
  rectMode (CENTER);
  ellipseMode (RADIUS);

  //Set initial variable locations
  o1x = 50;
  o1y = 50;
  o1w = 15;
  o1h = 15;
  o2x = 50;
  o2y = 100;
  o2w = 15;
  o2h = 15;
  o3x = 50;
  o3y = 150;
  o3w = 15;
  o3h = 15;
  o4x = 50;
  o4y = 200;
  o4w = 15;
  o4h = 15;
  o5x = 350;
  o5y = 50;
  o5w = 15;
  o5h = 15;
  o6x = 350;
  o6y = 100;
  o6w = 15;
  o6h = 15;
  o7x = 350;
  o7y = 150;
  o7w = 15;
  o7h = 15;
  o8x = 350;
  o8y = 200;
  o8w = 15;
  o8h = 15;
}

void draw() {
  frameRate (60);
  noStroke ();
  //Draw background
  drawBackground();
  //Determine if the lights should turn on
  lightBooleans();
  //Draw rug
  drawRug();
  //Draw presents behind the tree
  drawPresent(50, 350, 60, color(0));
  drawPresent(220, 340, 45, color(230, 122, 7));
  //Draw the tree
  drawTree();
  //Draw presents in front of the tree
  drawPresent(150, 365, 50, color(1, 235, 213));
  drawPresent(100, 380, 25, color(75, 167, 103));
  drawPresent(300, 360, 40, color(100, 100, 100));
  drawPresent(250, 380, 45, color(255));
  //Draw lights and ornaments
  drawTreeLights();
  drawOrnaments();
  updateOrnaments();
  //In case the ornaments devour each other
  emergencyReset();
}

void drawBackground() {
 //Set background colour, and draw the window and floor
  background (255, 212, 233);
  fill (168, 124, 24);
  rect (200, 365, 400, 100);
  fill (255);
  rect (300, 130, 100, 150);
  fill (0, 26, 61);
  rect (300, 130, 90, 140);
  fill (255);
  rect (300, 130, 99, 5);
}


void drawRug() {
  //Draw a rug under the tree
  fill (74, 1, 0);
  ellipse (200, 365, 150, 30);
}

void drawTree() {
  //Draw the Christmas Tree
  noStroke();
  fill (51, 33, 0);
  rect (200, 330, 60, 70);
  fill (0, 71, 8);
  triangle (200, 0, 350, 320, 50, 320);
  triangle (200, 0, 330, 250, 70, 250);
  triangle (200, 0, 305, 180, 95, 180);
  triangle (200, 0, 275, 110, 125, 110);
}


void drawPresent(int pX, int pY, int pWH, color c ) {

  //Draw a Christmas Present with the definitions given in Draw
  stroke(255);
  fill (c);
  //Draw main box
  rect(pX, pY, pWH, pWH);
  //Draw top of box
  quad(pX - pWH/2, pY - pWH/2, pX, pY - pWH/2 - pWH/5, pX + pWH, pY - pWH/2 - pWH/5, pX + pWH/2, pY - pWH/2);
  //Draw right of box
  quad(pX + pWH/2, pY + pWH/2, pX + pWH/2, pY - pWH/2, pX + pWH, pY - pWH/2 - pWH/5, pX + pWH, pY + pWH/2 - pWH/5);
}

void drawTreeLights() {
  //Set the colour of the lights to random if all ornaments are on the tree
  if (light1) {
   fill (random (0, 255), random (0, 255), random (0, 255));
   frameRate (30);
  } else {
    fill (0);
  }
  //Draw top lights
  float bulb1X = 135;
  while (bulb1X <= 265) {
    ellipse (bulb1X, 110 + sin(bulb1X / 10)*10, 5, 10);
    bulb1X = bulb1X + 25;
  }
  //Draw middle lights
  float bulb2X = 110;
  while (bulb2X <= 295) {
    ellipse (bulb2X, 180 + sin(bulb2X / 10)*10, 5, 10);
    bulb2X = bulb2X + 25;
  }
  //Draw bottom lights
    float bulb3X = 80;
  while (bulb3X <= 320) {
    ellipse (bulb3X, 250 + sin(bulb3X / 10)*10, 5, 10);
    bulb3X = bulb3X + 25;
  }
  
}

void drawOrnaments() {
  //Draw 1st ornaments at its initial location
  fill (255, 255, 93);
  ellipse (o1x, o1y, o1w, o1h);
  //Draw 2nd ornaments at its initial location
  fill (31, 255, 138);
  ellipse (o2x, o2y, o2w, o2h);
  //Draw 3rd ornaments at its initial location
  fill (255);
  ellipse (o3x, o3y, o3w, o3h);
  //Draw 4th ornaments at its initial location
  fill (0, 0, 53);
  ellipse (o4x, o4y, o4w, o4h);
  //Draw 5th ornaments at its initial location
  fill (255, 246, 110);
  ellipse (o5x, o5y, o5w, o5h);
  //Draw 6th ornaments at its initial location
  fill (255, 144, 35);
  ellipse (o6x, o6y, o6w, o6h);
  //Draw 7th ornaments at its initial location
  fill (255, 150, 163);
  ellipse (o7x, o7y, o7w, o7h);
  //Draw 8th ornaments at its initial location
  fill (0, 255, 222);
  ellipse (o8x, o8y, o8w, o8h);
}

void updateOrnaments () {
  //Update Ornament 1
  //Determine if mouse is over Ornament 1
  if (mouseX < o1x + o1w && mouseX > o1x - o1w && mouseY < o1y + o1h && mouseY > o1y - o1h )
  {
    grab1 = true;
  } else {
    grab1 = false;
  }
  //Determine if mouse is grabbing Ornament 1, and prevent from grabbing others
  if (mousePressed && grab1 && !inUse) {
    o1x = mouseX;
    o1y = mouseY;
    inUse = true;
  } else {
    inUse = false;
  }
  //Provide boundry box to screen
  if (o1x < 0) {
    o1x = 0;
  } 
  if (o1x > 400) {
    o1x = 400;
  } 
  if (o1y < 0) {
    o1y = 0;
  }
  if (o1y > 400) {
    o1y = 400;
  }

  //Update Ornament 2
  //Determine if mouse is over Ornament 2
  if (mouseX < o2x + o2w && mouseX > o2x - o2w && mouseY < o2y + o2h && mouseY > o2y - o2h )
  {
    grab2 = true;
  } else {
    grab2 = false;
  }
  //Determine if mouse is grabbing Ornament 2, and prevent from grabbing others
  if (mousePressed && grab2 && !inUse) {
    o2x = mouseX;
    o2y = mouseY;
    inUse = true;
  } else {
    inUse = false;
  }
  //Provide boundry box to screen
  if (o2x < 0) {
    o2x = 0;
  } 
  if (o2x > 400) {
    o2x = 400;
  } 
  if (o2y < 0) {
    o2y = 0;
  }
  if (o2y > 400) {
    o2y = 400;
  }

  //Update Ornament 3
  //Determine if mouse is over Ornament 3
  if (mouseX < o3x + o3w && mouseX > o3x - o3w && mouseY < o3y + o3h && mouseY > o3y - o3h )
  {
    grab3 = true;
  } else {
    grab3 = false;
  }
  //Determine if mouse is grabbing Ornament 3, and prevent from grabbing others
  if (mousePressed && grab3 && !inUse) {
    o3x = mouseX;
    o3y = mouseY;
    inUse = true;
  } else {
    inUse = false;
  }
  //Provide boundry box to screen
  if (o3x < 0) {
    o3x = 0;
  } 
  if (o3x > 400) {
    o3x = 400;
  } 
  if (o3y < 0) {
    o3y = 0;
  }
  if (o3y > 400) {
    o3y = 400;
  }

  //Update Ornament 4
  //Determine if mouse is over Ornament 4
  if (mouseX < o4x + o4w && mouseX > o4x - o4w && mouseY < o4y + o4h && mouseY > o4y - o4h )
  {
    grab4 = true;
  } else {
    grab4 = false;
  }
  //Determine if mouse is grabbing Ornament 4, and prevent from grabbing others
  if (mousePressed && grab4 && !inUse) {
    o4x = mouseX;
    o4y = mouseY;
    inUse = true;
  } else {
    inUse = false;
  }
  //Provide boundry box to screen
  if (o4x < 0) {
    o4x = 0;
  } 
  if (o4x > 400) {
    o4x = 400;
  } 
  if (o4y < 0) {
    o4y = 0;
  }
  if (o4y > 400) {
    o4y = 400;
  }

  //Update Ornament 5
  //Determine if mouse is over Ornament 5
  if (mouseX < o5x + o5w && mouseX > o5x - o5w && mouseY < o5y + o5h && mouseY > o5y - o5h )
  {
    grab5 = true;
  } else {
    grab5 = false;
  }
  //Determine if mouse is grabbing Ornament 5, and prevent from grabbing others
  if (mousePressed && grab5 && !inUse) {
    o5x = mouseX;
    o5y = mouseY;
    inUse = true;
  } else {
    inUse = false;
  }
  //Provide boundry box to screen
  if (o5x < 0) {
    o5x = 0;
  } 
  if (o5x > 400) {
    o5x = 400;
  } 
  if (o5y < 0) {
    o5y = 0;
  }
  if (o5y > 400) {
    o5y = 400;
  }

  //Update Ornament 6
  //Determine if mouse is over Ornament 6
  if (mouseX < o6x + o6w && mouseX > o6x - o6w && mouseY < o6y + o6h && mouseY > o6y - o6h )
  {
    grab6 = true;
  } else {
    grab6 = false;
  }
  //Determine if mouse is grabbing Ornament 6, and prevent from grabbing others
  if (mousePressed && grab6 && !inUse) {
    o6x = mouseX;
    o6y = mouseY;
    inUse = true;
  } else {
    inUse = false;
  }
  //Provide boundry box to screen
  if (o6x < 0) {
    o6x = 0;
  } 
  if (o6x > 400) {
    o6x = 400;
  } 
  if (o6y < 0) {
    o6y = 0;
  }
  if (o6y > 400) {
    o6y = 400;
  }

  //Update Ornament 7
  //Determine if mouse is over Ornament 7
  if (mouseX < o7x + o7w && mouseX > o7x - o7w && mouseY < o7y + o7h && mouseY > o7y - o7h )
  {
    grab7 = true;
  } else {
    grab7 = false;
  }
  //Determine if mouse is grabbing Ornament 7, and prevent from grabbing others
  if (mousePressed && grab7 && !inUse) {
    o7x = mouseX;
    o7y = mouseY;
    inUse = true;
  } else {
    inUse = false;
  }
  //Provide boundry box to screen
  if (o7x < 0) {
    o7x = 0;
  } 
  if (o7x > 400) {
    o7x = 400;
  } 
  if (o7y < 0) {
    o7y = 0;
  }
  if (o7y > 400) {
    o7y = 400;
  }

  //Update Ornament 8
  //Determine if mouse is over Ornament 8
  if (mouseX < o8x + o8w && mouseX > o8x - o8w && mouseY < o8y + o8h && mouseY > o8y - o8h )
  {
    grab8 = true;
  } else {
    grab8 = false;
  }
  //Determine if mouse is grabbing Ornament 8, and prevent from grabbing others
  if (mousePressed && grab8 && !inUse) {
    o8x = mouseX;
    o8y = mouseY;
    inUse = true;
  } else {
    inUse = false;
  }
  //Provide boundry box to screen
  if (o8x < 0) {
    o8x = 0;
  } 
  if (o8x > 400) {
    o8x = 400;
  } 
  if (o8y < 0) {
    o8y = 0;
  }
  if (o8y > 400) {
    o8y = 400;
  }
}

void emergencyReset () {

  //If ornaments absorb each other, reset to initial locations when backspace is pressed
  if (keyCode == BACKSPACE) {
    o1x = 50;
    o1y = 50;
    o1w = 15;
    o1h = 15;
    o2x = 50;
    o2y = 100;
    o2w = 15;
    o2h = 15;
    o3x = 50;
    o3y = 150;
    o3w = 15;
    o3h = 15;
    o4x = 50;
    o4y = 200;
    o4w = 15;
    o4h = 15;
    o5x = 350;
    o5y = 50;
    o5w = 15;
    o5h = 15;
    o6x = 350;
    o6y = 100;
    o6w = 15;
    o6h = 15;
    o7x = 350;
    o7y = 150;
    o7w = 15;
    o7h = 15;
    o8x = 350;
    o8y = 200;
    o8w = 15;
    o8h = 15;
    keyCode = 0;
  }
}

void lightBooleans() {
  //Determine if all the ornaments are on the tree
 if ((o1x >80 && o1x <320 && o1y > 10 && o1y < 310) && (o2x >80 && o2x <320 && o2y > 10 && o2y < 310)
 && (o3x >80 && o3x <320 && o3y > 10 && o3y < 310) && (o4x >80 && o4x <320 && o4y > 10 && o4y < 310)
 && (o5x >80 && o5x <320 && o5y > 10 && o5y < 310) && (o6x >80 && o6x <320 && o6y > 10 && o6y < 310)
 && (o7x >80 && o7x <320 && o7y > 10 && o7y < 310) && (o8x >80 && o8x <320 && o8y > 10 && o8y < 310)) {
   light1 = true;
 } else {
  light1 = false; 
 }
  
}