Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*Use arrow keys to move paddle.  //<>//
Hit the ball into the orange carrot to gain points (you lose points if you hit the purple carrot). 
If you fail to hit the ball, the game resets.
Reach 31 points to win & click on heart to play again. */

//intialize variables//
float borderW, borderH;
float paddleX, paddleY, paddleW, paddleH, paddleSX, paddleSY;
float ballX, ballY, ballW, ballH, ballSX, ballSY;
float carrotX, carrotY, carrotW, carrotH, carrotNX, carrotNY, carrotNW, carrotNH;
float score;
int bubbleColor, heartPink, mainPink, darkPink, midPink, lightPink, orange, green, purple, negGreen, orangeOne, orangeTwo, orangeThree;
boolean padLeft, padRight, padUp, padDown;
boolean playAgain;

//set window size and variables//
void setup() {
  //set window size
  size(400, 400);

  //set all colors for game
  bubbleColor = color(250, 144, 172, 150);
  heartPink = color (255, 0, 0, 150);
  mainPink = color(247, 140, 165);
  darkPink = color(222, 150, 169);
  midPink = color(232, 176, 191);
  lightPink = color(240, 198, 208);
  orange = color(242, 177, 108);
  green = color(134, 206, 133);
  purple = color(174, 127, 193);
  negGreen = color(104, 178, 164);
  orangeOne = color(215, 149, 79);
  orangeTwo = color(229, 172, 111);
  orangeThree = color(215, 152, 85);

  //set dimensions for L&R borders
  borderW = 25;
  borderH = height;

  //set initial location, dimensions & speed for the paddle
  paddleX = width/2;
  paddleY = 80;
  paddleW = 96;
  paddleH = 10;
  paddleSX = 5;
  paddleSY = 2;

  //set initial location, dimensions & speed for the ball
  ballX = width/2;
  ballY = height/2;
  ballW = 25;
  ballH = 25;
  ballSX = 2; 
  ballSY = 5;

  //set dimensions for token box & randomize location
  carrotX = random(borderW+carrotW/2+10, width-borderW-carrotW/2-10);
  carrotY = random(height/4+carrotH/2+10, height-carrotH/2-10);
  carrotW = 30;
  carrotH = 30;

  //set dimensions for token box (bad carrot) & randomize location
  carrotNX = random(borderW+carrotW/2+10, width-borderW-carrotW/2-10);
  carrotNY = random(height/4+carrotH/2+10, height-carrotH/2-10);
  carrotNW = 30;
  carrotNH = 30;

  //set initial score for game
  score = 0;
}

//call all functions
void draw() {
  background(lightPink);
  println(score);

  carrot();
  negCarrot();
  borders();
  bunny();
  paddle(); 
  ball(); 
  ballReset();
  carrotScore();
  scoreMark();
  circlePrint();

  //create reset menu when player gets to 31 points
  if (playAgain == true) {
    resetMenu();
  }
}

//draw borders and background objects for game
void borders() {
  rectMode(CENTER);
  noStroke();

  //draw mid square
  fill(midPink);
  rect(width/2, 50, width, 100);

  //draw top square
  fill(darkPink);
  rect(width/2, 25, width, 50);

  //left border
  fill(255);
  strokeWeight(2);
  stroke(mainPink);
  rect(borderW/2, borderH/2, borderW, borderH);
  //right border
  rect(width-borderW/2, borderH/2, borderW, borderH);

  //create outline for entire window
  fill(0, 0, 0, 0);
  rect(width/2, height/2, width, height);
}


//draw bunny character for paddle
void bunny() {
  rectMode(CORNERS);
  fill(255);
  noStroke();

  //head and ears
  rect(paddleX-paddleW/4, paddleY-35, paddleX+paddleW/4, paddleY, 15);
  rect(paddleX-paddleW/6, paddleY-55, paddleX-paddleW/15, paddleY, 20);
  rect(paddleX+paddleW/6, paddleY-55, paddleX+paddleW/15, paddleY, 20);

  //eyes
  stroke(0);
  strokeWeight(5);
  point(paddleX - paddleW/8, paddleY-20);
  point(paddleX + paddleW/8, paddleY-20);

  //muzzle    
  stroke(120);
  strokeWeight(.5);
  line(paddleX-paddleW/30, paddleY-12, paddleX+paddleW/30, paddleY-15);
  line(paddleX+paddleW/30, paddleY-12, paddleX-paddleW/30, paddleY-15);

  //blush
  noStroke();
  fill(lightPink);
  ellipse(paddleX-paddleW/6, paddleY-13, 6, 4);
  ellipse(paddleX+paddleW/6, paddleY-13, 6, 4);
}


//draw paddle (includes movement and constraints)
void paddle() {
  rectMode(CENTER);
  fill(mainPink);
  noStroke();

  //draw paddle
  rect(paddleX, paddleY, paddleW, paddleH);

  //move paddle using keypad
  if (padLeft) {
    paddleX = paddleX - paddleSX;
  }
  if (padRight) {
    paddleX = paddleX + paddleSX;
  }
  if (padUp) {
    paddleY = paddleY - paddleSY;
  }
  if (padDown) {
    paddleY = paddleY + paddleSY;
  }

  //restrict paddle from moving outside of playing field
  if (paddleX - paddleW/2 - borderW < 0) {
    paddleX = paddleX + paddleSX;
  }
  if (paddleX + paddleW/2 + borderW > width) {
    paddleX = paddleX - paddleSX;
  }
  if (paddleY + paddleH/2 > 100) {
    paddleY = paddleY - paddleSY;
  }
  if (paddleY - paddleH/2 < 50) {
    paddleY = paddleY + paddleSY;
  }
}


//draw ball (includes movement + restrictions + reaction to contact with paddle)
void ball() {
  ellipseMode(CENTER);
  fill(255);
  noStroke();

  //ball
  ellipse(ballX, ballY, ballW, ballH);

  //create heart icon
  fill(mainPink);
  quad(ballX, ballY, ballX+5, ballY-8, ballX+10, ballY, ballX, ballY+11);
  quad(ballX, ballY, ballX-5, ballY-8, ballX-10, ballY, ballX, ballY+11);

  //create ball movement
  ballX = ballX + ballSX;
  ballY = ballY + ballSY;

  //create bounderies for ball on X and Y axis; ball will move in opposite direction if it makes contact
  if (ballX + ballW/2 > width-borderW || ballX - ballH/2 < borderW) {
    ballSX = ballSX*-1;
  }
  if (ballY + ballH/2 > height || ballY - ballH/2 < 0) {
    ballSY = ballSY*-1;
  }

  //ball will change directions if it touches the paddle
  if (ballY - ballH/2 < paddleY + paddleH/2 && ballX < paddleX + paddleW/2 && ballX > paddleX - paddleW/2) {
    if (ballSY < 0) {
      ballSY = -ballSY;
    }
  }
}


//create game reset if ball falls behind paddle (paddle resets, tokens randomize, score turns to zero)
void ballReset() {
  if (ballY-ballH < paddleY-paddleH-30) {
    //delay(1500);
    ballX = width/2;
    ballY = 3*height/4;
    paddleX = width/2;
    carrotX = random(borderW+carrotW/2+10, width-borderW-carrotW/2-10);
    carrotY = random(height/4+carrotH/2+10, height-carrotH/2-10);
    score = 0;
  }
}

//draw hit box and carrot token
void carrot() {
  rectMode(CENTER);
  fill(252, 227, 235);
  noStroke();

  //hit box
  rect(carrotX, carrotY, carrotW, carrotH);

  //draw carrot
  fill(green);
  triangle(carrotX+2, carrotY+3, carrotX+3, carrotY-15, carrotX+10, carrotY-10); 
  triangle(carrotX+14, carrotY, carrotX, carrotY, carrotX+14, carrotY-10); 
  fill(orange);
  triangle(carrotX-15, carrotY+15, carrotX+10, carrotY+5, carrotX-2, carrotY-10);
}


//draw the bad carrot token
void negCarrot() {
  if (score>25) {
    rectMode(CENTER);
    fill(0);

    //hit box
    rect(carrotNX, carrotNY, carrotNW, carrotNH);

    //draw bad carrot
    fill(negGreen);
    triangle(carrotNX-2, carrotNY+3, carrotNX-3, carrotNY-15, carrotNX-10, carrotNY-10); 
    triangle(carrotNX-14, carrotNY, carrotNX, carrotNY, carrotNX-14, carrotNY-10); 
    fill(purple);
    triangle(carrotNX+15, carrotNY+15, carrotNX-10, carrotNY+5, carrotNX+2, carrotNY-10);
  }
}


//create ability to gain and lose points depending on whether ball touches good or bad token
void carrotScore() {
  //touches good token, adds to score when touched
  if ((ballX > carrotX-carrotW/2 && ballX < carrotX+carrotW/2 && ballY > carrotY-carrotH/2 && ballY < carrotY + carrotH/2) 
    || (ballX + ballW/2> carrotX-carrotW/2 && ballX + ballW/2 < carrotX+carrotW/2 && ballY > carrotY-carrotH/2 && ballY < carrotY + carrotH/2)
    || (ballX - ballW/2> carrotX-carrotW/2 && ballX - ballW/2 < carrotX+carrotW/2 && ballY > carrotY-carrotH/2 && ballY < carrotY + carrotH/2)
    || (ballX > carrotX-carrotW/2 && ballX < carrotX+carrotW/2 && ballY+ballH/2 > carrotY-carrotH/2 && ballY+ballH/2 < carrotY + carrotH/2)
    || (ballX + ballW/2> carrotX-carrotW/2 && ballX + ballW/2 < carrotX+carrotW/2 && ballY-ballH/2 > carrotY-carrotH/2 && ballY-ballH/2 < carrotY + carrotH/2)) {
    score = score+1;
    carrotX = random(borderW+carrotW/2+10, width-borderW-carrotW/2-10);
    carrotY = random(height/4+carrotH/2+10, height-carrotH/2-10);
  }

  //bad token appears when over 25, subtracts from score when touched
  if (score > 25) {
    if ((ballX > carrotNX-carrotNW/2 && ballX < carrotNX+carrotNW/2 && ballY > carrotNY-carrotNH/2 && ballY < carrotNY + carrotNH/2) 
      || (ballX + ballW/2> carrotNX-carrotNW/2 && ballX + ballW/2 < carrotNX+carrotNW/2 && ballY > carrotNY-carrotNH/2 && ballY < carrotNY + carrotNH/2)
      || (ballX - ballW/2> carrotNX-carrotNW/2 && ballX - ballW/2 < carrotNX+carrotNW/2 && ballY > carrotNY-carrotNH/2 && ballY < carrotNY + carrotNH/2)
      || (ballX > carrotNX-carrotNW/2 && ballX < carrotNX+carrotNW/2 && ballY+ballH/2 > carrotNY-carrotNH/2 && ballY+ballH/2 < carrotNY + carrotNH/2)
      || (ballX + ballW/2> carrotNX-carrotNW/2 && ballX + ballW/2 < carrotNX+carrotNW/2 && ballY-ballH/2 > carrotNY-carrotNH/2 && ballY-ballH/2 < carrotNY + carrotNH/2)) {
      score = score - 1;
      carrotNX = random(borderW+carrotW/2+10, width-borderW-carrotW/2-10);
      carrotNY = random(height/4+carrotH/2+10, height-carrotH/2-10);
    }
  }

  //create true and false statement for reset menu, dependant on player's score
  if (score>30) {
    playAgain = true;
    ballSX = 0;
    ballSY = 0;
  }
  if (score<30) {
    playAgain = false;
  }
}


//create leftward marks for score; each point = one circle
void scoreMark() {
  fill(bubbleColor);
  int circle = 25;
  while ((circle < 25+25*score) && (circle < 400)) {
    ellipse(borderW/2, circle, 25, 25);
    circle = circle + 25;
  }

  //create rightward marks for score; each point = one circle
  if (score > 15) {
    fill(bubbleColor);
    int circleR = 25;
    while (circleR < 25 + (score-15)*25) {
      ellipse(width-borderW/2, circleR, 25, 25);
      circleR = circleR + 25;
    }
  }
}

//draw print on borders using loop
void circlePrint() {
  strokeWeight(1);
  stroke(247, 140, 165);
  fill(0, 0, 0, 0);

  int circle = 0;
  while (circle<height+50) {
    ellipse(borderW/2, circle, 25, 25);
    ellipse(width-borderW/2, circle, 25, 25);
    circle = circle + 25;
  }
}


void resetMenu() {
  background(lightPink);
  noStroke();
  //draw carrots
  //greenery
  fill(green);
  quad(0, 250, 0, 300, 70, 300, 70, 250);
  triangle(0, 300, 70, 300, 50, 320);
  quad(10, 220, 10, 250, 90, 250, 90, 220);
  triangle(10, 220, 60, 220, 40, 210);
  quad(60, 220, 80, 210, 120, 250, 80, 270);
  quad(70, 240, 90, 280, 80, 290, 70, 280);

  quad(380, 290, 380, 300, 390, 300, 390, 290);
  quad(390, 290, 390, 300, 400, 320, 400, 260);

  quad(330, 140, 330, 180, 380, 140, 360, 120);
  quad(330, 180, 380, 140, 400, 140, 400, 260);
  triangle(370, 220, 370, 250, 400, 260);
  quad(340, 220, 360, 210, 370, 220, 350, 230);
  //back
  fill(orangeOne);
  triangle(100, 350, 310, 210, 360, 280);
  quad(310, 210, 340, 215, 360, 240, 360, 280);
  //middle
  fill(orangeTwo);
  triangle(60, 320, 365, 270, 360, 340);
  quad(365, 270, 360, 340, 390, 320, 380, 280);
  //left
  fill(orangeThree);
  triangle(65, 335, 125, 250, 320, 355);
  quad(65, 335, 125, 250, 100, 270, 75, 295);

  //draw bunny
  fill(255);
  quad(240, 260, 240, 360, 270, 330, 260, 260);
  quad(200, 240, 180, 360, 130, 340, 140, 280);
  triangle(200, 240, 140, 280, 170, 240);
  quad(200, 240, 240, 240, 240, 360, 180, 360);
  triangle(240, 250, 240, 260, 260, 260);
  quad(190, 240, 260, 260, 280, 250, 300, 220);
  quad(190, 240, 300, 220, 160, 190, 160, 220); 
  quad(160, 190, 300, 220, 300, 170, 200, 160);
  triangle(200, 160, 300, 170, 240, 150);
  quad(210, 120, 230, 90, 240, 110, 230, 160);
  quad(230, 90, 210, 120, 180, 130, 140, 120);
  //right ear
  quad(310, 90, 300, 170, 290, 180, 280, 120);
  fill(245);
  //left foot
  quad(120, 270, 120, 360, 100, 310, 100, 290);
  quad(120, 270, 120, 360, 130, 360, 140, 300);
  //right foot
  quad(180, 295, 180, 380, 160, 320, 170, 290);
  quad(180, 295, 180, 380, 200, 370, 190, 300);
  //left paw
  triangle(160, 250, 160, 280, 190, 260);
  quad(160, 250, 160, 280, 140, 280, 140, 260);
  //right paw 
  quad(240, 280, 240, 320, 205, 310, 200, 290);
  quad(240, 280, 240, 320, 260, 320, 260, 310);
  //right ear  
  quad(280, 100, 260, 150, 290, 140, 310, 90);
  triangle(280, 100, 260, 150, 250, 130);
  //eyes

  fill(0);
  ellipse(200, 190, 10, 10);
  ellipse(260, 210, 10, 10);
  //mouth
  stroke(0);  
  strokeWeight(.5);
  line(225, 212, 215, 218);
  line(215, 210, 225, 220);

  //draw bubble and point
  fill(255);
  ellipseMode(CENTER);
  noStroke();
  ellipse(100, 130, 80, 100);
  quad(110, 170, 150, 190, 140, 180, 130, 160);

  //draw heart - if mouse hovers over bubble, heart turns red
  if (mouseX < 140 && mouseX > 60 && mouseY<180 && mouseY>80) {
    fill(heartPink);
  } else fill(mainPink);
  triangle(100, 125, 130, 135, 100, 165);
  quad(100, 125, 130, 135, 130, 105, 105, 110);
  triangle(100, 125, 100, 165, 70, 135);
  quad(100, 125, 70, 135, 70, 105, 95, 110);
}

//initialize controls for the paddle
void keyPressed() {
  if (keyCode == LEFT) {
    padLeft = true;
  }
  if (keyCode == RIGHT) {
    padRight = true;
  }
  if (keyCode == UP) {
    padUp = true;
  }
  if (keyCode == DOWN) {
    padDown = true;
  }
}

//initialize controls for the paddle
void keyReleased() {
  if (keyCode == LEFT) {
    padLeft = false;
  }
  if (keyCode == RIGHT) {
    padRight = false;
  }
  if (keyCode == UP) {
    padUp = false;
  }
  if (keyCode == DOWN) {
    padDown = false;
  }
}

//reset game by clicking on bubble after reaching reset menu
void mouseClicked() {
  if ((mouseX < 140 && mouseX > 60 && mouseY<180 && mouseY>80) && (score>30)) {
    ballX = width/2;
    ballY = 3*height/4;
    paddleX = width/2;
    carrotX = random(borderW+carrotW/2+10, width-borderW-carrotW/2-10);
    carrotY = random(height/4+carrotH/2+10, height-carrotH/2-10);
    score = 0;
    ballSX = 2;
    ballSY = 5;
  }
}