How to make: if timer is finished end game

Hi, I’m tryna make an if statement consisting of my timer and how to end the game after my countdown of 60 seconds has finished.

i have a class called countdown:

class CountDown //start of CountDown class
{ 
  private int durationSeconds; 

  public CountDown(int duration) 
  { 
    this.durationSeconds = duration;
  }    
  public int getRemainingTime()  //return the seconds left on the timer or 0 
  { //millis() processing command, returns time in 1000ths sec since program started 
    return max(0, durationSeconds - millis()/1000) ;
  }
} //end of class

and in my main I have:

CountDown timer;  //declare instance variable of CountDown

void setup()
{
size(400,500);
timer = new CountDown(60);  //call CountDown constructor – 60 secs
}

void draw()
{
 fill(255, 255, 0);
  textSize(26);
  text("Score: " + score, 10, 50);

  text(timer.getRemainingTime(), 20, 20); //display seconds remaining top left
}

what I’m trying to do is, once my timer finishes I want the GameMode == FINISHED. but I don’t know how to say once timer is finished I tried
if timer.getremainingtime = 0 then do this… but sadly that doesn’t work as well.

Hi @TheGullible1

The class method getRemainingTime() returns the remaining time of the timer as an integer

return max(0, durationSeconds - millis()/1000) ;

If the timer gets to 0, it will always return 0 so an If condition can check if the timer hit 0 and finish the game.
As an example check the code below:

class CountDown //start of CountDown class
{ 
  private int durationSeconds; 

  public CountDown(int duration) 
  { 
    this.durationSeconds = duration;
  }    
  public int getRemainingTime()  //return the seconds left on the timer or 0 
  { //millis() processing command, returns time in 1000ths sec since program started 
    return max(0, durationSeconds - millis()/1000) ;
  }
} //end of class

CountDown timer;  //declare instance variable of CountDown

void setup()
{
size(400,500);
timer = new CountDown(10);  //call CountDown constructor – 60 secs
}
int score = 0;
void draw()
{
  background(240);
 fill(0);
  textSize(26);
  text("Score: " + score, 10, 50);

  text(timer.getRemainingTime(), 20, 20); //display seconds remaining top left
  if(timer.getRemainingTime() == 0){ //Check if the timer finished countdown
   text("FINISH", width/2, height/2); 
  }
}

Hope it helps! :slight_smile:
Best regards

1 Like

Thank you so much, it worked perfectly <3

1 Like