how to implement time to game

edited November 2015 in Library Questions

Hey guys, im working on a prototype for color mixing game, that in the end should be controlled by Arduino inputs. Right now there is a button to get a random color and then you have to match it before the time runs out. But I have a few Problems with the code:

  1. I want the timer to start counting down from "60" when the random color is given. However right now the count down begins before the number and color is even shown. If I click on "Get Color" again i want it to reset and start a new game.

  2. When I match the color given, if want it to say "Win" for like 2-3 seconds, and then reset, but I cant finde a way to "pause" it and start it again only based on time. I have tried with noLoop but without luck.

  3. Similar to question #2, I want it to say "you lost" for 2-3 sec when the timer has reached "60". Then I want it to reset for a new game.

Here is my Code:

///// Libaries ////// 
  import controlP5.*;
  ControlP5 cp5; 

//// VARIABLES  ////// 
  int ranR,ranG,ranB;   
  int RED, GREEN, BLUE; 

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   `; `
  int tol = 20;
  int level = 5;  
  float procBar = -90; 

  int t; 
  int interval = 60;
  String time; 
  PFont f;

  boolean random_; 
  boolean startGame = false; 


/////////// SETUP /////////////// 
void setup(){
  size(960,640);
  background(30);
  smooth();

  f = createFont("Avenir-Medium", 12);

/////// CP5 CONTROLS /////// 
  cp5 = new ControlP5(this);

    cp5.addSlider("RED")
     .setPosition(width/2-100,height/2+200)
     .setSize(220,25)
     .setRange(0,255)
     .setValue(RED) 
     .setColorActive(color(255,0,0))
     .setColorBackground(color(150,0,0))
     .setColorForeground(color(200,0,0))
     ;

   cp5.addSlider("GREEN")
     .setPosition(width/2-100,height/2+240)
     .setSize(220,25)
     .setRange(0,255)
     .setValue(RED) 
     .setColorActive(color(0,255,0))
     .setColorBackground(color(0,150,0))
     .setColorForeground(color(0,200,0))
     ;

   cp5.addSlider("BLUE")
     .setPosition(width/2-100,height/2+280)
     .setSize(220,25)
     .setRange(0,255)
     .setValue(RED) 
     .setColorActive(color(0,0,255))
     .setColorBackground(color(0,0,150))
     .setColorForeground(color(0,0,200))
     ;     
}


/////// LOOP START //////////////////////////////////// 
void draw(){
  background(30);
  textFont(f); 

   // start button 
   if(mouseY > height-50 && mouseY < height-15 && mouseX > 25 && mouseX < 125){
   fill(120);
   } else { fill(60);}
   noStroke();
   rect(25,height-50,100,35); 
   fill(200); 
   textSize(16); 
   text("Get Color!",35,height-27);


  if(startGame){
 if(random_){
       //// GETTING RANDOM COLOR /// 
       ranR = int(random(255));
       ranG = int(random(255));
       ranB = int(random(255));    
       random_ = false;       
 }

  t = interval - ((millis() - currentMillis )/1000);
  time = nf(t,2);  
  println("time:  " + time +"   t:  " + t +"  currentMillis:  " + currentMillis); 


//ARC WITH RANDOM COLOR
  noStroke();
  fill(ranR,ranG,ranB);
  arc(width/2,height/2-100,400,400, radians(-90),radians(90));

//ARC WITH CONTROLLED COLOR
  fill(RED,GREEN,BLUE);
  arc(width/2,height/2-100,400,400, radians(90),radians(270));


//CHECK IF COLORS ARE MATCHED
  if(RED > ranR - tol && RED < ranR + tol ){
   if( GREEN > ranG - tol && GREEN < ranG + tol){
     if(BLUE > ranB - tol && BLUE < ranB + tol){


      fill(245); 
      textSize(75);  

      text("WIN!",width/2-52,height/2); // show for 3 sec's

//adjusting level and tolerance for next game
        if(level < 10){  
      level++; 
      tol -= 4;
      }

//getting new color for next gamer

       ranR = int(random(255));
       ranG = int(random(255));
       ranB = int(random(255)); 

//reset time for new game
        t = interval; 
    }
   }
  } 


//CHECK IF YOU LOOSE
   if(t < 1){    
     if(level > 0){
     level--;
     tol += 4; 

     }
     text("you lost!",width/2-52,height/2); // show for 3 sec's

     while(ranR + ranG + ranB != 500){
       ranR = int(random(255));
       ranG = int(random(255));
       ranB = int(random(255)); 
    }
    t = interval;  

   } 


//Calculating time  
  t = interval - (millis()/1000);
  time = nf(t,2);

//Display time 
   fill(245);
   strokeWeight(5); 
   stroke(30);
   ellipse(width/2, height/2-100, 50,50); 
   fill(30);
   textSize(24);
   text(time,width/2-13,height/2-90);
   noStroke(); 

//showing level and tolerance   
   fill(245); 
   textSize(18);
   text("Level:  " + level, width/2-30, height/2+150);
   text("Tolerance:  " + tol,width/2-50,height/2+180); 

  }
}

//////// LOOP END /////////


void mouseClicked(){
  if(mouseY > height-50 && mouseY < height-15 && mouseX > 25 && mouseX < 125){
  if(!startGame){
    startGame = true;}
    else{startGame = false; 
    }

  if(random_){
    random_ = false;}
    if(!random_){
    random_ = true;}
  }
}

Answers

Sign In or Register to comment.