Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Author: Riley Jackson
//Date: Sept. 28, 2018
//Discription: 


//All the user defined variables used in the code
float carrotPositionX;
float carrotPositionY;
float bunnyPositionX = 200;
float bunnyPositionY = 200;
float bunnyUpdatePositionX;
float bunnyUpdatePositionY;
float snowPositionX;
float snowPositionY;
int carrotSpacingX = 40;

//this sets up the window of the interactive toy and prints out a line to explain how the program functions 
void setup() {
  size(400, 400);
  println("Use the arrow keys to move the rabbit around the screen and press shift to restart");
}

//this draws all the seperate parts of the program and updates the parts that move
void draw() {
  background(10, 20, 50);

  //calls the updateSnow() function
  updateSnow();

  fill(255);
  rect(0, 200, 400, 200);

  //calls the undateBunny() function
  updateBunny();

  //calls the updateCarrot() function
  updateCarrot();

  //calls the drawSnow function
  drawSnow();

  //calls the drawBunny() function
  drawBunny();
}

//this draws the carrot and put is at the correct height position on the screen
void drawCarrot() {
  noStroke();
  ellipseMode(CENTER);

  fill(55, 160, 28);
  triangle(carrotPositionX -5, (height/2)-10, carrotPositionX, (height/2)-15, carrotPositionX+5, (height/2)-5);
  triangle(carrotPositionX+10, (height/2)-15, carrotPositionX+15, (height/2)-10, carrotPositionX+5, (height/2)-5);

  fill(244, 173, 66);
  triangle(carrotPositionX, (height/2) - 5, carrotPositionX+10, (height/2)-5, carrotPositionX+5, (height/2)+15);

  fill(255);
  ellipse(carrotPositionX+5, (height/2)+10, 15, 15);
}

//this creates the loop that allows the carrots to appear across the screen with out having to redraw them each time
void updateCarrot() {

  //sets the current x position of the carrot to 0
  carrotPositionX = 0;

  //starts the loop and sets the perameter for the loop to contine.
  while (carrotPositionX <= width) {
    //calls the funtion drawCarrot(); to draw the carrot
    drawCarrot();
    //this adds to the carrots x position each time to create a line with them evenly spaced out
    carrotPositionX = carrotPositionX + carrotSpacingX;
  }
}

//this funtion updates the snow by randomly putting it across the sky.
void updateSnow() {
  //sets these two variables to random x and y corrdinates between the specified numbers
  snowPositionX = random(0, 400);
  snowPositionY = random(0, 200);
}

//this function draws a single piece of snow to be repeated in the updateSnow() function
void drawSnow() {
  ellipse(snowPositionX, snowPositionY, 10, 10);
}

//draws the bunny once with variable that can be changes later for convienece
void drawBunny() {
  noStroke();
  ellipseMode(CENTER);

  fill(237, 216, 168);
  ellipse(bunnyPositionX, bunnyPositionY + 25, 50, 50);
  ellipse(bunnyPositionX, bunnyPositionY, 40, 40);

  arc(bunnyPositionX + -10, bunnyPositionY + -5, 10, 50, -PI, 0);
  arc(bunnyPositionX + 10, bunnyPositionY+ -5, 10, 50, -PI, 0);

  fill(0);
  ellipse(bunnyPositionX + -10, bunnyPositionY+ -5, 7, 7);
  ellipse(bunnyPositionX + 10, bunnyPositionY+ -5, 7, 7);

  fill(255);
  ellipse(bunnyPositionX + -12, bunnyPositionY+ -7, 2, 2);
  ellipse(bunnyPositionX + 8, bunnyPositionY+ -7, 2, 2);

  fill(0);
  triangle(bunnyPositionX + -5, bunnyPositionY, bunnyPositionX, bunnyPositionY+5, bunnyPositionX+5, bunnyPositionY);
}

//this updates the bunny's movement and constrains it to the screen.
void updateBunny() {

  //constrains the x position of the bunny
  bunnyPositionX = constrain(bunnyPositionX, 0, 400);
  //constrains the y position of the bunny
  bunnyPositionY = constrain(bunnyPositionY, 200, 400);

  //calls the keyPressed() fuction
  keyPressed();
}

//this funtion is used when one of the specifed keys are pressed and then will move the bunny around the screen
void keyPressed() {
  if (key ==CODED) {
    
    //if the up key is pressed the bunny's y position will decrease
    if (keyCode == UP) {

      bunnyPositionY--;
    }
    //if the down key is pressed the bunny's y position will increase
    if (keyCode == DOWN) {

      bunnyPositionY++;
    }
    //if the left key is pressed the bunny's x position will decrease
    if (keyCode == LEFT) {

      bunnyPositionX--;
    }
    //if the right keys is pressed the bunny's x position will increase
    if (keyCode == RIGHT) {

      bunnyPositionX++;
    }
    //if the shift key is pressed the bunny will move directly to point 200,200 on the screen
    else if(keyCode == SHIFT){
      
      bunnyPositionX = 200;
      bunnyPositionY= 200;
      
    }
  }
}