Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/////Pinata Time///// //<>//
/* 
 Oct 1st 2017 
 Intro to Media Computation
 Instructor: Fredd Eyles  
 Created by: Charmaine Wong/ Ho Wing Wong 
 
 Keep hitting the pinata to make it explode out candy! 
 */

//user defined variables for the candy backdrop  
float candyX = random(0, 400);
float candyY = random(-200, 0);

float chocoX=random(0, 400);
float chocoY=random(-200, 0);

float cakeX=random(0, 400);
float cakeY=random(-200, 0);


//bat that replaces the cursor 
float batX;
float batY;

//hit detection, the amount of times hit until the player wins 
float pinataHealth=5000;
float damage=1000;


boolean candyDrop = false;
boolean win;



void setup() {
  size(400, 400); 
  smooth(); 
  frameRate(60);
  candyY=10;
  chocoY=10;
  cakeY=10;
  win = false;
}

void draw() {
  //default background colour
  frameRate(60);
  background(248, 216, 231);
  rectMode(CORNER);
  drawCandy();
  drawChoco();
  drawCake();
  updateCandy();
  reset(); 
  state(); 



  if (pinataHealth>0) {
    drawPinata();
  }

  drawBat();
}
//Candy

void drawCandy() 
{  
  if (candyDrop == false) {

    fill(140, 117, 159);
    rect(candyX, candyY+60, 20, 20);

    triangle(candyX, candyY+40, candyX+20, candyY+40, candyX+10, candyY+60);

    triangle(candyX, candyY+100, candyX+10, candyY+80, candyX+20, candyY+100);

    rect(candyX-270, candyY+80, 20, 20);

    triangle(candyX-270, candyY+70, candyX-250, candyY+70, candyX-260, candyY+80);
    triangle(candyX-270, candyY+110, candyX-250, candyY+110, candyX-260, candyY+100);
  }
} 
void drawChoco() {
  if (candyDrop == false) {
    rectMode(CENTER); 
    fill(197, 135, 174); 
    stroke(197, 135, 174); 


    triangle(chocoX, chocoY+70, chocoX+30, chocoY+70, chocoX+15, chocoY+85);
    triangle(chocoX, chocoY+130, chocoX+15, chocoY+115, chocoX+30, chocoY+130);
    rect(chocoX+15, chocoY+100, 13, 40);
  }
}
void drawCake() {
  {
    if (candyDrop == false) {
      ellipseMode(CENTER);

      ellipse(cakeX+170, cakeY+250, 20, 25);

      ellipse(cakeX-30, cakeY+330, 20, 25);
    }
  }


  if (candyY>height) {
    candyY=-30;
    candyX = random(0, 400);
  }

  if (chocoY>height) {
    chocoY=-30;
    chocoX=random(0, 400);
  }

  if (cakeY>height) {
    cakeY=-30;
    cakeX=random(0, 400);
  }
}

void updateCandy() {
  //moving parts 
  candyY = candyY +1;
  chocoY= chocoY+1;
  cakeY= cakeY+2;

  if ( pinataHealth<=0)
  {
    //chocoY=chocoY+4;
    //candyY=candyY+4;
    //cakeY=cakeY+4;
    //removed since it didn't create an exciting explosion effect I wanted 

    chocoY=random(-20, 400);
    chocoX= random(0, 400);
    candyY=random(-20, 400);
    candyX= random(0, 400);
    cakeX=random(0, 400); 
    cakeY=random(-20, 400);
  }
}


//random 'spawn' of the item in different areas 

{
  if (candyY>height)
  { 
    candyX = random (0, 400);
    candyY = random (-100, -10);
  }
}

{
  if (chocoY>height)
  { 
    chocoX = random (0, 400);
    chocoY = random (-100, -10);
  }

  if (cakeY>height)
  { 
    cakeX = random (0, 400);
    cakeY = random (-100, -10);
  }
}
void drawPinata() {
  //Original Pinata

  //line connecting to pinata 
  strokeWeight(7);
  line(160, 0, 160+sin(frameCount/20.0)*30, 160+abs(cos(frameCount/20.0)*10));

  //back ear 
  strokeWeight(1); 
  stroke(197, 135, 174);
  fill(197, 135, 174);
  quad(70+sin(frameCount/20.0)*30, 53+abs(cos(frameCount/20.0)*10), 80+sin(frameCount/20.0)*30, 50+abs(cos(frameCount/20.0)*10), 90+sin(frameCount/20.0)*30, 100+abs(cos(frameCount/20.0)*10), 70+sin(frameCount/20.0)*30, 120+abs(cos(frameCount/20.0)*10));
  //back legs 
  //left leg 
  rectMode(CORNER);
  rect(130+sin(frameCount/20.0)*30, 240+abs(cos(frameCount/20.0)*10), 20, 30);

  //right leg
  rectMode (CORNER); 
  rect(190+sin(frameCount/20.0)*30, 240+abs(cos(frameCount/20.0)*10), 20, 30);


  //hooves 
  stroke(140, 117, 159); 
  fill(140, 117, 159);

  rectMode(CORNER);
  rect(130+sin(frameCount/20.0)*30, 270+abs(cos(frameCount/20.0)*10), 20, 10); 
  rect(105+sin(frameCount/20.0)*30, 280+abs(cos(frameCount/20.0)*10), 20, 10); 
  rect(195+sin(frameCount/20.0)*30, 270+abs(cos(frameCount/20.0)*10), 20, 10);
  rect(215+sin(frameCount/20.0)*30, 280+abs(cos(frameCount/20.0)*10), 20, 10);

  stroke(245, 184, 199);
  fill(245, 184, 199);
  beginShape();
  vertex(90+sin(frameCount/20.0)*30, 30+abs(cos(frameCount/20.0)*10));
  vertex(100+sin(frameCount/20.0)*30, 30+abs(cos(frameCount/20.0)*10));
  vertex(130+sin(frameCount/20.0)*30, 120+abs(cos(frameCount/20.0)*10));
  vertex(130+sin(frameCount/20.0)*30, 160+abs(cos(frameCount/20.0)*10));
  vertex(220+sin(frameCount/20.0)*30, 160+abs(cos(frameCount/20.0)*10));
  vertex(240+sin(frameCount/20.0)*30, 180+abs(cos(frameCount/20.0)*10));
  vertex(240+sin(frameCount/20.0)*30, 280+abs(cos(frameCount/20.0)*10));
  vertex(210+sin(frameCount/20.0)*30, 280+abs(cos(frameCount/20.0)*10));
  vertex(210+sin(frameCount/20.0)*30, 240+abs(cos(frameCount/20.0)*10));
  vertex(130+sin(frameCount/20.0)*30, 240+abs(cos(frameCount/20.0)*10));
  vertex(130+sin(frameCount/20.0)*30, 280+abs(cos(frameCount/20.0)*10));
  vertex(100+sin(frameCount/20.0)*30, 280+abs(cos(frameCount/20.0)*10));
  vertex(100+sin(frameCount/20.0)*30, 240+abs(cos(frameCount/20.0)*10));
  vertex(90+sin(frameCount/20.0)*30, 220+abs(cos(frameCount/20.0)*10));
  vertex(90+sin(frameCount/20.0)*30, 160+abs(cos(frameCount/20.0)*10));
  vertex(60+sin(frameCount/20.0)*30, 140+abs(cos(frameCount/20.0)*10));
  vertex(60+sin(frameCount/20.0)*30, 120+abs(cos(frameCount/20.0)*10));
  vertex(90+sin(frameCount/20.0)*30, 100+abs(cos(frameCount/20.0)*10));
  vertex(90+sin(frameCount/20.0)*30, 30+abs(cos(frameCount/20.0)*10));
  // etc;
  endShape();

  //eyes
  fill(255); 
  strokeWeight(2); 
  stroke(123, 88, 153); 
  ellipseMode(CENTER); 
  ellipse(90+sin(frameCount/20.0)*30, 120+abs(cos(frameCount/20.0)*10), 15, 15); 

  //nose 
  noStroke(); 
  fill(197, 135, 174); 
  quad(60+sin(frameCount/20.0)*30, 120+abs(cos(frameCount/20.0)*10), 70+sin(frameCount/20.0)*30, 113+abs(cos(frameCount/20.0)*10), 70+sin(frameCount/20.0)*30, 148+abs(cos(frameCount/20.0)*10), 60+sin(frameCount/20.0)*30, 140+abs(cos(frameCount/20.0)*10)); 

  //nostrils
  strokeWeight(2); 
  stroke(123, 88, 153); 
  line(65+sin(frameCount/20.0)*30, 125+abs(cos(frameCount/20.0)*10), 65+sin(frameCount/20.0)*30, 135+abs(cos(frameCount/20.0)*10));

  // tails 
  noStroke(); 
  stroke(253, 238, 199); 
  fill(253, 238, 199); 

  //first tail 
  quad(230+sin(frameCount/20.0)*30, 160+abs(cos(frameCount/20.0)*10), 260+sin(frameCount/20.0)*30, 130+abs(cos(frameCount/20.0)*10), 260+sin(frameCount/20.0)*30, 150+abs(cos(frameCount/20.0)*10), 240+sin(frameCount/20.0)*30, 170); 
  quad(270+sin(frameCount/20.0)*30, 130+abs(cos(frameCount/20.0)*10), 300+sin(frameCount/20.0)*30, 145+abs(cos(frameCount/20.0)*10), 300+sin(frameCount/20.0)*30, 160+abs(cos(frameCount/20.0)*10), 270+sin(frameCount/20.0)*30, 150+abs(cos(frameCount/20.0)*10));

  //second tail 
  triangle(250+sin(frameCount/20.0)*30, 170+abs(cos(frameCount/20.0)*10), 270+sin(frameCount/20.0)*30, 160+abs(cos(frameCount/20.0)*10), 270+sin(frameCount/20.0)*30, 180+abs(cos(frameCount/20.0)*10));
  triangle(280+sin(frameCount/20.0)*30, 160+abs(cos(frameCount/20.0)*10), 310+sin(frameCount/20.0)*30, 190+abs(cos(frameCount/20.0)*10), 280+sin(frameCount/20.0)*30, 180+abs(cos(frameCount/20.0)*10)); 

  //third tail 
  triangle(255+sin(frameCount/20.0)*30, 180+abs(cos(frameCount/20.0)*10), 265+sin(frameCount/20.0)*30, 210+abs(cos(frameCount/20.0)*10), 245+sin(frameCount/20.0)*30, 190);
  triangle(270+sin(frameCount/20.0)*30, 190+abs(cos(frameCount/20.0)*10), 300+sin(frameCount/20.0)*30, 220+abs(cos(frameCount/20.0)*10), 275+sin(frameCount/20.0)*30, 210);

  //dark pink body
  stroke(240, 178, 215);
  fill(240, 178, 215);
  beginShape();
  vertex(90+sin(frameCount/20.0)*30, 160+abs(cos(frameCount/20.0)*10));
  vertex(220+sin(frameCount/20.0)*30, 160+abs(cos(frameCount/20.0)*10));
  vertex(240+sin(frameCount/20.0)*30, 180+abs(cos(frameCount/20.0)*10));
  vertex(240+sin(frameCount/20.0)*30, 200+abs(cos(frameCount/20.0)*10));
  vertex(90+sin(frameCount/20.0)*30, 200+abs(cos(frameCount/20.0)*10));
  vertex(90+sin(frameCount/20.0)*30, 160+abs(cos(frameCount/20.0)*10));
  // etc;
  endShape();

  //purple body 
  stroke(203, 206, 255);
  fill(203, 206, 255);

  beginShape();
  vertex(90+sin(frameCount/20.0)*30, 200+abs(cos(frameCount/20.0)*10));
  vertex(240+sin(frameCount/20.0)*30, 200+abs(cos(frameCount/20.0)*10));
  vertex(240+sin(frameCount/20.0)*30, 240+abs(cos(frameCount/20.0)*10));
  vertex(100+sin(frameCount/20.0)*30, 240+abs(cos(frameCount/20.0)*10));
  vertex(90+sin(frameCount/20.0)*30, 220+abs(cos(frameCount/20.0)*10));
  vertex(90+sin(frameCount/20.0)*30, 200+abs(cos(frameCount/20.0)*10));
  // etc;
  endShape();

  //details pinata

  stroke(251, 205, 190);
  fill(251, 205, 190);
  quad(120+sin(frameCount/20.0)*30, 180+abs(cos(frameCount/20.0)*10), 125+sin(frameCount/20.0)*30, 180+abs(cos(frameCount/20.0)*10), 130+sin(frameCount/20.0)*30, 190+abs(cos(frameCount/20.0)*10), 120+sin(frameCount/20.0)*30, 195+abs(cos(frameCount/20.0)*10));

  triangle(180+sin(frameCount/20.0)*30, 195+abs(cos(frameCount/20.0)*10), 190+sin(frameCount/20.0)*30, 210+abs(cos(frameCount/20.0)*10), 180+sin(frameCount/20.0)*30, 210+abs(cos(frameCount/20.0)*10));

  noStroke(); 
  stroke(253, 238, 199); 
  fill(253, 238, 199); 

  quad(160+sin(frameCount/20.0)*30, 225+abs(cos(frameCount/20.0)*10), 165+sin(frameCount/20.0)*30, 225+abs(cos(frameCount/20.0)*10), 170+sin(frameCount/20.0)*30, 240+abs(cos(frameCount/20.0)*10), 158+sin(frameCount/20.0)*30, 240+abs(cos(frameCount/20.0)*10));

  quad(105+sin(frameCount/20.0)*30, 240+abs(cos(frameCount/20.0)*10), 120+sin(frameCount/20.0)*30, 260+abs(cos(frameCount/20.0)*10), 110+sin(frameCount/20.0)*30, 260+abs(cos(frameCount/20.0)*10), 104+sin(frameCount/20.0)*30, 250+abs(cos(frameCount/20.0)*10));

  quad(220+sin(frameCount/20.0)*30, 240+abs(cos(frameCount/20.0)*10), 224+sin(frameCount/20.0)*30, 240+abs(cos(frameCount/20.0)*10), 230+sin(frameCount/20.0)*30, 260+abs(cos(frameCount/20.0)*10), 220+sin(frameCount/20.0)*30, 260+abs(cos(frameCount/20.0)*10));

  noStroke(); 
  stroke(210, 172, 209); 
  fill(210, 172, 209);

  quad(195+sin(frameCount/20.0)*30, 250+abs(cos(frameCount/20.0)*10), 200+sin(frameCount/20.0)*30, 250+abs(cos(frameCount/20.0)*10), 205+sin(frameCount/20.0)*30, 260+abs(cos(frameCount/20.0)*10), 195+sin(frameCount/20.0)*30, 260+abs(cos(frameCount/20.0)*10));
}

void drawBat() {
  stroke(140, 117, 159); 
  fill(140, 117, 159); 
  rectMode(CENTER);
  batX = mouseX;
  batY = mouseY;
  rect(batX, batY, 20, 80); 

  //hit dection for pinata

  if (batX >=90 && batX<=240 && batY>=160 && batY<=240 && mousePressed) {
    pinataHealth=pinataHealth-damage;
    frameRate(1);

    background(255, 105, 97); 



    //Line
    fill(140, 117, 159);
    strokeWeight(7);
    line(160, 0, 160+sin(frameCount/20.0)*30, 160+abs(cos(frameCount/20.0)*10));

    //
    //back ears 
    strokeWeight(1);
    stroke(197, 135, 174);
    fill(197, 135, 174);
    quad(70, 53, 80, 50, 90, 100, 70, 120);

    //hooves 
    rectMode(CORNER);
    rect(80, 270, 20, 10);
    rect(183, 270, 20, 10);
    rect(235, 270, 20, 10);
    quad(120, 265, 135, 255, 140, 260, 125, 270);

    //back legs 
    quad(100, 220, 130, 220, 105, 270, 75, 270);
    quad(180, 220, 220, 220, 255, 270, 230, 270);

    //teeth 
    fill(255); 
    stroke(253, 238, 199); 
    rectMode(CORNER);
    rect(50, 140, 10, 10);
    rect(40, 140, 10, 10);




    //pinata body when pressed 
    stroke(245, 184, 199);
    fill(245, 184, 199);
    beginShape();
    vertex(90, 30);
    vertex(100, 30);
    vertex(130, 120);
    vertex(130, 160);
    vertex(210, 160);
    vertex(250, 190);
    vertex(210, 270);
    vertex(180, 270);
    vertex(210, 220);
    vertex(110, 220);
    vertex(140, 250);
    vertex(115, 270);
    vertex(80, 225);
    vertex(80, 160);
    vertex(40, 160);
    vertex(40, 150);
    vertex(80, 150);
    vertex(80, 140);
    vertex(60, 140);
    vertex(60, 125);
    vertex(90, 100);
    vertex(90, 30);
    // etc;
    endShape();

    //nose
    fill(188, 213, 243);
    stroke(188, 213, 243);
    quad(60, 125, 70, 117, 70, 140, 60, 140);
    stroke(140, 117, 159);
    line(65, 125, 65, 135);

    //eyes 
    fill(140, 117, 159);
    noStroke();
    ellipseMode(CENTER); 
    ellipse(90, 120, 18, 18);

    fill(247, 183, 183);
    stroke(247, 183, 183);
    rectMode(CORNER);
    rect(80, 125, 20, 20);


    //details of pinata 
    //pinata tail 

    fill(133, 213, 202);
    stroke(133, 213, 202); 
    strokeWeight(2); 
    quad(225, 160, 240, 150, 250, 150, 240, 170);
    rectMode(CORNER);
    rect(240, 120, 10, 20);
    triangle(240, 110, 240, 90, 250, 110);

    quad(255, 150, 268, 150, 260, 180, 250, 170);
    quad(260, 140, 270, 120, 280, 120, 270, 140);
    triangle(255, 120, 260, 80, 265, 120);
    triangle(270, 110, 285, 90, 280, 110);
    triangle(280, 140, 300, 160, 270, 160);

    //peach body 
    fill(251, 205, 190);
    stroke(251, 205, 190);
    strokeWeight(2);
    quad(79, 161, 215, 160, 250, 190, 80, 190);

    //blue body 
    fill(170, 224, 211);
    stroke(170, 224, 211);
    quad(80, 190, 250, 190, 235, 220, 80, 220);

    //Pinata details 
    stroke(251, 205, 190);
    fill(251, 205, 190);
    quad(120, 180, 125, 180, 130, 190, 120, 195);

    triangle(180, 195, 190, 210, 180, 210);

    noStroke(); 
    stroke(253, 238, 199); 
    fill(253, 238, 199); 

    quad(90, 130, 96, 130, 100, 140, 92, 145);

    quad(105, 240, 120, 260, 110, 260, 104, 250);

    quad(220, 240, 224, 240, 230, 260, 220, 260);

    noStroke(); 
    stroke(210, 172, 209); 
    fill(210, 172, 209);

    quad(195, 250, 200, 250, 205, 260, 195, 260);

    if (damage>=1000) {
      damage=1000;
    }
  }
}



//determine win 
void state() {
  if (win==true) {
    chocoY=random(-20, 400);
    chocoX= random(0, 400);
    candyY=random(-20, 400);
    candyX= random(0, 400);
    cakeX=random(0, 400); 
    cakeY=random(-20, 400);
  }
}

//resetting program 
void reset() {
  if (keyPressed) {
    pinataHealth=5000;
    drawPinata();

    win=false;
  }
}