Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Corn Popper by Shane Logtenberg 991435131
//Click and drag the handle to move corn popper. balls will bounce in dome.
//"Enjoy your toy, baby."

int aX = 150;
int handleX = 2;
int handleY = 126;
int handleX2 = 28;
int handleY2 = 153;
boolean click = false;
int pX;
float x0= aX, x1= aX, x2 = aX, x3 = aX;
float y0= 315, y1= 315, y2 = 315, y3 = 315;
float yspeed0, yspeed1, yspeed2 = 0, yspeed3 = 0;
float xspeed0, xspeed1, xspeed2 = 0, xspeed3 = 0;
float gravity =0.9;
boolean click0, click1, click2, click3 = false;
void setup() {
  size (400, 400);
} 
void draw() {
  //1 Make background
  carpet();
  //2 Draw initial Corn Popper position depending on handle position
  aX = handleX+148;
  popper(aX);
  //3 draw popping balls
  drawBalls();
  mouseClick();
  moveRed();
  moveYellow();
  moveBlue();
  moveGreen();
}
// If mouse is over handle, and is clicked, then handle is held
void mouseDragged() {
  if (pmouseX >= handleX && pmouseX <= handleX2 && pmouseY >= handleY && pmouseY <= handleY2) {
    click = true;
  }
  //If handle is held, moving the mouse will move the handle and corn popper.
  if (click == true) {
    handleX = pmouseX;
    handleX2 = pmouseX + 26;
  }
  // Keep handle and corn popper on screen
  if (handleX > 202) {
    handleX = 202;
    handleX2 = 228;
  }
  if (pmouseX < 0) {
    handleX = 2;
    handleX2 = 28;
  }
}
//If mouse is released, handle is not held
void mouseReleased() {
  click = false;
}

void popper(int pX) {  
  ellipseMode(CENTER);
  rectMode(CORNER);
  // draw handle
  fill(0, 153, 204);
  stroke(0);
  strokeWeight(1);
  quad(pX-130, 140, pX-140, 140, pX-50, 340, pX-50, 320);
  ellipse(pX-135, 140, 25, 25);
  // draw popper
  fill(255, 255, 255);
  ellipse(pX, 350, 60, 60);
  rect(pX-50, 315, 100, 25);
  arc(pX, 340, 100, 20, 0, PI);
  noFill();
  arc(pX, 315, 100, 20, 0, PI);
  // draw dome
  stroke(0, 153, 204, 70);
  fill(0, 153, 204, 20);
  arc(aX, 320, 100, 140, PI, 2*PI);
  arc(aX, 320, 80, 10, 0, PI);
  // draw wheel
  stroke(0);
  fill(255, 0, 0);
  ellipse(pX, 372, 40, 40);
  stroke(170, 0, 0);
  strokeWeight(3);
  ellipse(pX, 372, 20, 20);
  //draw highlight
  noFill();
  stroke(255);
  strokeWeight(1);
  arc(aX, 320, 90, 120, PI*random(1.4, 1.45), PI*random(1.7, 1.75));
  arc(pX, 372, 30, 30, PI*random(1.3, 1.4), PI*random(1.7, 1.8));
  line(pX-125, 160, pX-52, 325);
  arc(pX-135, 140, 20, 20, PI*random(1.4, 1.5), PI*random(1.8, 1.9));
}
void carpet() {
  background(204, 255, 255);
  stroke(198, 127, 0);
  strokeWeight(3);
  fill(153, 102, 0);
  rect(-3, 280, width+5, 320);
  line(0, 290, width, 290);
  //Loop dots of variety of opacity to make texture for carpet (use for and randomSeed)
  randomSeed(90);
  for (int carpetx=0; carpetx < width; carpetx+= 2) {
    for (int carpety=320; carpety < height; carpety+= 2) {
      noStroke();
      fill(random(200, 240), random(200, 230), random(150, 170));
      rect(carpetx, carpety, carpetx+2, carpety+2);
    }
  }
}
void mouseClick() {
  //So long as the mouse is held down, the balls will be launched when landing
  if (click0 == false && click == true && y0 >= 315) {
    click0 = true;
  }
  if (click1 == false && click == true && y1 >= 315) {
    click1 = true;
  }
  if (click2 == false && click == true && y2 >= 315) {
    click2 = true;
  }
}
void drawBalls() {
  strokeWeight(1);
  stroke(255);
  //draw Blue ball
  fill(0, 105, 205);
  ellipse(x2, y2, 20, 20);
  //draw Red ball
  fill(255, 0, 0);
  ellipse(x0, y0, 20, 20);
  //draw Yellow ball
  fill(255, 255, 0);
  ellipse(x1, y1, 20, 20);
  //draw Green ball
  fill(0,205,0);
  ellipse(x3, y3, 20, 20);
}
void moveRed() {
  //When the ball is launched, gravity will act on it
  if (y0<315) {
    click0 = false;
  }
  //Ball is launched at random angle
  if (click0==true) {
    y0 = y0 + yspeed0;
    yspeed0 = -8;
    xspeed0 = xspeed0 + int(random(-5, 5));
  }  
  if (y0<315 && click0 == false) {
    x0 = x0 + xspeed0;
  }
  if (click0==false) {
    y0 = y0 + yspeed0;
    yspeed0 = yspeed0 + gravity;
  }
  //If ball reaches the base, and mouse isn't pushed, move to designated spot
  if (x0==aX+20 && y0==315) {
    xspeed0 = 0;
  }
  if (x0>aX+22 && y0==315) {
    x0 = x0 + xspeed0;
    xspeed0 = -5;
  }
  if (x0<aX+18 && y0==315) {
    x0 = x0 + xspeed0;
    xspeed0 = +5;
  }
  if (y0>315) {
    yspeed0 = 0;
    y0=315;
  }
  //If ball hits dome, bounce back.
  if (x0 < aX-30) {
    xspeed0 = 5;
  } 
  if (x0 < aX-40) {
    x0 = aX-40;
  }
  if (x0 > aX+30) {
    xspeed0 = -5;
  }
  if (x0 > aX+40) {
    x0 = aX+40;
  }
}
void moveYellow() {
  //When the ball is launched, gravity will act on it
  if (y1<315) {
    click1 = false;
  }
  //Ball is launched at random angle
  if (click1==true) {
    y1 = y1 + yspeed1;
    yspeed1 = -8;
    xspeed1 = xspeed1 + int(random(-5, 5));
  }
    
  if (y1<315 && click1 == false) {
    x1 = x1 + xspeed1;
  }
  if (click1==false) {
    y1 = y1 + yspeed1;
    yspeed1 = yspeed1 + gravity;
  }
  //If ball reaches the base, and mouse isn't pushed, move to designated spot
  if (x1==aX-20 && y1==315) {
    xspeed1 = 0;
  }
  if (x1>aX-18 && y1==315) {
    x1 = x1 + xspeed1;
    xspeed1 = -5;
  }
  if (x1<aX-22 && y1==315) {
    x1 = x1 + xspeed1;
    xspeed1 = +5;
  }
  if (y1>315) {
    yspeed1 = 0;
    y1=315;
  }
  //If ball hits dome, bounce back.
  if (x1 < aX-30) {
    xspeed1 = 5;
  }
  if (x1 < aX-40) {
    x1 = aX-40;
  }
  if (x1 > aX+30) {
    xspeed1 = -5;
  }
  if (x1 > aX+40) {
    x1 = aX+40;
  }
}
void moveBlue() {
  //When the ball is launched, gravity will act on it
  if (y2<315) {
    click2 = false;
  }
  //Ball is launched at random angle
  if (click2==true) {
    y2 = y2 + yspeed2;
    yspeed2 = -8;
    xspeed2 = xspeed2 + int(random(-5, 5));
  }
  
  if (y2<315 && click2 == false) {
    x2 = x2 + xspeed2;
  }
  if (click2==false) {
    y2 = y2 + yspeed2;
    yspeed2 = yspeed2 + gravity;
  }
//If ball reaches the base, and mouse isn't pushed, move to designated spot
  if (x2==aX && y2==315) {
    xspeed2 = 0;
  }
  if (x2>aX+2 && y2==315) {
    x2 = x2 + xspeed2;
    xspeed2 = -5;
  }
  if (x2<aX-2 && y2==315) {
    x2 = x2 + xspeed2;
    xspeed2 = +5;
  }
  if (y2>315) {
    yspeed2 = 0;
    y2=315;
  }
  //If ball hits dome, bounce back.
  if (x2 < aX-30) {
    xspeed2 = 5;
  } 
   if (x2 < aX-40) {
    x2 = aX-40;
  } 
  if (x2 > aX+30) {
    xspeed2 = -5;
  }
  if (x2 > aX+40) {
    x2 = aX+40;
  }
}
void moveGreen() {
  //When the ball is launched, gravity will act on it
  if (y3<315) {
    click2 = false;
  }
  //Ball is launched at random angle
  if (click2==true) {
    y3 = y3 + yspeed3;
    yspeed3 = -8;
    xspeed3 = xspeed3 + int(random(-5, 5));
  }
  
  if (y3<315 && click2 == false) {
    x3 = x3 + xspeed3;
  }
  if (click2==false) {
    y3 = y3 + yspeed3;
    yspeed3 = yspeed3 + gravity;
  }
//If ball reaches the base, and mouse isn't pushed, move to designated spot
  if (x3==aX && y3==315) {
    xspeed3 = 0;
  }
  if (x3>aX+2 && y3==315) {
    x3 = x3 + xspeed3;
    xspeed3 = -5;
  }
  if (x3<aX-2 && y3==315) {
    x3 = x3 + xspeed3;
    xspeed3 = +5;
  }
  if (y3>315) {
    yspeed3 = 0;
    y3=315;
  }
  //If ball hits dome, bounce back.
  if (x3 < aX-30) {
    xspeed3 = 5;
  } 
   if (x3 < aX-40) {
    x3 = aX-40;
  } 
  if (x3 > aX+30) {
    xspeed3 = -5;
  }
  if (x3 > aX+40) {
    x3 = aX+40;
  }
}