Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Penguin Run by Melissa Goldsmith
// journey to the arctic circle and help Penguin catch the fish

//press 'w' to move penguin right
// press 'a' to turn penguin around and left
//catch the fish and the difficulty will increase
//drop the fish and the difficluty willl decrease



float penguinX = 20;
float penguinY = 315;

float fishX = 20;
float fishY = 100;

float X = 0;
float Y = 0;

float penguinSpeed= 20;
float fishSpeed = 2;

void setup() {
  rectMode(CORNERS);
  size (400, 400);
  background(165, 242, 243);

  noStroke();
  fill(59, 48, 38);
  rect(0, 340, 400, 400);

  // fish spawns in random place above window
  fishX = random(0, 400);
}

void draw() {
  frameRate(20);

  //sky
  noStroke();
  fill(165, 242, 243);
  rect(0, 0, 400, 340);


  //loop/repeating pattern
  while (X < 400) {
    fill(255);
    noStroke();
    rect(0, 340, 400, 360);
    triangle(X, 360, X+35, 380, X+70, 360);
    ellipse(X, 375, 10, 10);
    fill(59, 48, 38);
    ellipse(X+35, 365, 10, 10);
    X=X+70;
  }

  //fish falls from sky
  fishY = fishY + fishSpeed;
  fish();

  //penguin is on screen even when no key is pressed
  penguin();
}

void keyPressed() {
  frameRate(20);


  //penguin hops right 
  if (keyPressed) {
    if (key == 'w') {
      if (penguinX < width) {

        //sky
        noStroke();
        fill(165, 242, 243);
        rect(0, 0, 400, 340);
        fish();
        penguin();
        penguinX = penguinX+3;
      } else {
        // if penguin leaves the window move it just off screen on the opposite side
        penguinX = penguinX-430;
      }
      //jump
      penguinY = penguinY - penguinSpeed;
      if ((penguinY > 0) || (penguinY <= 315)) {
        penguinSpeed=penguinSpeed* -1;
      }
    }
  }

  //Rpenguin hops left
  if (keyPressed) {
    if (key == 'a') {
      if (penguinX > 0) {
        noStroke();
        fill(165, 242, 243);
        rect(0, 0, 400, 340);
        fish();
        Rpenguin();
        penguinX = penguinX-3;
      } else {
        // if penguin leaves the window move it just off screen on the opposite side
        penguinX = 430;
      }
      //jump
      penguinY = penguinY - penguinSpeed;
      if ((penguinY > 0) || (penguinY <= 315)) {
        penguinSpeed=penguinSpeed* -1;
      }
    }
  }
}

// if penguin ends movment in the air, fall to the ground
void keyReleased() {
  if (penguinY < 315) {
    penguinY =315;
  }
  if (penguinSpeed < 0) {
    penguinSpeed=penguinSpeed* -1;
  }
}

void penguin() {
  //beak
  noStroke();
  fill(255, 195, 77);
  triangle(penguinX+10, penguinY-35, penguinX+22, penguinY-23, penguinX+10, penguinY-15);
  //foot
  fill(253, 86, 2);
  rect(penguinX, penguinY+22, penguinX+15, penguinY+25);
  fill(0);
  //body
  ellipse(penguinX, penguinY, 40, 50);
  //head
  ellipse(penguinX, penguinY-25, 33, 33);
  fill(255);
  //chest
  ellipse(penguinX+10, penguinY, 19, 33);
  //eye
  ellipse(penguinX-6, penguinY-25, 8, 8); 
  //wing
  fill(60);
  ellipse(penguinX-8, penguinY-3, 10, 20);
}

void Rpenguin() {
  //flip penguin aroud Y-axis
  //beak
  noStroke();
  fill(255, 195, 77);
  triangle(penguinX-10, penguinY-35, penguinX-22, penguinY-23, penguinX-10, penguinY-15);
  //foot
  fill(253, 86, 2);
  rect(penguinX, penguinY+22, penguinX-15, penguinY+25);
  fill(0);
  //body
  ellipse(penguinX, penguinY, 40, 50);
  //head
  ellipse(penguinX, penguinY-25, 33, 33);
  fill(255);
  //chest
  ellipse(penguinX-10, penguinY, 19, 33);
  //eye
  ellipse(penguinX+6, penguinY-25, 8, 8); 
  //wing
  fill(60);
  ellipse(penguinX+8, penguinY-3, 10, 20);
}

void fish() {
  //fish 
  noStroke();
  fill(250, 128, 114);
  //body
  ellipse(fishX, fishY, 15, 11);
  triangle(fishX-2, fishY, fishX-12, fishY-5, fishX-12, fishY+5);
  //eye
  fill(0);
  ellipse(fishX+2, fishY-2, 4, 4);
  //mouth
  stroke(0);
  line(fishX+3, fishY+1, fishX+6, fishY+1);


  // if the fish is caught reset it and speed it up to a max of 5
  if ((((fishX>=penguinX-20)&&(fishX<=penguinX+20)&&(fishY>=penguinY-25)&&(fishY<=penguinY+25)))) {
    fishY=-5;
    fishX=random(0, 400);
    if (fishSpeed < 5) {
      fishSpeed = fishSpeed + 1;
    }
  }

  //if the fish isnt caught reset it and slow down to min of 1
  if (fishY >= 334) {
    fishY=-5;
    fishX= random(0, 400);

    if (fishSpeed > 1) {
      fishSpeed = fishSpeed-1;
    }
  }
}