Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Bunny
/* Kimi Yip
Move you move left and right and the bunny will follow it.
Click the left mouse button to make the bunny jump - you can only make it jump when it is on the ground.
Direct the bunny to land on the stars.
When bunny lands on a star, the star will disappear and reappear in a different location.
Keep bouncing off stars to increase your score.
If you fall back to the ground, your score will reset back to 0. 
 */
 
//Declare number of stars in the background
int numberOfBackgroundStars= 150;
BackgroundStars[] backgroundstars=new BackgroundStars[numberOfBackgroundStars];

//Make new objects
Snow snow;
Bunny bunny;
Stars stars;
Stars stars1;
Stars stars2;
Stars stars3;
Stars stars4;
Stars stars5;
Score score;

//Sets screeen size & frame rate
void setup() {
  size(800, 600);
  frameRate(50);

  //Makes new objects  
  createBackgroundStars();
  snow=new Snow();
  bunny = new Bunny(400, 300);
  stars= new Stars(random(10, 750), 150);
  stars1= new Stars(random(10, 750), 300);
  stars2= new Stars(random(10, 750), 450);
  stars3= new Stars(random(10, 750), 100);
  stars4= new Stars(random(10, 750), 200);
  stars5= new Stars(random(10, 750), 400);
  score=new Score();
}

// Pulls and draws objects & functions from all tabs
void draw() {

  background(30, 50, 90);

  displayBackgroundStars();
  snow.drawMounds(5, 20, 1);
  bunny.moveTowards(mouseX);
  bunny.display();
  stars.display();
  stars1.display();
  stars2.display();
  stars3.display();
  stars4.display();
  stars5.display();
  score.display();
     
}

//When mouse is clicked, bunny will jump
void mousePressed() {
  bunny.jump();
}

//Creates background Stars
void createBackgroundStars() {
  for (int i=0; i<backgroundstars.length; i++) {
    backgroundstars[i]=new BackgroundStars (random(width), random(height));
  }
}

//Displays background Stars
void displayBackgroundStars() {
  for (int i=0; i<backgroundstars.length; i++) {
    backgroundstars[i].display();
  }
}
// Background stars class 
class BackgroundStars {

  //Declare variables   
  float size;
  PVector bgStarPosition=new PVector();

  //Background Stars constructor & declare variable values
  BackgroundStars(float x, float y) {
    bgStarPosition.x=x;
    bgStarPosition.y=y;
    size=random(0.1, 3);
  }

  //Draw background stars locations & size of stars 
  void display() {
    fill(random(75, 255));
    ellipse(bgStarPosition.x, bgStarPosition.y, size, size);
  }
}
//Bunny character class
class Bunny {

  //Declare bunny variables 
  float orientation;
  float moveSpeed;
  float verticalSpeed;
  float jumpPower;
  float gravity;
  float floor;
  PVector bunnyPosition;
  PVector bunnyMain;

  //Bunny constructor - declare variable values  
  Bunny(float x, float y) {
    bunnyPosition = new PVector(x, y);
    moveSpeed = 10;
    jumpPower = -20;
    gravity = 1;
    orientation = 1;
    floor = 500;
  }

  //Draw bunny's body, and set functions for bunny's movements, speed, gravity and jump  
  void display() {
    updateHeight();
    fill(255);
    noStroke();

    //Draw Bunny    
    //Body
    setbunnyMain(0, 0);
    ellipse(bunnyMain.x, bunnyMain.y, 40, 35);

    //Head
    setbunnyMain(20, -10);
    ellipse(bunnyMain.x, bunnyMain.y, 28, 28);


    //left Ear
    setbunnyMain(14, -30);
    ellipse(bunnyMain.x, bunnyMain.y, 10, 20);

    //Right Ear
    setbunnyMain(26, -30);
    ellipse(bunnyMain.x, bunnyMain.y, 10, 20);

    // Tail
    setbunnyMain(-20, -5);
    ellipse(bunnyMain.x, bunnyMain.y, 10, 10);

    //Leg
    stroke(255);
    strokeWeight(10);
    setbunnyMain(-8, 16);
    line(bunnyMain.x, bunnyMain.y, bunnyMain.x + 7 * orientation, bunnyMain.y);

    //Arm
    setbunnyMain(10, 10);
    line(bunnyMain.x, bunnyMain.y, bunnyMain.x + 2  * orientation, bunnyMain.y + 6);

    //Left Eye
    noStroke();
    fill(180, 210, 250);
    setbunnyMain(14, -12);
    ellipse(bunnyMain.x, bunnyMain.y, 7, 7);

    //Right Eye
    setbunnyMain(26, -12);
    ellipse(bunnyMain.x, bunnyMain.y, 7, 7);

    //Nose
    fill(180, 130, 145);
    setbunnyMain(20, -7);
    triangle(bunnyMain.x - 2.5 * orientation, bunnyMain.y, bunnyMain.x + 2.5 * orientation, bunnyMain.y, bunnyMain.x, bunnyMain.y + 2);
  }

  //Bunny follow MouseY's position
  void moveTowards(float x) {
    if (x == bunnyPosition.x) {
      return;
    }

    // Bunny cannot go faster than speed    
    float distance = abs(x - bunnyPosition.x);
    if (distance > moveSpeed) {
      distance = moveSpeed;
    }

    //Bunny going right and left    
    if (bunnyPosition.x < x) {
      bunnyPosition.x += distance;
      orientation = 1;
    } else {
      bunnyPosition.x -= distance;
      orientation = -1;
    }
  }

  //Update vertical position of bunny
  void updateHeight() {
    verticalSpeed += gravity;
    bunnyPosition.y += verticalSpeed;

    //Constrain bunny to floor
    if (bunnyPosition.y >= floor) {
      bunnyPosition.y = floor;
      verticalSpeed = 0;
    }
  }

  //Bunny jumps and restricts it so that it cannot jump again in the air  
  void jump() {
    if (bunnyPosition.y < floor) {
      return;
    }
    verticalSpeed = jumpPower;
  }

  //Bunny will jump again in air after landing on a star  
  void airjump() {
    verticalSpeed = jumpPower;
  }

  //Settings to flip's bunny's position so it is facing the direction of the mouse 
  void setbunnyMain(float x, float y) {
    //bunnyMain = bunnyPosition.copy();
    bunnyMain = new PVector(bunnyPosition.x, bunnyPosition.y);
    bunnyMain.x += x * orientation;
    bunnyMain.y += y;
  }
}
//Score Class 
class Score {

  //Score variables 
  int scoreNumber;

  //Score constructor & variable values
  Score() {
    scoreNumber = 0;
  } 

  //Draw score and fuction to reset score back to 0 if bunny lands on ground
  void display() {
    textSize(15);
    fill(255);
    text("Score:", 10, 20);
    text(scoreNumber, 60, 20);

    if (bunny.bunnyPosition.y == bunny.floor) {
      scoreNumber = 0 ;
    }
  }
}
//Snowy ground class
class Snow {

  //Declare snow variables  
  PVector snowPosition=new PVector();
  float snowMounds;

  //Snow constructor   
  Snow() {
  }

  //Draw snowmounds, position, bump's frequencies and roundness of bumps  
  void drawMounds(int bumpRoundness, int bumpFrequency, int lineWidth) 
  {
    fill(255);
    stroke(255);

    snowPosition.x = 0;
    while (snowPosition.x < width) {
      snowMounds = (sin(snowPosition.x/bumpFrequency))*bumpRoundness-100;
      rect(snowPosition.x, height + 22, lineWidth, snowMounds);
      snowPosition.x += lineWidth;
    }
  }
}
//Main stars class
class Stars {

  //Declare star variables  
  PVector starPosition;
  color starColor;
  float circleSize;
  boolean isCollided = false;
  boolean newStar = false;

  //Main Star constructor, declared values for variables  & main star's x & y position
  Stars(float x, float y) {
    starPosition = new PVector(x, y);
    circleSize=7;
    changeColor();
  }

  //Draw star, declare starcolor setting, declare collision of bunny & star conditions
  void display() {
    fill(starColor);

    //Draw star's shape  
    quad(starPosition.x, starPosition.y +2, starPosition.x - 10, starPosition.y - 9, starPosition.x, starPosition.y - 20, starPosition.x + 10, starPosition.y - 9);
    quad(starPosition.x, starPosition.y, starPosition.x - 10, starPosition.y - 10, starPosition.x - 20, starPosition.y - 7, starPosition.x - 10, starPosition.y + 10);
    quad(starPosition.x + 1, starPosition.y, starPosition.x - 13, starPosition.y, starPosition.x - 13, starPosition.y + 18, starPosition.x + 1, starPosition.y + 13);
    quad(starPosition.x - 1, starPosition.y, starPosition.x - 1, starPosition.y + 13, starPosition.x + 13, starPosition.y + 18, starPosition.x + 13, starPosition.y);
    quad(starPosition.x, starPosition.y, starPosition.x + 10, starPosition.y + 10, starPosition.x + 20, starPosition.y - 7, starPosition.x + 10, starPosition.y - 10);
    
  
    //Declare if bunny lands on star's position (collision), bunny will jump in the air again  
    if (dist(bunny.bunnyPosition.x, bunny.bunnyPosition.y, starPosition.x, starPosition.y)< 20 && !isCollided ) {
      isCollided=true;
      bunny.airjump();
    }

    //Declare if collision is true and bunny lands on star, the star will fade away by decreasing alpha 
    if (isCollided && alpha(starColor) > 0) {
      isCollided=true;
      starColor = color(red(starColor), green(starColor), blue(starColor), alpha(starColor) -10);

      //Declare if a star's alpha reaches 0, then respawn a new star on the map with the same y  but still random x
      //Random respawned star color and add one to score when a star's alpha reached 0
    }
    if (alpha(starColor) <= 0) {
      isCollided = false;
      starColor =  color(random(200, 255), random(200, 255), random(200, 255));
      starPosition.x= random(10, 750);
      score.scoreNumber =  score.scoreNumber += 1 ;
    }

    //Set color settings to randomize star's colors
  }
  void changeColor() {
    starColor = color(random(200, 255), random(200, 255), random(200, 255));
  }
}