Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/////////////////////////////////////////////////
/////////////////////////////////////////////////
//PB and Jellyfish by Jessica Cornelius
//Move the JellyFish around using WASD or ARROW keys.
/////////////////////////////////////////////////
/////////////////////////////////////////////////

//Declaring Variables

float jellyX;
float jellyY;
int currentXPosition = 0;

boolean goLeft=false;
boolean goRight=false;
boolean goUp=false;
boolean goDown=false;
boolean move=false;

float speed=5.0;

//Setting Up Canvas

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

void draw() {
  
  background(141, 255, 254);
  drawWater();
  drawMove();
  drawCurrent(currentXPosition);
  drawSand();
  drawSeaweedBehind();
  drawSquish();
  drawSeaweedFront();
  noStroke();

  currentXPosition++;
  
}

void keyPressed() {

  //Movement Code Retrieved from http://www-acad.sheridanc.on.ca/PROG14998/2014/interactive-toy/interactive_toy_43/index.html
  //Shape Change Code Retrieved from http://www-acad.sheridanc.on.ca/PROG14998/2014/interactive-toy/interactive_toy_21/index.html

  /////////////////////////
  //MOVEMENT FOR ARROW KEYS
  /////////////////////////

  if (keyCode == LEFT) {
    goLeft=true;
  } else if (keyCode == UP) {
    goUp=true;
  } else if (keyCode == DOWN) {
    goDown=true;
  } else if (keyCode == RIGHT) {
    goRight=true;
  }

  /////////////////////////
  //MOVEMENT FOR WASD
  /////////////////////////

  if (key=='a') {
    goLeft=true;
  } else if (key=='w') {
    goUp=true;
  } else if (key=='s') {
    goDown=true;
  } else if (key=='d') {
    goRight=true;
  }
}

void keyReleased() {

  /////////////////////////
  //TO DISABLE MOVEMENT OF WASD AND ARROW KEYS WHEN RELEASED
  /////////////////////////

  if (key=='a') {
    goLeft=false;
  } else if (key=='w') {
    goUp=false;
  } else if (key=='s') {
    goDown=false;
  } else if (key=='d') {
    goRight=false;
  }

  if (keyCode == LEFT) {
    goLeft=false;
  } else if (keyCode == UP) {
    goUp=false;
  } else if (keyCode == DOWN) {
    goDown=false;
  } else if (keyCode == RIGHT) {
    goRight=false;
  }
}

/////////////////////////////////////////////////
//DRAW
/////////////////////////////////////////////////

/////////////////////////
//WATER
/////////////////////////

void drawWater() {
  
  fill(151, 223, 248);
  rect(200, 70, 400, 65);

}

/////////////////////////
//JELLYFISH
/////////////////////////

//JELLYFISH SHAPE WHEN NOT MOVING

void bigJelly() {
  noStroke();
  fill(random(0,255), random(0,255), random(0,255));
  rectMode(CENTER);
  rect(jellyX+200, jellyY+200, 45, 35, 20);
  rect(jellyX+200, jellyY+215, 45, 10, 10);
  rect(jellyX+190, jellyY+215, 5, 45, 20);
  rect(jellyX+210, jellyY+215, 5, 45, 20);
  rect(jellyX+200, jellyY+215, 5, 45, 20);
  fill(random(0,255), random(0,255), random(0,255));
  ellipse(jellyX+190, jellyY+195, 10, 10);
  ellipse(jellyX+200, jellyY+192, 5, 5);
  ellipse(jellyX+185, jellyY+202, 5, 5);
  
 //KEEPING JELLYFISH ON CANVAS
 //Retrieved from http://www-acad.sheridanc.on.ca/PROG14998/2014/interactive-toy/interactive_toy_05/index.html
 
  jellyX = constrain(jellyX, -200, 200);                 
  jellyY = constrain(jellyY, -80, 200);
}

//JELLYFISH SHAPE WHEN MOVING

void smallJelly() {
  noStroke();
  //fill(246, 135, 248);
  fill(random(0,255), random(0,255), random(0,255));
  rectMode(CENTER);
  rect(jellyX+200, jellyY+200, 45, 25, 20);
  rect(jellyX+200, jellyY+215, 45, 10, 10);
  rect(jellyX+190, jellyY+215, 5, 30, 20);
  rect(jellyX+210, jellyY+215, 5, 30, 20);
  rect(jellyX+200, jellyY+215, 5, 30, 20);
  //fill(232, 90, 235);
  fill(random(0,255), random(0,255), random(0,255));
  ellipse(jellyX+190, jellyY+195, 10, 8);
  ellipse(jellyX+200, jellyY+192, 5, 3);
  ellipse(jellyX+185, jellyY+202, 5, 3);
  jellyX = constrain(jellyX, -200, 200);                 
  jellyY = constrain(jellyY, -80, 200);
}

void drawMove() {

  //ENABLES MOVEMENT OF JELLYFISH SHAPE&SPEED

  if (goRight==true) {
    jellyX+=speed;
  }
  if (goLeft==true) {
    jellyX+=-speed;
  }
  if (goUp==true) {
    jellyY+=-speed;
  }
  if (goDown==true) {
    jellyY+=speed;
  }
  if (goRight == true || goLeft == true || goUp == true || goDown == true) {
    move = true;
  } else {
    move = false;
  }
}

//JELLYFISH WILL NOT SQUISH UNLESS MOVING

void drawSquish() {
  if (move == true) {
    smallJelly();
  }else{
    bigJelly();
  }
}

/////////////////////////
//SAND
/////////////////////////

 void drawSand() {
 stroke(234, 234, 208);
 float a = 0.0;
 float inc = TWO_PI/25.0;
 for (int x = 0; x <= width; x++) {
   line(x, height, x , height - 30 + 20*(sin(a/30)));
   a = a + inc;
 }
 }
 
/////////////////////////
//CURRENT
/////////////////////////

void drawCurrent(int x) {
 stroke(223, 255, 255);
 float a = 0.0;
 float inc = TWO_PI/25.0;
 for (int i = 0; i <= width; i++) {
   line(i, height, i , (height - 350 + 20 * (sin((a-x/2)/20))));
   a = a + inc;
 }
  stroke(200, 255, 255);
 for (int i = 0; i <= width; i++) {
   line(i, height, i , (height - 350 + 20 * (sin((a-x/4)/20))));
   a = a + inc;
 }
 noStroke();
 rectMode(CORNER);
 for (int j = 0; j <= height; j= j+5){
   fill(40, 110, 154, 0+j);
   rect(0, 150+j, width, 5);
 }
}

/////////////////////////
//SEAWEED
/////////////////////////

void drawSeaweedBehind() {
  float s = 0;
  strokeWeight(5);
  while(s<height){
  stroke(141, 227, 137);
  point(80, 300 + 200 * sin(s*10)+ 50);
  stroke(177, 224, 174);
  point(100, 280 + 200 * sin(s*10)+ 50);
  stroke(141, 227, 137);
  point(120, 380 + 200 * sin(s*10)+ 50);
  stroke(141, 227, 137);
  point(375, 400 + 200 * sin(s*10)+ 50);
  stroke(177, 224, 174);
  point(25, 400 + 200 * sin(s*10)+ 50);
  s = s + 5;
}
}

void drawSeaweedFront() {
  float s = 0;
  strokeWeight(5);
  while(s<height){
  stroke(141, 227, 137);
  point(280, 300 + 200 * sin(s*10)+ 50);
  stroke(177, 224, 174);
  point(300, 280 + 200 * sin(s*10)+ 50);
  stroke(141, 227, 137);
  point(220, 380 + 200 * sin(s*10)+ 50);
  s = s + 5;
}
}