Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*

Help spongebob put his glasses on! 
When the timing is right, press space to gain a point and reach the next speed difficulty.
Try to beat your high score. 

By: Alice (Yi Yang) Peng 

*/


//setting up 
void setup()
{
  // setting up the canvas
  size(400, 400);
  ellipseMode(CENTER);
  frameRate(60);
  noStroke();
  
  //intro text for player
  print("press space when the glasses are just right! highscore is",highscore, "\n");
  
}


// global variables 
float gY =0; //used to keep track of the glasses location
int score = 0; // the score
float speed = 2; // the speed
int highscore = 0; // keeps the high score
float randy = random(-50,100); // grabs a random y location to place the glasses at 


// a function for drawing the background
void drawBackground(){
  // setting up the canvas
  background(254, 245, 108);
  showScore();
  ellipseMode(CENTER);
  noStroke();
  fill(186,189,22);
  
  // variables used for drawing the background pattern
  int countx = 5 ;
  int county = 5;
  int gridX = 40;
  int gridY = 40;
  
  // a loop to draw the background pattern using circles
  while(county>0){
    // nested loop to save time and create a board of circles
    while(countx>0){
   ellipse(gridX,gridY,40,40);
   gridX = gridX + 80;
   countx = countx -1; 
  }
  // resetting some variables
    gridX = 40;  
    gridY = gridY + 80;
    countx =5;
    county = county-1;
  }
}

// a function to draw the eyes
void eyes() {
  
  // setting up canvas
  ellipseMode(CENTER);
  stroke(0);
  strokeWeight(3);

  // using circles to draw eyes

  // eyelashes
  fill(0);
  
  // left eye
  line(120, randy+200, 120, randy+120);
  line(120, randy+200, 160, randy+130);
  line(120, randy+200, 80, randy+130); 
  
  //right eye
  line(280, randy+200, 280, randy+120);
  line(280, randy+200, 320, randy+130);
  line(280, randy+200, 240, randy+130);  

  // white of the eyes 
  fill(255);
  ellipse(120, randy+200, 120, 120);
  ellipse(280, randy+200, 120, 120);

  // blue
  fill(63, 198, 245);
  ellipse(130, randy+200, 50, 50);
  ellipse(270, randy+200, 50, 50);

  // black pupils
  fill(0);
  ellipse(130, randy+200, 35, 35);
  ellipse(270, randy+200, 35, 35);
}


// a function to draw the glasses
void drawGlasses() {
  
  // setting up canvas
  strokeWeight(10);
  rectMode(CORNERS);
  noFill();
  stroke(0);
  
  //frames
  rect(40, gY-140, 190, gY-270);
  rect(210,gY-270,360,gY-140);
  
  //wires 
  line(40,gY-200,0,gY-200);
  line(190,gY-200,210,gY-200);
  line(360,gY-200,400,gY-200);
  
  // using the speed variable to move the glasses down
  gY = gY + speed;
  
  // if the glasses go out of the screen, bring it back to the top
  if (gY >=670){
   gY = -150; 
  }
}

// function to keep track of the score 
void showScore(){
  // setting up canvas
  rectMode(CORNERS);
  int a =0;
  int x1 = 20;

  // creating the little blocks that represent score
  while(a<score){
   fill(0);
   rect(x1,20,x1+10,30);
   a = a + 1;
   x1 = x1 + 40;
  }
}

// function for if a key is pressed
void keyPressed(){
  // checking if it is space
 if(keyCode==32) {
   // checking if the glasses and eyes line up
   if(gY>= (randy+400-40) && gY<= (randy+400+30)){
    score = score+1; 
    speed= speed + 1.2;
    gY =0;
    randy = random(-50,100);
  // if they don't line up, clears score and prints high score
   }else{
    if(score>highscore){
     highscore=score;
     print("NICE! New highscore is",highscore,"\n");
    }else{
      // if there is no high score, prints "try again"
     print("try again \n"); 
    }
    // resetting some variables for start over
    score = 0;
    speed = 2;
   }
 }
}

// the draw function that "wraps" everything up!
void draw() {
  
  // required functions for game to run
  drawBackground();
  eyes();
  drawGlasses();
  showScore();
  
}

// end of code