How to make the timer start after a condition?

final int stateSampleGame = 0;
final int stateBoss = 1;
final int stateVictory = 2;

int state = stateSampleGame;

void setup () {
  size (960, 640);
  background(255);
}
 void draw () {
  background(255);
  switch (state) {
  case stateSampleGame:        
    gameScreen();
    break;
  case stateBoss:        
    bossScreen();
    break;
  case stateVictory:  
    victoryScreen();
    break;
  default:    // error
    println("Error number 939; unknown state : "
      + state 
      + ".");
    exit();
    break;
  } // switch
}

void keyPressed () {
  switch (state) {
  case stateSampleGame:        
    keyPressedForGameScreen();
    break;
  case stateBoss:        
    keyPressedForBossScreen();
    break;
  case stateVictory:  
    keyPressedForVictoryScreen();
    break;
  default:    // error
    println("Error number 939; unknown state : "
      + state 
      + ".");
    exit();
    break;
  } // switch
}

void mousePressed () {
  switch (state) {
  case stateSampleGame:        
    mousePressedForGameScreen();
    break;
  case stateBoss:        
    mousePressedForBossScreen();
    break;
  case stateVictory:  
    mousePressedForVictoryScreen();
    break;

  default:    // error
    println("Error number 939; unknown state : "
      + state 
      + ".");
    exit();
    break;
  } // switch
}

int score;

void gameScreen() {
  fill(0);
  text("Click the screen and score 100 to win", 200, 280);
  text(score, 200, 300);
  if (score >= 100) {
    state = stateBoss;
  }
}
void keyPressedForGameScreen() {
}
void mousePressedForGameScreen() {
  if (mousePressed) {
    score = score + 5;
  }
}
int bossHp = 150;
int startTime;
int duration = 5000;
float xspeed = 5;
float yspeed= 3;
float x = 480; 
float y = -100;

void bossScreen() {
  text(bossHp, 600, 100);
  if (millis() > duration) {
    boss();
  }

  if (millis() < startTime+5*1000) //"timer"
   ///banner
    if (blink(20)) {
      fill(80, 80, 80, 100);
      rect(-1, height/3, width, height/4);
      fill(0);
      text("BOSS BATTLE", width/2, height/2);
    }
    ///banner
}

boolean blink(int span) {
  return frameCount%(span*2) < span;
}

void keyPressedForBossScreen() {
}
void mousePressedForBossScreen() {
  if (mousePressed) {
    bossHp = bossHp - 10;
    if (bossHp <=0) {
      state = stateVictory;
    }
  }
}

void boss() {
  ellipse(x, y, 100, 300);
  y=y+yspeed;
  if (y >= height/2-10) {
    yspeed =0;
    x = x + xspeed;
    if ((x > width - 105) || (x < 105)) {
      xspeed = xspeed * -1;
    }
  }
}
void victoryScreen() {
  text("You Win", width/2, height/2); 
}

void keyPressedForVictoryScreen() {
}
void mousePressedForVictoryScreen() {
}
Tagged:

Answers

  • edited April 2018

    The way I understand it, once you run your program, the timer automatically starts, however in this code, there should be some kind of

    `if(score>=100){
    //timer
    //banner
    }`
    

    though that doesn't work. Can someone help me figure it out?

  • edited April 2018 Answer ✓

    you just need to stet startTime in the moment the Boss mode starts (that's where it says state = stateBoss; ):

       startTime=millis(); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    

    I also corrected the if / else clause with the timer and made a new boolean bossGameStarted so that mouse clicks are ignored during the banner time. (This variable could be replaced with a new state bannerBeforeBoss or so.)

    here is the full sketch

    final int stateSampleGame = 0;
    final int stateBoss = 1;
    final int stateVictory = 2;
    
    int state = stateSampleGame;
    
    boolean bossGameStarted=false; 
    
    void setup () {
      size (960, 640);
      background(255);
    }
    void draw () {
      background(255);
      switch (state) {
      case stateSampleGame:        
        gameScreen();
        break;
      case stateBoss:        
        bossScreen();
        break;
      case stateVictory:  
        victoryScreen();
        break;
      default:    // error
        println("Error number 939; unknown state : "
          + state 
          + ".");
        exit();
        break;
      } // switch
    }
    
    // -----------------------------------------------------
    
    void keyPressed () {
      switch (state) {
      case stateSampleGame:        
        keyPressedForGameScreen();
        break;
      case stateBoss:        
        keyPressedForBossScreen();
        break;
      case stateVictory:  
        keyPressedForVictoryScreen();
        break;
      default:    // error
        println("Error number 939; unknown state : "
          + state 
          + ".");
        exit();
        break;
      } // switch
    }
    
    void mousePressed () {
      switch (state) {
      case stateSampleGame:        
        mousePressedForGameScreen();
        break;
      case stateBoss:        
        mousePressedForBossScreen();
        break;
      case stateVictory:  
        mousePressedForVictoryScreen();
        break;
    
      default:    // error
        println("Error number 939; unknown state : "
          + state 
          + ".");
        exit();
        break;
      } // switch
    }
    
    // -----------------------------------------------------
    // functions for game screen 
    
    int score;
    
    void gameScreen() {
      fill(0);
      text("Click the screen and score 100 to win", 200, 280);
      text(score, 200, 300);
      if (score >= 100) {
        state = stateBoss;
        startTime=millis(); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      }
    }
    
    void keyPressedForGameScreen() {
    }
    
    void mousePressedForGameScreen() {
      if (mousePressed) {
        score = score + 5;
      }
    }
    
    // -----------------------------------------------------
    // functions for BOSS screen 
    
    int bossHp = 150;
    int startTime;
    int duration = 5000;
    float xspeed = 5;
    float yspeed= 3;
    float x = 480; 
    float y = -100;
    
    void bossScreen() {
    
      if (millis() < startTime+5*1000) { //"timer"
        ///banner
        if (blink(20)) {
          fill(80, 80, 80, 100);
          rect(-1, height/3, width, height/4);
          fill(0);
          text("BOSS BATTLE", width/2, height/2);
          ///banner
        }//
      }//if
      //-------
      else 
      {
        bossGameStarted=true; 
        text(bossHp, 600, 100);
        boss();
      }//else
    }//func
    
    void keyPressedForBossScreen() {
      //
    }
    
    void mousePressedForBossScreen() {
      if (bossGameStarted) {
        bossHp = bossHp - 10;
        if (bossHp <=0) {
          state = stateVictory;
        }
      }
    }
    
    void boss() {
      ellipse(x, y, 100, 300);
      y=y+yspeed;
      if (y >= height/2-10) {
        yspeed =0;
        x = x + xspeed;
        if ((x > width - 105) || (x < 105)) {
          xspeed = xspeed * -1;
        }
      }
    }
    
    boolean blink(int span) {
      return frameCount%(span*2) < span;
    }
    
    // -----------------------------------------------------
    // functions for Victory screen 
    
    void victoryScreen() {
      fill(255, 0, 0);
      textSize(55); 
      textAlign(CENTER); 
      text("You Win", width/2, height/2);
    }
    
    void keyPressedForVictoryScreen() {
      //state=stateSampleGame;
    }
    
    void mousePressedForVictoryScreen() {
      //state=stateSampleGame;
    }
    //
    
  • edited April 2018

    @Chrisir Hi Chrisir, thanks for giving an answer to the code but it caused another problem, I actually have a

            If (key=='p'){
            state=statePause
            }
    

    in both the KeyPressedForGme and KeyPressedForBoss screen. I thought the timer problem would not affect it. The problem right now is when you pause in the boss screen, the game will be paused. However after resuming the game, the banner "animation" will play first. Thanks again.

  • Line 114 use bossGameStarted

    here in an appropriate way for example

  • Answer ✓

    like here:

    lots of other changes like variable previousState

    final int stateSampleGame = 0;
    final int stateBoss       = 1;
    final int stateVictory    = 2;
    final int statePause      = 3; 
    int state = stateSampleGame;
    int previousState = stateSampleGame;
    
    // false: Banner is allowed to appear: 
    boolean bossGameStarted=false; 
    
    void setup () {
      size (960, 640);
      background(255);
    }
    
    void draw () {
    
      background(255);
    
      switch (state) {
    
      case stateSampleGame:        
        gameScreen();
        break;
    
      case stateBoss:        
        bossScreen();
        break;
    
      case stateVictory:  
        victoryScreen();
        break;
    
      case statePause:
        pauseScreen();
        break; 
    
      default:    // error
        println("Error number 939a (in function draw()); unknown state : "
          + state 
          + ".");
        exit();
        break;
        //
      } // switch
      //
    }//func 
    
    // -----------------------------------------------------
    // Inputs 
    
    void keyPressed() {
      switch (state) {
    
      case stateSampleGame:        
        keyPressedForGameScreen();
        break;
    
      case stateBoss:        
        keyPressedForBossScreen();
        break;
    
      case stateVictory:  
        keyPressedForVictoryScreen();
        break;
    
      case statePause:
        keyPressedForPauseScreen();
        break; 
    
      default:    // error
        println("Error number 939b (keyPressed); unknown state : "
          + state 
          + ".");
        exit();
        break;
      } // switch
    }
    
    void mousePressed() {
      switch (state) {
    
      case stateSampleGame:        
        mousePressedForGameScreen();
        break;
    
      case stateBoss:        
        mousePressedForBossScreen();
        break;
    
      case stateVictory:  
        mousePressedForVictoryScreen();
        break;
    
      case statePause:
        mousePressedForPauseScreen();
        break; 
    
      default:    // error
        println("Error number 939c (mousePressed); unknown state : "
          + state 
          + ".");
        exit();
        break;
      } // switch
    }
    
    // -----------------------------------------------------
    // functions for game screen 
    
    int score;
    
    void gameScreen() {
      fill(0);
      textSize(19); 
      textAlign(LEFT); 
      text("Click the screen and score 100 to win", 
        200, 280);
      text(score, 200, 300);
      if (score >= 100) {
        state = stateBoss;
        startTime=millis(); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      }
    }
    
    void keyPressedForGameScreen() {
      if (key=='p') {
        previousState=state;
        state=statePause;
      }
    }
    
    void mousePressedForGameScreen() {
      if (mousePressed) {
        score = score + 5;
      }
    }
    
    // -----------------------------------------------------
    // functions for BOSS screen 
    
    int bossHp = 150;
    int startTime; //"timer"
    int duration = 5000;
    float xspeed = 5;
    float yspeed= 3;
    float x = 480; 
    float y = -100;
    
    void bossScreen() {
    
      //"timer"
      if (millis() < startTime+5*1000 && !bossGameStarted) { 
        // banner
        if (blink(20)) {
          fill(80, 80, 80, 100);
          rect(-1, height/3, 
            width, height/4);
          fill(0);
          textSize(45); 
          textAlign(CENTER); 
          fill(255, 0, 0); 
          text("BOSS BATTLE", width/2, height/2);
        }//if
      }//if
      //-------
      else
      {
        bossGameStarted=true; 
        fill(255, 0, 0); //RED
        text(bossHp, 600, 100);
        boss();
      }//else
    }//func
    
    void keyPressedForBossScreen() {
      //
      if (key=='p') {
        previousState=state;
        state=statePause;
      }
    }
    
    void mousePressedForBossScreen() {
      if (bossGameStarted) {
        bossHp = bossHp - 10;
        if (bossHp <=0) {
          state = stateVictory;
        }
      }
    }
    
    void boss() {
      ellipse(x, y, 100, 300);
      y=y+yspeed;
      if (y >= height/2-10) {
        yspeed =0;
        x = x + xspeed;
        if ((x > width - 105) || (x < 105)) {
          xspeed = xspeed * -1;
        }
      }
    }
    
    boolean blink(int span) {
      return frameCount%(span*2) < span;
    }
    
    // -----------------------------------------------------
    // functions for Victory screen 
    
    void victoryScreen() {
      fill(255, 0, 0);
      textSize(55); 
      textAlign(CENTER); 
      text("You Win", width/2, height/2);
    }
    
    void keyPressedForVictoryScreen() {
      state=stateSampleGame;
    }
    
    void mousePressedForVictoryScreen() {
      // state=stateSampleGame;
    }
    //
    
    // -----------------------------------------------------
    // functions for pause screen 
    
    void pauseScreen() {
      fill(0, 0, 250);
      textSize(55); 
      textAlign(CENTER); 
      text(" P A U S E ", 
        width/2, height/2);
    }
    
    void keyPressedForPauseScreen() {
      state=previousState;
    }
    
    void mousePressedForPauseScreen() {
      // ignore
    }
    //
    
Sign In or Register to comment.