Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*Horse Free Jumping By: Marseilaise Perkins 
Assignment 2: Interactive Toy
          This is a horse free jump competition. The horse must jump over the hurdles. To play press 'w' to make the horse jump. 
          using the mouse will make the clouds move - this is to show how fast the horse is going
*/




// VARIABLES \\
//sets the starting position for the hurdles
float hurdleX;
float hurdleY;

//sets the starting position for the horse
float horseY = 0;
float horseAcc = 0;

float railingX;
float railingY;

float cloud2X = random(0, 400);
float cloud2Y = random(0, 250);


//sets when the horse will jump. false = going up, true = going down
boolean jumpStage = false; 
boolean jump = false;



//sets up the canvas
void setup() {

  size (400, 400);
  frameRate(60); 
  noStroke();
  rectMode(CENTER);
 
//calls the hurdles into the setup so it continues to show up at the end of the window
hurdleX = 500;
hurdleY = 290;

}



//sets up the animation and objects that will continously be in frame
void draw()
{
  background(0);
//sets the ground, area, and track to show continously
  area();
 
//keeps the hurdles looping   
  if (hurdleX<-800) {
  hurdleX=200;
  hurdleY=400;
}      

//keeps the cloud looping   
  if (cloud2X<-300) {
  cloud2X=300;
  cloud2Y=150;
}   


//this calls clouds2 into the frame
clouds2();

//calls the lower railing into the frame
looprailing();

//calls the upper railing into the frame
looprailing2();  

//the standing (or running position of the horse) in this position when the player does not press 'w'.  
  if (jump == false)
  {
//this is the function that is called when the player is not making the horse jump
    horseCanter();
//this means that when the horse jumps it will change.
  } else
  {
//this is the funciton called when 'w' is pressed. 
    horseLeap();
}
 
//calls the Hurdle function to continously show in the canvas. This does not make the hurdles loop.
hurdle(); 

//this is the second hurdle
hurdle1();

//this function is the gravity of the horse jumping up vertically.
gravity();

//this function is called to bring the clouds into the frame.
clouds();
}



//this function creates the railing to loop.
void looprailing() {
  int railingX= 1;
  while (railingX<=width) {
fill(117, 117, 117);
rect(200, 340, 400, 2);    
rect(railingX , 345, 2, 10);
rect(railingX, 345, 2, 10);
rect(railingX, 345, 2, 10); 
railingX+=35;
}
  
}

void looprailing2() {
  int railingX= 1;
  while (railingX<=width) {
fill(117, 117, 117);
rect(200, 290, 400, 2);    
rect(railingX , 295, 2, 10);
rect(railingX, 295, 2, 10);
rect(railingX, 295, 2, 10); 
railingX+=35;
}

}

//this is the function for creating the sky, ground, and track
void area()
{
  fill(170, 251, 255);
  rect(200, 200, 400, 400);

  fill(211, 197, 143);
  rect(200, 350, 400, 100);

  fill(86, 155, 47);
  rect(200, 400, 400, 100);
}

//this is the function for creating the first short hurdle, also the speed for both hurdles. 
void hurdle() {
  fill(211, 31, 51);
  quad(180 + hurdleX, 288, 180 + hurdleX, 290, 200 + hurdleX, 300, 200 + hurdleX, 298);
  fill(255);
  rect(180 + hurdleX, 292, 4, 20);
  rect(200 + hurdleX, 296, 4, 22);
  //speed hurdles move at
  hurdleX = hurdleX + (-4);

//this is the creation and coordinates of the second small hurdle.  
   fill(211, 31, 51);
  quad(380 + hurdleX, 288, 380 + hurdleX, 290, 400 + hurdleX, 300, 400 + hurdleX, 298);
  fill(255);
  rect(380 + hurdleX, 292, 4, 20);
  rect(400 + hurdleX, 296, 4, 22);

//this is the creation and coordinates of the third small hurdle.  
     fill(211, 31, 51);
  quad(780 + hurdleX, 288, 780 + hurdleX, 290, 800 + hurdleX, 300, 800 + hurdleX, 298);
  fill(255);
  rect(780 + hurdleX, 292, 4, 20);
  rect(800 + hurdleX, 296, 4, 22);


}
      
//this is the creation and coordinates of the second large hurdle.     
void hurdle1() {
  fill(211, 31, 51);
  quad(200 + hurdleX, 275, 200 + hurdleX, 277, 221 + hurdleX, 289, 221 + hurdleX, 287);
  quad(200 + hurdleX, 288, 200 + hurdleX, 290, 220 + hurdleX, 302, 220 + hurdleX, 300);
  fill(255);
  rect(200 + hurdleX, 285, 4, 35);
  rect(220 + hurdleX, 294, 4, 40);  

//this is the creation of the secondthird large hurdle  
    fill(211, 31, 51);
  quad(570 + hurdleX, 275, 570 + hurdleX, 277, 591 + hurdleX, 289, 591 + hurdleX, 287);
  quad(570 + hurdleX, 288, 570 + hurdleX, 290, 590 + hurdleX, 302, 590 + hurdleX, 300);
  fill(255);
  rect(570 + hurdleX, 285, 4, 35);
  rect(590 + hurdleX, 294, 4, 40);
  
//this is the creation of the third large hurdle  
    fill(211, 31, 51);
  quad(800 + hurdleX, 275, 800 + hurdleX, 277, 821 + hurdleX, 289, 821 + hurdleX, 287);
  quad(800 + hurdleX, 288, 800 + hurdleX, 290, 820 + hurdleX, 302, 820 + hurdleX, 300);
  fill(255);
  rect(800 + hurdleX, 285, 4, 35);
  rect(820 + hurdleX, 294, 4, 40); 
  
}

//function to create the clouds
void clouds() {
  //cloud1 follows mouse movement
  fill (233, 245, 244);
  ellipse(360+(mouseX - 360)*.9, 40,40, 35);  
  ellipse(350+(mouseX - 360)*.9, 30, 30, 30);
  ellipse(360+(mouseX - 360)*.9, 50, 30, 30);
  ellipse(380+(mouseX - 360)*.9, 50, 40, 30);
  ellipse(360+(mouseX - 360)*.9, 60, 20, 20);
  ellipse(350+(mouseX - 360)*.9, 50, 30, 30);
  ellipse(335+(mouseX - 360)*.9, 40, 30, 20);
  ellipse(340+(mouseX - 360)*.9, 50, 30, 20);
  
  //cloud2 follows mouse movement
  fill(221, 245, 244);
  ellipse(280+(mouseX - 280)*.5, 100, 20, 20);
  ellipse(280+(mouseX - 280)*.5, 115, 30, 30);
  ellipse(280+(mouseX - 280)*.5, 125, 25, 25);
  ellipse(270+(mouseX - 280)*.5, 115, 40, 40);
  ellipse(265+(mouseX - 280)*.5, 105, 20, 20);
  ellipse(260+(mouseX - 280)*.5, 115, 40, 25);
  ellipse(260+(mouseX - 280)*.5, 125, 45, 20);
  ellipse(290+(mouseX - 280)*.5, 110, 30, 30);
  ellipse(290+(mouseX - 280)*.5, 120, 50, 35);
}  

void clouds2() {
    fill(202, 238, 244);
  ellipse(cloud2X, cloud2Y, 20, 20);
  ellipse(cloud2X-15, cloud2Y-5, 30, 30);
  ellipse(cloud2X, cloud2Y+10, 25, 25);
  ellipse(cloud2X-20, cloud2Y+20, 40, 40);
  ellipse(cloud2X-30, cloud2Y-10, 20, 20);
  ellipse(cloud2X-30, cloud2Y, 40, 25);
  ellipse(cloud2X, cloud2Y, 45, 20);
  ellipse(cloud2X-40, cloud2Y, 40, 30);
  ellipse(cloud2X-35, cloud2Y+15, 50, 35);
 
 cloud2X = cloud2X + random(-2); 
}

//this was the first function that helped to create horseleap
void horseTrot() {
  //tail
  fill(56, 36, 9);
  triangle(26, 273, 18, 268, 11, 272);
  fill(78, 52, 17);
  triangle(26, 273, 16, 270, 9, 276);
  fill(107, 70, 18);
  triangle(26, 273, 14, 275, 10, 280);

  //FRONT left leg
  fill(215, 178, 128);
  quad(50, 285, 45, 285, 47, 293, 50, 292);
  fill(145, 116, 76);
  quad(50, 292, 47, 293, 50, 298, 52, 297);
  quad(52, 297, 50, 298, 50, 300, 54, 300); 
  fill(56, 45, 29);
  quad(54, 300, 50, 300, 51, 302, 55, 302);

  //BACK left leg
  fill(221, 182, 129);
  quad(33, 284, 29, 280, 22, 288, 25, 290);
  fill(156, 124, 80);
  quad(22, 288, 20, 289, 21, 291, 25, 290);
  fill(145, 116, 76);
  quad(24, 290, 21, 291, 21, 294, 24, 296);
  quad(24, 296, 21, 294, 19, 296, 20, 298);
  fill(56, 45, 29);
  triangle(20, 298, 19, 296, 16, 297);

  //hair
  fill(56, 36, 9);
  quad(52, 266, 47, 261, 40, 270, 44, 276);

  //neck, chest, body, bum
  fill(202, 166, 117);
  quad(55, 271, 52, 267, 43, 275, 51, 281);
  ellipse(46, 281, 12, 12);
  quad(43, 275, 28, 273, 29, 283, 45, 287);
  ellipse(28, 279, 13, 13);

  //ear right
  fill(136, 109, 73);
  triangle(50, 266, 52, 265, 50, 262);

  //head and muzzle
  fill(221, 186, 136);
  ellipse(53, 269, 8, 8);
  quad(57, 266, 54, 273, 58, 273, 60, 270);
  quad(60, 270, 58, 273, 59, 274, 62, 271);

  //ear left
  fill(136, 109, 73);
  triangle(52, 265, 55, 265, 55, 262);

  //eye
  fill(0);
  ellipse(54, 269, 1.5, 1.5);

  //FRONT right leg
  fill(234, 194, 139);
  ellipse(45, 283, 5, 5);
  quad(45, 284, 43, 287, 48, 289, 50, 288);
  fill(200, 164, 115);
  quad(49, 287, 48, 288, 48, 291, 50, 291);
  quad(50, 291, 48, 291, 45, 293, 46, 295);
  quad(46, 295, 45, 293, 41, 293, 41, 295);
  fill(56, 45, 29);
  triangle(41, 295, 41, 293, 38, 295);

  //BACK right leg
  fill(234, 194, 139);
  ellipse(24, 283, 9, 10);
  quad(27, 288, 24, 288, 24, 291, 26, 294);
  fill(200, 164, 115);
  quad(26, 294, 24, 291, 22, 296, 25, 297);
  fill(180, 147, 102);
  quad(25, 297, 22, 296, 24, 299, 26, 299);
  fill(56, 45, 29);
  quad(26, 299, 24, 299, 25, 302, 29, 302);
}

//created the function horseCanter and its coordinates.
void horseCanter() {

  //tail
  fill(56, 36, 9);
  triangle(23, 278, 14, 276, 9, 280);
  fill(78, 52, 17);
  triangle(23, 278, 12, 279, 8, 284);
  fill(107, 70, 18);
  triangle(23, 278, 15, 280, 10, 288);

  //BACK left leg
  fill(215, 178, 128);
  quad(25, 284, 23, 281, 16, 286, 18, 288);
  fill(145, 116, 76);
  quad(18, 288, 16, 286, 14, 287, 15, 290);
  quad(15, 290, 14, 287, 10, 294, 12, 296);
  fill(56, 45, 29);
  quad(12, 296, 10, 294, 9, 296, 13, 297);

  //FRONT left leg
  fill(200, 164, 115);
  quad(49, 283, 48, 288, 55, 290, 57, 287);
  quad(60, 293, 57, 287, 55, 290, 56, 294);
  fill(180, 147, 102);
  quad(60, 293, 57, 294, 57, 296, 60, 297);
  fill(56, 45, 29);
  quad(60, 297, 57, 296, 56, 298, 59, 299);

  //hair
  fill(56, 36, 9);
  quad(52, 266, 45, 264, 38, 273, 44, 276);

  //neck, chest, body, bum
  fill(202, 166, 117);
  quad(55, 271, 52, 267, 43, 275, 51, 281);
  ellipse(46, 281, 12, 12);
  quad(43, 275, 28, 273, 29, 283, 45, 287);
  ellipse(28, 279, 13, 13);

  //ear right
  fill(136, 109, 73);
  triangle(50, 266, 52, 265, 50, 262);

  //head and muzzle
  fill(221, 186, 136);
  ellipse(53, 269, 8, 8);
  quad(57, 266, 54, 273, 58, 273, 60, 270);
  quad(60, 270, 58, 273, 59, 274, 62, 271);

  //ear left
  fill(136, 109, 73);
  triangle(52, 265, 55, 265, 55, 262);

  //eye
  fill(0);
  ellipse(54, 269, 1.5, 1.5);

  //FRONT right leg
  fill(234, 194, 139);
  ellipse(47, 285, 5, 5);
  quad(50, 285, 45, 283, 45, 293, 48, 293);
  fill(200, 164, 115);
  quad(48, 293, 45, 293, 45, 300, 47, 300);
  fill(56, 45, 29);
  quad(47, 300, 45, 300, 45, 302, 48, 302);

  //BACK right leg

  fill(221, 182, 129);
  quad(30, 285, 25, 285, 26, 288, 29, 290 );
  fill(156, 124, 80);
  quad(29, 289, 26, 288, 23, 291, 27, 292);
  fill(145, 116, 76); 
  quad(27, 292, 23, 291, 25, 295, 27, 294); 
  quad(27, 294, 25, 295, 26, 299, 30, 299);
  fill(56, 45, 29);
  quad(30, 299, 26, 299, 26, 301, 32, 301);
  fill(234, 194, 139);
  ellipse(27, 282, 9, 10);
}

//created the gravity function 
void gravity()
{
    
 if (jump == true)
 {
  if (jumpStage == false) {
//this is how fast the horse accelerates up
    horseAcc -= 0.5;
    if (horseAcc <= 0)
    {
     jumpStage = true;
    }
     horseY -= horseAcc;
    } else
    {
//how fast the horse comes down      
     horseAcc += 0.5;
      if (horseAcc >= 9)
    {
    jumpStage = false;
        jump = false;
        horseY = 0;
    }
    horseY += horseAcc;
    }
  }
}

//creation of the horseLeap function and its coordinates
void horseLeap() {
  //tail
  fill(56, 36, 9);
  triangle(26, 273 + horseY, 18, 268 + horseY, 11, 272 + horseY);
  fill(78, 52, 17);
  triangle(26, 273 + horseY, 16, 270 + horseY, 9, 276 + horseY);
  fill(107, 70, 18);
  triangle(26, 273 + horseY, 14, 275 + horseY, 10, 280 + horseY);

  //FRONT left leg
  fill(215, 178, 128);
  quad(50, 285 + horseY, 45, 285 + horseY, 47, 293 + horseY, 50, 292 + horseY);
  fill(145, 116, 76);
  quad(50, 292 + horseY, 47, 293 + horseY, 50, 298 + horseY, 52, 297 + horseY);
  quad(52, 297 + horseY, 50, 298 + horseY, 50, 300 + horseY, 54, 300 + horseY); 
  fill(56, 45, 29);
  quad(54, 300 + horseY, 50, 300 + horseY, 51, 302 + horseY, 55, 302 + horseY);

  //BACK left leg
  fill(221, 182, 129);
  quad(33, 284 + horseY, 29, 280 + horseY, 22, 288 + horseY, 25, 290 + horseY);
  fill(156, 124, 80);
  quad(22, 288 + horseY, 20, 289 + horseY, 21, 291 + horseY, 25, 290 + horseY);
  fill(145, 116, 76);
  quad(24, 290 + horseY, 21, 291 + horseY, 21, 294 + horseY, 24, 296 + horseY);
  quad(24, 296 + horseY, 21, 294 + horseY, 19, 296 + horseY, 20, 298 + horseY);
  fill(56, 45, 29);
  triangle(20, 298 + horseY, 19, 296 + horseY, 16, 297 + horseY);

  //hair
  fill(56, 36, 9);
  quad(52, 266 + horseY, 47, 261 + horseY, 40, 270 + horseY, 44, 276 + horseY);

  //neck, chest, body, bum
  fill(202, 166, 117);
  quad(55, 271 + horseY, 52, 267 + horseY, 43, 275 + horseY, 51, 281 + horseY);
  ellipse(46, 281 + horseY, 12, 12);
  quad(43, 275 + horseY, 28, 273 + horseY, 29, 283 + horseY, 45, 287 + horseY);
  ellipse(28, 279 + horseY, 13, 13);

  //ear right
  fill(136, 109, 73);
  triangle(50, 266 + horseY, 52, 265 + horseY, 50, 262 + horseY);

  //head and muzzle
  fill(221, 186, 136);
  ellipse(53, 269 + horseY, 8, 8);
  quad(57, 266 + horseY, 54, 273 + horseY, 58, 273 + horseY, 60, 270 + horseY);
  quad(60, 270 + horseY, 58, 273 + horseY, 59, 274 + horseY, 62, 271 + horseY);

  //ear left
  fill(136, 109, 73);
  triangle(52, 265 + horseY, 55, 265 + horseY, 55, 262 + horseY);

  //eye
  fill(0);
  ellipse(54, 269 + horseY, 1.5, 1.5);

  //FRONT right leg
  fill(234, 194, 139);
  ellipse(45, 283 + horseY, 5, 5);
  quad(45, 284 + horseY, 43, 287 + horseY, 48, 289 + horseY, 50, 288 + horseY);
  fill(200, 164, 115);
  quad(49, 287 + horseY, 48, 288 + horseY, 48, 291 + horseY, 50, 291 + horseY);
  quad(50, 291 + horseY, 48, 291 + horseY, 45, 293 + horseY, 46, 295 + horseY);
  quad(46, 295 + horseY, 45, 293 + horseY, 41, 293 + horseY, 41, 295 + horseY);
  fill(56, 45, 29);
  triangle(41, 295 + horseY, 41, 293 + horseY, 38, 295 + horseY);

  //BACK right leg
  fill(234, 194, 139);
  ellipse(24, 283 + horseY, 9, 10);
  quad(27, 288 + horseY, 24, 288 + horseY, 24, 291 + horseY, 26, 294 + horseY);
  fill(200, 164, 115);
  quad(26, 294 + horseY, 24, 291 + horseY, 22, 296 + horseY, 25, 297 + horseY);
  fill(180, 147, 102);
  quad(25, 297 + horseY, 22, 296 + horseY, 24, 299 + horseY, 26, 299 + horseY);
  fill(56, 45, 29);
  quad(26, 299 + horseY, 24, 299 + horseY, 25, 302 + horseY, 29, 302 + horseY);
  
}

//sets up what happens when a key is pressed specifically key 'w'
void keyPressed()
{
 if (jump == false)
 {
  if (key == 'w' || key == 'W')
  {
   horseAcc = 8.5;
   jump = true;
  }
 }
}