Timing Component for Game

edited December 2016 in Using Processing

I need a countdown timer for a game.. It should pause when the key dedicated for pause is pressed and it should continue when its unpaused. The timer should display on the screen. I have tried many ways but none of them work. Below is the code for my keypressed. It would be great if someone could help me with it.

Below is my keypressed code: final int k = keyCode;

    void keyPressed () {  
    if (k == 'P')
        if (looping) { 
          text("Game Paused",200,200);
          noLoop();

        }
        else {         
          loop();
        }

}
Tagged:

Answers

  • I am on a journey

    I look monday

    ;-)

  • edited June 2014
    // forum.processing.org/two/discussion/5650/
    // timing-component-for-game
    
    static final color BG = 0;
    static final int TEXT_SIZE = 0100, QUALITY = 4, FPS = 60;
    
    boolean isPaused;
    
    // ...
    
    void setup() {
      size(800, 600, JAVA2D);
      smooth(QUALITY);
      frameRate(FPS);
    
      textSize(TEXT_SIZE);
    
      // ...
    }
    
    void draw() {
      if (isPaused) {
        text("Game Paused", 200, 200);
        noLoop();
        return;
      }
    
      background(BG);
    
      // ...
    }
    
    void keyPressed() {
      final int k = keyCode;
    
      if (!(isPaused ^= k == 'P'))  loop();
    
      // ...
    }
    
  • For a countdown you mean in seconds ?

    And counting from where down to zero? From 50 seconds?

    What happens at zero?

    ;-)

  • this one is not really precise...

    int timed;
    int maxTime = 50;
    int currentTime;
    font f;
    boolean pause = false;
    
    void setup(){
        size(200,200);
        f = createFont("arial", 15);
        textFont(f, 15);    
        timed = millis(); 
        fill(255);   
        }
    
    
    void draw(){
        background(70);
       if(!pause){
       if(millis() - timed > 1000){
       currentTime--;
       timed = millis();
           }
       }else{
           text("paused", 20, 150);
           }
    
        text(nf(currentTime, 2), 20, 20);
        currentTime = currentTime > 0 ?  currentTime:maxTime;
    
    
       }
    
    void mousePressed(){
        pause = !pause;
        timed = millis();
    
        }
    
  • it needs to be a countdown timer from 60 seconds. when it reaches 0 the game ends

  • edited June 2014

    Thank you.

  • Function millis() isn't affected by noLoop(). Merely the auto callback to draw() is turned off.
    So, rather than base the timer on millis(), we can use the current value of frameCount:
    http://processing.org/reference/frameCount.html

    Every time draw() is invoked, frameCount is automatically increased by 1.
    By default, frameRate() is 60 FPS. Therefore, 1 second means 60 frameCount! :-B
    In order to have a countdown of 1 minute, we're gonna need an elapsed TIMING = FPS * 60! *-:)

    Take a look at how it works online: (*)
    http://studio.processingtogether.com/sp/pad/export/ro.9Djm2xo4wna7I/latest

    And here's the source now: o->

    /**
     * Paused Countdown (v2.0)
     * by GoToLoop (2014/Jun)
     *
     * forum.processing.org/two/discussion/5650/timing-component-for-game
     *
     * studio.processingtogether.com/sp/pad/export/ro.9Djm2xo4wna7I/latest
     */
    
    static final color FG = #D0A000, BG = 0;
    static final int TEXT_SIZE = 0100, QUALITY = 4;
    static final int FPS = 60, TIMING = 1*FPS*60 + 60;
    
    int timeup = TIMING;
    boolean isPaused, isGameOver;
    
    // ...
    
    void setup() {
      size(800, 600, JAVA2D);
      smooth(QUALITY);
      frameRate(FPS);
    
      textSize(TEXT_SIZE);
      textAlign(CENTER, CENTER);
    
      fill(FG);
    
      // ...
    }
    
    void draw() {
      if (isPaused | isGameOver) {
        text(isGameOver? "Game Over" : "Game Paused"
          , width>>1, height>>2);
        noLoop();
        return;
      }
    
      background(BG);
    
      final int countdown = (timeup-frameCount)/60 | 0;
      if (countdown == 0)  isGameOver = true;
      text("Countdown: " + countdown, width>>1, height>>1);
    
      // ...
    }
    
    void keyPressed() {
      final int k = keyCode;
    
      if (!(isPaused ^= k == 'P'))  loop();
    
      // ...
    }
    
Sign In or Register to comment.