Your browser does not support the canvas tag.

previous        Show / Hide Source    Download        next
// ASSIGNMENT 2
// INTERACTIVE TOY
// PONDER

// JUSTIN LUOMA-REDDY

float frogX = 100;
float frogY = 200;
float frogX2 = 75;
float frogY2 = 100;
float frogX3 = 375;
float frogY3 = 50;
float velocityY = 0.1;
float velocityX = 0.5;
float velocityY2 = 0.1;
float velocityX2 = 0.5;
float velocityY3 = 0.1;
float velocityX3 = 0.5;
float acceleration = 0.01;
boolean moveUp = false;
boolean moveDown = false;
boolean moveLeft = false;
boolean moveRight = false;
float lilyX = 200;
float lilyY = 380;
int state = 0;



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

void draw(){
  background(200);
  scenery();
  lilyPad(lilyX,lilyY);
  frog();
  frog2();
  frog3();
  
}


// The following code controls the x,y position of frog 1 as well as its velocity.
void frog(){
  fill (100,255,100);
  frogModel(frogX,frogY,100,255,100);
  frogY = frogY + velocityY;
  velocityY = velocityY + acceleration;
  frogX = frogX + velocityX;
  
  // the following sets the conditional vertical behavior of frog 1 when it touches the lily pad (platform) or reaches the top of the window
  if ((frogY > lilyY - 20) && (frogY < lilyY + 10) && (frogX > lilyX - 10) && (frogX < lilyX + 110) && (state == 0)){
    velocityY = velocityY * random(-1.05,-0.95);
    frogY = lilyY - 20;
    
    // The following code controls the conditional horizontal behavior of frog 1 when it touches the lily pad (platform) or reaches the left or right edge of the window.
    if ((random(1,10))<5){
      velocityX = velocityX * -1;
    }
  }
  if (frogY < 0){
    frogY = 0;
  }
    
  if (frogX < 20){
    velocityX = velocityX * -1;
  }
  
  if (frogX > 380){
    velocityX = velocityX * -1;
  }
}

// The following controls the x,y position of frog 2 as well as its velocity.
void frog2(){
  fill (100,255,100);
  frogModel(frogX2,frogY2,205,155,25);
  frogY2 = frogY2 + velocityY2;
  velocityY2 = velocityY2 + acceleration;
  frogX2 = frogX2 + velocityX2;
  
  // the following sets the conditional vertical behavior of frog 2 when it touches the lily pad (platform) or reaches the top of the window
  if ((frogY2 > lilyY - 20) && (frogY2 < lilyY + 10) && (frogX2 > lilyX - 10) && (frogX2 < lilyX + 110) && (state == 1)){
    velocityY2 = velocityY2 * random(-1.05,-0.95);
    frogY2 = lilyY - 20;
    
    // The following code controls the conditional horizontal behavior of frog 2 when it touches the lily pad (platform) or reaches the left or right edge of the window.
    if ((random(1,10))<5){
      velocityX2 = velocityX2 * -1;
    }
  }
  if (frogY2 < 0){
    frogY2 = 0;
   }
  if (frogX2 < 20){
    velocityX2 = velocityX2 * -1;
  }
  
  if (frogX2 > 380){
    velocityX2 = velocityX2 * -1;
  }
}

// The following controls the x,y position of frog 3 as well as its velocity.
void frog3(){
  fill (100,255,100);
  frogModel(frogX3,frogY3,255,5,150);
  frogY3 = frogY3 + velocityY3;
  velocityY3 = velocityY3 + acceleration;
  frogX3 = frogX3 + velocityX3;
  
  // the following sets the conditional vertical behavior of frog 3 when it touches the lily pad (platform) or reaches the top of the window
  if ((frogY3 > lilyY - 20) && (frogY3 < lilyY + 10) && (frogX3 > lilyX - 10) && (frogX3 < lilyX + 110) && (state == 2)){
    velocityY3 = velocityY3 * random(-1.05,-0.95);
    frogY3 = lilyY - 20;
    
    // The following code controls the conditional horizontal behavior of frog 3 when it touches the lily pad (platform) or reaches the left or right edge of the window.
    if ((random(1,10))<5){
      velocityX3 = velocityX3 * -1;
    }
  }
  if (frogY3 < 0){
    frogY3 = 0;
   }
  if (frogX3 < 20){
    velocityX3 = velocityX3 * -1;
  }
  
  if (frogX3 > 380){
    velocityX3 = velocityX3 * -1;
  }
}


// The following code assembles the visual frog model to be used by the frog function. The frog function determines the models location at any given time.
void frogModel(float x,float y,int a,int b,int c){
  fill(a-100,b-100,c-100);
  rect(x,y,10,10);
  fill(a,b,c);
  rect(x-5,y-15,20,15);
  fill(255);
  rect(x-2.5,y-25,5,10);
  rect(x+7.5,y-25,5,10);
  fill(0);
  rect(x-2.5,y-25,3,7);
  rect(x+7.5,y-25,3,7);
  fill(a,b,c);
  rect(x-10,y+10,30,5);
  quad(x,y+5,x-10,y+2.5,x-5,y+10,x,y+10);
  quad(x+10,y+5,x+20,y+2.5,x+15,y+10,x+10,y+10);
  fill(225,255,225);
  rect(x,y+10,5,5);
  rect(x+5,y+10,5,5);
}

// The following code assembles the visual lily pad (platform) model.
void lilyPad(float x,float y){
  
  fill(state*255-50,255-state*100-50,state*125-100);
  ellipse(x+50,y-8,130,35);
  
  fill(state*255,255-state*100,state*125);
  beginShape();
  vertex(x,y);
  vertex(x+30,y-8);
  vertex(x-5,y-6);
  vertex(x+10,y-15);
  vertex(x+35,y-12);
  vertex(x+15,y-20);
  vertex(x+85,y-20);
  vertex(x+65,y-12);
  vertex(x+90,y-15);
  vertex(x+105,y-6);
  vertex(x+70,y-8);
  vertex(x+100,y);
  endShape(CLOSE);
  
  // The following code constrains the x and y position of the lily pad (platform) within the bottom portion of the window.
  lilyX = constrain(lilyX,20,width-120);
  lilyY = constrain(lilyY,300,380);
  
  // The follwing code controls the motion and speed of the lily pad (platform) when the keys 'w','a','s', and or 'd' are pressed or released.  The evaluation of the boolean statements is determined by the key pressed.
  if (moveUp == true){
      lilyY = lilyY - 5;
    }
    if (moveDown == true){
      lilyY = lilyY + 5;
    }
    if (moveLeft == true){
      lilyX = lilyX - 5;
    }
    if (moveRight == true){
      lilyX = lilyX + 5;
    }
    
    
  if (moveUp == false){
      lilyY = lilyY;
    }
    if (moveDown == false){
      lilyY = lilyY;
    }
    if (moveLeft == false){
      lilyX = lilyX;
    }
    if (moveRight == false){
      lilyX = lilyX;
    }
  
}

// The following code creates the lily pad (scenic) visual models (of varying size) to be used in the function scenery().
void lilyPad2(float x, float y){
  fill(120,255,120);
  ellipse(x,y,20,5);
}
void lilyPad3(float x, float y){
  fill(120,255,120);
  ellipse(x,y,25,7.5);
}
void lilyPad4(float x, float y){
  fill(120,255,120);
  ellipse(x,y,30,10);
}


// The following code creates the stone visual model to be used in the function scenery().
void stone(float x,float y,float w,float h){
  
  fill(255,255,255,150);
  rect(x-5,y+h-2.5,w+10,8);
  rect(x-10,y+h-5,w+20,15);
  fill(150);
  rect(x,y,w,h);
  fill(150,255,159,180);
  rect(x,y,w,5);
  fill(255,255,255,150);
  
}

// The following code assembles various visual elements that construct the scenic background to be called upon as a single function ,scenery(), in draw().
void scenery(){
  stroke(0);
  float s = 0;
  fill(50,10,5);
  
  // The following code utilizes and loop to create the row of rectangular tree trunks in the background.
  while (s<width){
    rect(0+s,0,20,200);
    s = s +30;
  }
  
  // The following code utilizes a loop to create the water and gradient effect. The rectangles are closer together towards the centre of the window.
  float d = 0;
  fill(100,100,255,100);
  while (d<150){
    rect(0,200+d,400,200);
    d = d*2 + 8;
  }
 
  // The following code creates the sense of foliage in the background through the use of repeated ellipses.
  noStroke();
  fill(50,100,50);
  rect(0,0,400,80);
  fill(0,150,0,150);
  ellipse(0,0,150,80);
  ellipse(200,50,200,100);
  ellipse(0,110,50,30);
  ellipse(260,10,50,30);
  ellipse(50,50,100,20);
  ellipse(100,90,100,20);
  ellipse(20,50,80,20);
  ellipse(200,70,90,30);
  ellipse(180,10,140,30);
  ellipse(50,0,150,50);
  ellipse(110,30,60,30);
  ellipse(220,80,40,20);
  ellipse(230,140,20,10);
  ellipse(70,70,60,45);
  ellipse(45,60,40,30);
  fill(150,200,150,50);
  ellipse(200,105,40,30);
  ellipse(155,120,50,25);
  ellipse(110,110,55,25);
  ellipse(20,70,20,20);
  ellipse(195,60,60,30);
  
  // The following code creates the waterfall and stone visuals.
  stroke(0);
  stone(0,180,400,20);
  stone(50,185,30,30);
  stone(0,150,50,90); 
  fill(160);
  rect(270,0,130,200);
  fill(180,200,255);
  rect(300,0,40,200);
  fill(255,255,255,random(80,120));
  rect(260,200,200,20);
  rect(260,180,200,40);
  rect(260,170,200,50);
  stone(150,200,25,15);
  stone(220,190,50,30);
  stone(330,140,100,80);
  
  // The following code displays the scenic lily pads in the mid-ground/background
  lilyPad2(250,250);
  lilyPad2(200,260);
  lilyPad3(30,275);
  lilyPad2(150,240);
  lilyPad3(70,265);
  lilyPad3(135,255);
  lilyPad2(300,250);
  lilyPad2(110,235);
  lilyPad2(320,260);
  lilyPad4(5,330);
  lilyPad4(60,300);
  lilyPad2(100,320);
  lilyPad4(350,395);
  lilyPad4(370,370);
}
  
 
// The following code controls the value of the current state when the space bar is pressed.  This in turn will be used as a conditional in determining the motion behaviour of each frog.
void keyPressed(){
  if((state == 0)&&(key == ' ')){
    state = 1;
  }
  else if((state == 1)&&(key == ' ')){
    state = 2;
  }
  else if((state == 2)&&(key == ' '))
  state = 0;
  
  // The following code changes the evaluation of a boolean statement when a particular key is pressed.  These keys in turn control the postiion and motion of the lily pad (platform).
  if (key == 'w'){
    moveUp = true;
  }
  if (key == 's'){
    moveDown = true;
  }
  if (key == 'a'){
    moveLeft = true;
  }
  if (key == 'd'){
    moveRight = true;
  }
    
  // The following code resets the y position and velocity of each frog.
  if (key == 'r'){
    frogY = 200;
    velocityY = 0;
    frogY2 = 100;
    velocityY2 = 0;
    frogY3 = 50;
    velocityY3 = 0;
  }
}

// The following code ensures that the lily pad (platform) stops moving once a key is released by changing the evaluation of a boolean statement to false.
void keyReleased(){
  if (key == 'w'){
    moveUp = false;
  }
  if (key == 's'){
    moveDown = false;
  }
  if (key == 'a'){
    moveLeft = false;
  }
  if (key == 'd'){
    moveRight = false;
  }
}