Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
////////////////////////////////////////////////////
/*
ARROW MATCHER. Fun little game thing where you have to match as many arrows as possible while
avoiding mistakes.

All comments refer to the line below

*/
////////////////////////////////////////////////////

//Variables

//The last arrow the player pressed, i.e their choice
int choice;
//Current Direction. The current direction of the arrow in the middle. Compared to choice
int curDir;
//Score and high score
int score = 0;
int hScore = 0;
//Since the while loop is practically useless in Processing, rather than actually wait for player input
//  I just told the code to skip over choosing the next arrow until the player picks. "chose" holds that info.
boolean chose = false;
//Similar purpose as chose, but just used once to set up the first arrow so players can start going.
boolean firstArrow = true;
//Keeps track of player's lives
int lives = 3;
//"started" prevents most of the program from running while false
boolean started = false;
//Same as started, really
boolean failed = false;
//Checks what the time was when the clock is about to be reset
float lastTime = 0;
//The amount of time that has passed
float time;
//Used in loop
int x = 0;


void setup(){
  
    size(400, 400);
    print("DEBUG CONSOLE: NOTHING HERE IS IMPORTANT GUYS DONT WORRY");
}

void draw(){
  
    background(150, 150, 255);
    noStroke();
    
    //Don't do any of this until they've pressed start
    if (started){
      //Don't do any of this if they're dead
      if (lives > 0){
    
        //Only choose the next arrow if it's needed
        if (firstArrow || chose){
    
          nextArrow();
          firstArrow = false;
          chose = false;
    
        }
        //Start the timer. Subtracts time to artificially reset it
        time = millis() - lastTime;
      
        //Player loses when 15 seconds have passed
        if (time > 15000){
      
          failed = true;
          score = 0;
      
        }
        
    
      } else {
        
         failed = true;
         score = 0;
        
      }
    //If they haven't pressed start, check if they have
    } else if (!started){
      
       if (mousePressed){
         
          if (150 < mouseX && mouseX < 250 && 150 < mouseY && mouseY < 250){
            
            started = true;
            lastTime = millis();
            
          }
         
       }
      
    }
    
    //Do this if they died
    if (failed){
      
        if (mousePressed){
          
            if (150 < mouseX && mouseX < 250 && 150 < mouseY && mouseY < 250){
            
            failed = false;
            lives = 3;
            lastTime = millis();
            
          }
          
        }
      
    }
    
    
    //Draw Elements. Draws all the GUI stuff
    drawElem();
    
  
}

//Keeps track of score, and updates it when applicable. Also keeps track of lives.
void updateScore(){
  
  if (chose){
      
      if (choice == curDir){
        
        score++;
        print(score);
        
        if (score > hScore){
          
           hScore = score; 
          
        }
        
      } else {
        
        lives--;
        print("WRONG");
        
      }
      
    }
  
}

void drawElem(){
   
  //Four quadrants
   fill(140, 184, 255);
   triangle(0, 0, 200, 200, 400, 0);
   fill(140, 255, 170);
   triangle(400, 0, 400, 400, 200, 200);
   fill(247, 255, 140);
   triangle(400, 400, 0, 400, 200, 200);
   fill(255, 140, 228);
   triangle(0, 0, 200, 200, 0, 400);
   
   //Loop to generate the flowing circles
   for (int y = 0; y <= 400; y += 40){
    
     
     if (x <= 400){
       
        fill(255, 188, 188, 100);
        ellipse(x, y, 40, 40);
        x += 40;
    
     } else {
       
         x = 0;
       
     }
  
   }
   
   
   //All the arrows around the circle
   fill(130, 130, 130);
   rect(195, 20, 10, 100, 50);
   triangle(200, 10, 230, 40, 170, 40);
   
   rect(280, 195, 100, 10, 50);
   triangle(390, 200, 360, 230, 360, 170);
   
   rect(195, 280, 10, 100, 50);
   triangle(200, 390, 170, 360, 230, 360);
   
   rect(20, 195, 100, 10, 50);
   triangle(10, 200, 40, 170, 40, 230);
   
   //The circle(s) in the middle
   fill(130, 130, 130);
   ellipse(200, 200, 110, 110);
   fill(219, 219, 219);
   ellipse(200, 200, 100, 100);
  
  //Makes sure the game's started before drawing the middle arrow
  if (started){
  
  //Middle Arrow
  if (curDir == 1){
     //UP
       fill(130, 130, 130);
       rect(195, 175, 10, 50, 50);
       triangle(200, 165, 220, 185, 180, 185);
     
   } else if (curDir == 2){
     //RIGHT
       fill(130, 130, 130);
       rect(175, 195, 50, 10, 50);
       triangle(235, 200, 215, 220, 215, 180);
     
   } else if (curDir == 3){
     //DOWN
       fill(130, 130, 130);
       rect(195, 175, 10, 50, 50);
       triangle(200, 235, 180, 215, 220, 215);
     
   } else if (curDir == 4){
     //LEFT
       fill(130, 130, 130);
       rect(175, 195, 50, 10, 50);
       triangle(165, 200, 185, 180, 185, 220);
     
   }
   
   //Timer bar at the bottom
   fill(255, 0, 0);
   rect(0, 380, (400 - (time / 38)), 20);
  
  //If they HAVEN'T started, draw the start "button"
  } else if (!started){
    
      fill(76, 76, 76);
      textSize(16);
      text("START", 175, 205);
    
  }
  
  //If they've failed, draw the failed "button"
  if (failed){
    
      fill(219, 219, 219);
      ellipse(200, 200, 100, 100); 
    
      fill(76, 76, 76);
      textSize(16);
      text("TRY AGAIN", 160, 205);
    
  }
  
  //Draw score
  fill(76, 76, 76);
  textSize(16);
  text("Score: " + score, 10, 35);
  text("High Score: " + hScore, 10, 15);
  
  text("Lives: " + lives, 330, 15);
  
  
  
}

//Randomly pick the next arrow. Don't really need a method for this but whatever
void nextArrow(){
  
   curDir = (int)random(1, 5);
   
   print(curDir);
   
  
}

//Recognizes keystrokes. Key released was used instead of pressed due to repeating inputs when only one is wanted
void keyReleased(){

    if (keyCode == UP){
        
        choice = 1;
        chose = true;
        fill(0, 97, 255);
        rect(195, 20, 10, 100, 50);
        triangle(200, 10, 230, 40, 170, 40);
        //Checks to make sure the game is still going, then updates score
        if (lives > 0 && started && !failed){
          
          updateScore();
          
        }
        
      } else if (keyCode == RIGHT){
        
        choice = 2;
        chose = true;
        fill(0, 255, 67);
        rect(280, 195, 100, 10, 50);
        triangle(390, 200, 360, 230, 360, 170);
        if (lives > 0 && started && !failed){
          
          updateScore();
          
        }
        
      } else if (keyCode == DOWN){
        
        choice = 3;
        chose = true;
        fill(238, 255, 0);
        rect(195, 280, 10, 100, 50);
        triangle(200, 390, 170, 360, 230, 360);
        if (lives > 0 && started && !failed){
          
          updateScore();
          
        }
        
      } else if (keyCode == LEFT){
        
        choice = 4;
        chose = true;
        fill(255, 0, 195);
        rect(20, 195, 100, 10, 50);
        triangle(10, 200, 40, 170, 40, 230);
        if (lives > 0 && started && !failed){
          
          updateScore();
          
        }
        
        
      } else {
        
        //Just to be sure
        chose = false;
        
      }
  
}