How do I make the timer stop and reset?

2

Answers

  • edited April 2018

    It’s very hard to hit a pixel exactly

    Better work with

    if(mouseX>34 && 
       mouseX<45 &&
       mouseY>..... &&
       mouseY< ......) { 
    

    Also you need this to be in draw() not in mousePressed() for example so that it’s displaying throughout and not only flashes briefly

  • Not familiar with your code, but your conditional above is restrictive in nature. Your mouse pointer have to be very precisely and carefully placed at the position x,y => 38,14. Instead, you should use the dist() function to detect when the pointer is close to the position:

    final int MIN_DISTA = 10;  //10 px radius
    final PVector eggLocation = new PVector(38,14);
    
    if (dist(mouseX,mouseY,eggLocation.x,eggLocation.y) < MIN_DISTA) {...}
    

    You can adjust the min dista. If making the above change doesn't solve the problem, then it is very likely you have a bug in your code.

    Kf

  • Chrisir, I want it to be hard for the player to get the easter egg. But even if I get rid of the if statement and leave just these lines of code:

    fill(#AAAAAA);
        textSize(56);
        text("You found our secret!", 150, 150);
    

    it still won't work. What's going on?

    /*
     Press 'q' to move up.
     Press 's' to move down.
     Press 'u' to move left.
     Press 'i' to move right.
     Press 'v' to restart.
     Get to the end as fast as possible.
     If you go on to the red areas, you  will fail.
     */
    
    // The variable state 
    // the constants the variable state can have as values 
    final int NORMAL = 0; // unique numbers 
    final int FAIL   = 1;
    final int WON    = 2;
    int state = NORMAL; 
    
    int count_at_reset;
    float x = 10;
    float y = 380;
    
    int difference, sec;
    
    void setup() {
      size(400, 400);
      frameRate(60);
    }
    
    
    void draw() {
    
      if (mouseX == 38 && mouseY == 14) {
        fill(#AAAAAA);
        textSize(56);
        text("You found our secret!", 150, 150);
      }
    
      switch(state) {
    
      case NORMAL:
        drawForNormal(); 
        break; 
    
      case FAIL:
        drawForFail(); 
        break; 
    
      case WON:
        drawForWon(); 
        break;
    
      default:
        println("Error");
        break;
      } // switch 
      //
    } // func 
    
    // ----------------------------------------
    // functions called by draw() 
    
    void drawForNormal() {
    
      background(255, 0, 0);
      fill(0, 255, 255);
      noStroke(); 
      rect(0, 200, 30, 201);
      rect(30, 200, 320, 30);
      rect(80, 200, 20, -100);
      rect(100, 100, 30, 20);
      rect(130, 100, 20, 50);
      rect(150, 130, 20, 20);
      rect(170, 150, 20, -100);
      rect(170, 50, -50, 20);
      rect(120, 70, -20, -70);
    
      checkBoundaries(); 
    
      difference = millis() - count_at_reset;
      sec = difference /1000;
      fill(255);
      textSize(26);
      text(sec, 300, 30);
    
      // PLAYER
      stroke(0); 
      fill(0, 255, 0);
      rect(x, y, 10, 10);
    
      if (y < 1) {
        state=WON;
      }
    
      inputs();
    
      // --------------------------------
    
    
    
      if (x < 0 ) { 
        x = 0;
      }
    
      if (y > height-10 ) { 
        y = height-10;
      }
    }
    
    void drawForFail() {
      fill(255); 
      textSize(140); 
      text("FAIL", 60, 370);
      textSize(22);
      text("Press v to replay", 210, 70);
      inputs();
    }
    
    void drawForWon() {
      fill(255);
      textSize(30);
      text("YOU WON\n"
        +sec
        +"\nPress v to reset.", 50, 50);
    } 
    
    // -----------------------------------------------
    // Tools
    
    void inputs() {
    
      if (!keyPressed) {
        return;
      }
    
      if (key == 'v'||key == 'V') {
        reset();
        x = 10;
        y = 380;
      }
    
      if (key == 'q'||key == 'Q') {
        y = y - 1;
      }
    
      if (key == 's'||key == 'S') {
        y = y + 1;
      }
    
      if (key == 'u'||key == 'U') {
        x = x - 1;
      }
    
      if (key == 'i'||key == 'I') {
        x = x + 1;
      }
    
      if (key==' ') {
        /// if (state==FAIL) 
        state=NORMAL;
        x = 10;
        y = 380;
      }
      //
    
    
      if (key == 'v') {
        if (state==FAIL) {
          state=NORMAL; 
          x = 10;
          y = 380;
          reset();
        }
      }
    }//
    
    void keyPressed() {
      if (key == 'v') {
        if (state==WON) {
          state=NORMAL; 
          x = 10;
          y = 380;
          reset();
        }
      }
    }
    
    
    void checkBoundaries() {
      if (x > 20 && y > 221) {
        state = FAIL;
      }
    
      if (x > 340 && y < 222 && y > 192) {
        state = FAIL;
      }
    
      if (x > 180 && y < 200) {
        state = FAIL;
      }
    
      if (x > 90 && x < 181 && y < 200 && y > 140) {
        state = FAIL;
      }
    
      if (x > 90 && x < 130 && y < 141 && y > 110) {
        state = FAIL;
      }
    
      if (x > 110 && x < 181 && y < 50 && y > 0) {
        state = FAIL;
      }
    
      if (x > -1 && x < 80 && y < 200 && y > -1) {
        state = FAIL;
      }
    
      if (x > 79 && x < 100 && y < 100 && y > -1) {
        state = FAIL;
      }
    
      if (x > 99 && x < 170 && y < 100 && y > 60) {
        state = FAIL;
      }
    
      if (x > 140 && x < 170 && y > 99 && y < 130) {
        state = FAIL;
      }
    }
    
    void reset() {
      count_at_reset = millis();
    } 
    
  • Maybe it’s white text on white ground?

    Change color or position

    Or move these lines to the very end of draw()

  • Sorry, still not working. Also, it's not white.

  • ??

    White was an example

    You still have == instead of .....>....<....>.....<......

    Post your entire code

  • The lines must be in one of the functions and at the end

    Nothing in draw is allowed outside switch

  • It gets overwritten by background otherwise

  • edited April 2018

    Nothing in draw is allowed outside switch

    Sorry, I don't understand what that means.

    /*
     Press 'q' to move up.
     Press 's' to move down.
     Press 'u' to move left.
     Press 'i' to move right.
     Press 'v' to restart.
     Get to the end as fast as possible.
     If you go on to the red areas, you  will fail.
     */
    
    // The variable state 
    // the constants the variable state can have as values 
    final int NORMAL = 0; // unique numbers 
    final int FAIL   = 1;
    final int WON    = 2;
    int state = NORMAL; 
    
    int count_at_reset;
    float x = 10;
    float y = 380;
    
    int difference, sec;
    
    void setup() {
      size(400, 400);
      frameRate(60);
    }
    
    
    void draw() {
    
      switch(state) {
    
      case NORMAL:
        drawForNormal(); 
        break; 
    
      case FAIL:
        drawForFail(); 
        break; 
    
      case WON:
        drawForWon(); 
        break;
    
      default:
        println("Error");
        break;
      } // switch 
      //
    } // func 
    
    // ----------------------------------------
    // functions called by draw() 
    
    void drawForNormal() {
    
      background(255, 0, 0);
      fill(0, 255, 255);
      noStroke(); 
      rect(0, 200, 30, 201);
      rect(30, 200, 320, 30);
      rect(80, 200, 20, -100);
      rect(100, 100, 30, 20);
      rect(130, 100, 20, 50);
      rect(150, 130, 20, 20);
      rect(170, 150, 20, -100);
      rect(170, 50, -50, 20);
      rect(120, 70, -20, -70);
    
      checkBoundaries(); 
    
      difference = millis() - count_at_reset;
      sec = difference /1000;
      fill(255);
      textSize(26);
      text(sec, 300, 30);
    
      // PLAYER
      stroke(0); 
      fill(0, 255, 0);
      rect(x, y, 10, 10);
    
      if (y < 1) {
        state=WON;
      }
    
      inputs();
    
      // --------------------------------
    
    
    
      if (x < 0 ) { 
        x = 0;
      }
    
      if (y > height-10 ) { 
        y = height-10;
    
        if (mouseX == 38 && mouseY == 14) {
          fill(#AAAAAA);
          textSize(56);
          text("You found our secret!", 150, 150);
        }
      }
    }
    
    void drawForFail() {
      fill(255); 
      textSize(140); 
      text("FAIL", 60, 370);
      textSize(22);
      text("Press v to replay", 210, 70);
      inputs();
    }
    
    void drawForWon() {
      fill(255);
      textSize(30);
      text("YOU WON\n"
        +sec
        +"\nPress v to reset.", 50, 50);
    } 
    
    // -----------------------------------------------
    // Tools
    
    void inputs() {
    
      if (!keyPressed) {
        return;
      }
    
      if (key == 'v'||key == 'V') {
        reset();
        x = 10;
        y = 380;
      }
    
      if (key == 'q'||key == 'Q') {
        y = y - 1;
      }
    
      if (key == 's'||key == 'S') {
        y = y + 1;
      }
    
      if (key == 'u'||key == 'U') {
        x = x - 1;
      }
    
      if (key == 'i'||key == 'I') {
        x = x + 1;
      }
    
      if (key==' ') {
        /// if (state==FAIL) 
        state=NORMAL;
        x = 10;
        y = 380;
      }
      //
    
    
      if (key == 'v') {
        if (state==FAIL) {
          state=NORMAL; 
          x = 10;
          y = 380;
          reset();
        }
      }
    }//
    
    void keyPressed() {
      if (key == 'v') {
        if (state==WON) {
          state=NORMAL; 
          x = 10;
          y = 380;
          reset();
        }
      }
    }
    
    
    void checkBoundaries() {
      if (x > 20 && y > 221) {
        state = FAIL;
      }
    
      if (x > 340 && y < 222 && y > 192) {
        state = FAIL;
      }
    
      if (x > 180 && y < 200) {
        state = FAIL;
      }
    
      if (x > 90 && x < 181 && y < 200 && y > 140) {
        state = FAIL;
      }
    
      if (x > 90 && x < 130 && y < 141 && y > 110) {
        state = FAIL;
      }
    
      if (x > 110 && x < 181 && y < 50 && y > 0) {
        state = FAIL;
      }
    
      if (x > -1 && x < 80 && y < 200 && y > -1) {
        state = FAIL;
      }
    
      if (x > 79 && x < 100 && y < 100 && y > -1) {
        state = FAIL;
      }
    
      if (x > 99 && x < 170 && y < 100 && y > 60) {
        state = FAIL;
      }
    
      if (x > 140 && x < 170 && y > 99 && y < 130) {
        state = FAIL;
      }
    }
    
    void reset() {
      count_at_reset = millis();
    } 
    
  • Where does the if class in line 98 end and why???

  • WOW somehow I did not see that. Also why are these lines of code here?

    if (key==' ') {
        /// if (state==FAIL) 
        state=NORMAL;
        x = 10;
        y = 380;
      }
    
  • the user can hit space bar to reset

    you can delete them when you like "v" more

  • Thank you, Chrisir.

  • edited April 2018

    Chrisir, why cant I put these lines of code in void inputs()?

    if (key == 'v') {
        if (state==WON) {
          state=NORMAL; 
          x = 10;
          y = 380;
          reset();
        }
      }
    

    I changed these lines of code:

    void keyPressed() {
      if (key == 'v') {
        if (state==WON) {
          state=NORMAL; 
          x = 10;
          y = 380;
          reset();
        }
      }
    }
    

    to the first lines of code I showed you, yet it still didn't work. Can you help me?

  • edited April 2018

    As we discussed inputs() is not called in the state WON - see function drawForOne

    It’s not called to make sure that the user can’t move the character when he has won

    Since keyPressed() gets called automatically by processing (other than inputs()) it gets called in all states and therefore works here.

    In bigger projects we would also use switch(state) in keyPressed() to make sure that keys are handled differently in each state.

    There is another major difference when using keyPressed() as a function : here each key press is evaluated only once. This means that the steering of the character would not be as smooth in keyPressed() because you would hit the key again and again instead of just holding it down.

    Chrisir

  • Thank you because it made me think of a very easy solution: just put these lines of code into void inputs()

    if (key == 'v') {
        if (state==WON) {
          state=NORMAL; 
          x = 10;
          y = 380;
          reset();
        }
      }
    

    then put inputs() in void drawForWon().

  • then put inputs() in void drawForWon().

    Well, that's bad since then you can move your character when he has reached the target. Bad.

    As I wrote:

    In bigger projects we would also use switch(state) in keyPressed() to make sure that keys are handled differently in each state.

  • Well, that's bad since then you can move your character when he has reached the target.

    That didn't happen to me. I can't move my character.

  • edited April 2018

    Now I've changed the easter egg so it will just show some random shapes.

    if (mouseX == 38 && mouseY == 14) {
        fill(#AAAAAA);
        rect(368, 92, 76, 165);
        ellipse(82, 78, 71, 71);
      }
    }
    

    Probably will be more than just this. Also, what happens when I press the "export" button?

  • Easter egg will just flash briefly..?

    Export means that you make an executable file that runs without processing on any machine (where Java is installed I think).

  • edited April 2018

    Easter egg will just flash briefly..?

    When I put it in draw() and in the if statement to see what it would look like, it doesn't flash.

    /*
     Press 'q' to move up.
     Press 's' to move down.
     Press 'u' to move left.
     Press 'i' to move right.
     Press 'v' to restart.
     Get to the end as fast as possible.
     If you go on to the red areas, you  will fail.
     */
    
    // The variable state 
    // the constants the variable state can have as values 
    final int NORMAL = 0; // unique numbers 
    final int FAIL   = 1;
    final int WON    = 2;
    int state = NORMAL; 
    
    int count_at_reset;
    float x = 10;
    float y = 380;
    
    int difference, sec;
    
    void setup() {
      size(400, 400);
      frameRate(60);
    }
    
    
    void draw() {
    
      switch(state) {
    
      case NORMAL:
        drawForNormal(); 
        break; 
    
      case FAIL:
        drawForFail(); 
        break; 
    
      case WON:
        drawForWon(); 
        break;
    
      default:
        println("Error");
        break;
      } // switch 
      //
    } // func 
    
    // ----------------------------------------
    // functions called by draw() 
    
    void drawForNormal() {
    
      background(255, 0, 0);
      fill(0, 255, 255);
      noStroke(); 
      rect(0, 200, 30, 201);
      rect(30, 200, 320, 30);
      rect(80, 200, 20, -100);
      rect(100, 100, 30, 20);
      rect(130, 100, 20, 50);
      rect(150, 130, 20, 20);
      rect(170, 150, 20, -100);
      rect(170, 50, -50, 20);
      rect(120, 70, -20, -70);
    
      checkBoundaries(); 
    
      difference = millis() - count_at_reset;
      sec = difference /1000;
      fill(255);
      textSize(26);
      text(sec, 300, 30);
    
      // PLAYER
      stroke(0); 
      fill(0, 255, 0);
      rect(x, y, 10, 10);
    
      if (y < 1) {
        state=WON;
      }
    
      inputs();
    
      // --------------------------------
    
    
    
      if (x < 0 ) { 
        x = 0;
      }
    
      if (y > height-10 ) { 
        y = height-10;
      }
    
      if (mouseX == 38 && mouseY == 14) {
        fill(#AAAAAA);
        rect(368, 92, 76, 165);
        ellipse(82, 78, 71, 71);
        triangle(284, 350, 142, 72, 50, 1);
        quad(47, 97, 58, 4, 157, 149, 49, 28);
      }
    fill(#AAAAAA);
        rect(368, 92, 76, 165);
        ellipse(82, 78, 71, 71);
        triangle(284, 350, 142, 72, 50, 1);
        quad(47, 97, 58, 4, 157, 149, 49, 28);}
    
    void drawForFail() {
      fill(255); 
      textSize(140); 
      text("FAIL", 60, 370);
      textSize(22);
      text("Press v to replay", 210, 70);
      inputs();
    }
    
    void drawForWon() {
      fill(255);
      textSize(30);
      text("YOU WON\n"
        +sec
        +"\nPress v to reset.", 80, 270);
      inputs();
    }
    
    // -----------------------------------------------
    // Tools
    
    void inputs() {
    
      if (!keyPressed) {
        return;
      }
    
      if (key == 'v'||key == 'V') {
        reset();
        x = 10;
        y = 380;
      }
    
      if (key == 'q'||key == 'Q') {
        y = y - 1;
      }
    
      if (key == 's'||key == 'S') {
        y = y + 1;
      }
    
      if (key == 'u'||key == 'U') {
        x = x - 1;
      }
    
      if (key == 'i'||key == 'I') {
        x = x + 1;
      }
    
      if (key == 'v') {
        if (state==WON) {
          state=NORMAL; 
          x = 10;
          y = 380;
          reset();
        }
      }
    
    
      if (key == 'v') {
        if (state==FAIL) {
          state=NORMAL; 
          x = 10;
          y = 380;
          reset();
        }
      }
    }//
    
    
    
    void checkBoundaries() {
      if (x > 20 && y > 221) {
        state = FAIL;
      }
    
      if (x > 340 && y < 222 && y > 192) {
        state = FAIL;
      }
    
      if (x > 180 && y < 200) {
        state = FAIL;
      }
    
      if (x > 90 && x < 181 && y < 200 && y > 140) {
        state = FAIL;
      }
    
      if (x > 90 && x < 130 && y < 141 && y > 110) {
        state = FAIL;
      }
    
      if (x > 110 && x < 181 && y < 50 && y > 0) {
        state = FAIL;
      }
    
      if (x > -1 && x < 80 && y < 200 && y > -1) {
        state = FAIL;
      }
    
      if (x > 79 && x < 100 && y < 100 && y > -1) {
        state = FAIL;
      }
    
      if (x > 99 && x < 170 && y < 100 && y > 60) {
        state = FAIL;
      }
    
      if (x > 140 && x < 170 && y > 99 && y < 130) {
        state = FAIL;
      }
    }
    
    void reset() {
      count_at_reset = millis();
    } 
    
  • How are you even able to get the mouse at that exact position? And what if I wanted to make this game online some day?

  • Exact position of mouse: As said, better use >....&& ....< .... etc. to make the spot a little bigger for the mouse

    Second question summoning jeremydouglas

  • It’s very hard to hit a pixel exactly

    Better work with

    if(mouseX>34 && 
       mouseX<45 &&
       mouseY>..... &&
       mouseY< ......) { 
    

    Also you need this to be in draw() not in mousePressed() for example so that it’s displaying throughout and not only flashes briefly

  • Thank you very much, kfrajer. Sorry for not using your code.

    Also you need this to be in draw() not in mousePressed() for example so that it’s displaying throughout and not only flashes briefly

    Chrisir, it's not in mousePressed().

  • what if I wanted to make this game online some day?

    try

    https://www.openprocessing.org/home/join

  • edited April 2018

    Chrisir, when will this line of code occur?

    if (!keyPressed) {
        return;
      }
    
  • Nvm I figured it out by getting rid of it temporarily.

  • It’s leaving the function immediately so the rest of the function can assume that the key is pressed

    The ! sign means not

  • Chrisir, why are these lines of code in draw()?

    fill(255);

    It will just get overwritten by fill(0, 255, 255); in drawForNormal().

  • I can’t see it in your last full sketch above

    So you are probably right

    But remember to press ctrl-t regularly for auto format

  • edited May 2018

    I've done this to switch(state).

    default:
        state = NORMAL;
        println("Error");
        break;
    

    If somehow there might still be something wrong, then please tell me. Also, here is the code now.

    /*
     Press 'q' to move up.
     Press 's' to move down.
     Press 'u' to move left.
     Press 'i' to move right.
     Press 'v' to restart.
     Get to the end as fast as possible.
     If you go on to the red areas, you will fail.
     */
    
    // The variable state
    // the constants the variable state can have as values
    final int MIN_DISTA = 3; // unique numbers
    final PVector eggLocation = new PVector(38, 14);
    final int NORMAL = 0;
    final int FAIL   = 1;
    final int WON    = 2;
    int state = NORMAL;
    
    float x = 10;
    float y = 380;
    
    int difference, sec, count_at_reset;
    
    void setup() {
      size(400, 400);
      frameRate(60);
    }
    
    void draw() {
    
      switch(state) {
    
      case NORMAL:
        drawForNormal();
        break;
    
      case FAIL:
        drawForFail();
        break;
    
      case WON:
        drawForWon();
        break;
    
      default:
        state = NORMAL;
        println("Error");
        break;
      }
    }
    
    // ----------------------------------------
    // functions called by draw() 
    
    void drawForNormal() {
      background(255, 0, 0);
      fill(0, 255, 255);
      noStroke();
      rect(0, 200, 30, 201);
      rect(30, 200, 320, 30);
      rect(80, 200, 20, -100);
      rect(100, 100, 30, 20);
      rect(130, 100, 20, 50);
      rect(150, 130, 20, 20);
      rect(170, 150, 20, -100);
      rect(170, 50, -50, 20);
      rect(120, 70, -20, -70);
    
      checkBoundaries();
    
      difference = millis() - count_at_reset;
      sec = difference /1000;
      fill(255);
      textSize(26);
      text(sec, 300, 30);
    
      // PLAYER
      stroke(0);
      fill(0, 255, 0);
      rect(x, y, 10, 10);
    
      if (y < 1) {
        state=WON;
      }
    
      inputs();
    
      if (x < 0 ) { 
        x = 0;
      }
    
      if (y > height-10 ) {
        y = height-10;
      }
    
      if (dist(mouseX, mouseY, eggLocation.x, eggLocation.y)< MIN_DISTA || x == 219) {
        stroke(#F26E72);
        fill(#10FF58);
        rect(368, 92, 76, 165);
        stroke(#12B6F8);
        fill(#46FA47);
        ellipse(82, 78, 71, 71);
        stroke(#EB10BA);
        fill(#87FE65);
        triangle(284, 350, 142, 72, 50, 1);
        stroke(#A4BFC0);
        fill(#86ED91);
        quad(47, 97, 58, 4, 157, 149, 49, 28);
      }
    }
    
    void drawForFail() {
      fill(255);
      textSize(140);
      text("FAIL", 60, 370);
      textSize(22);
      text("Press v to replay", 210, 70);
      inputs();
    }
    
    void drawForWon() {
      fill(255);
      textSize(30);
      text("YOU WON\n"
        +sec
        +"\nPress v to reset.", 80, 270);
      inputs();
    }
    
    // -----------------------------------------------
    // Tools
    
    void inputs() {
    
      if (!keyPressed) {
        return;
      }
    
      if (key == 'v'||key == 'V') {
        reset();
      }
    
      if (key == 'q'||key == 'Q') {
        y--;
      }
    
      if (key == 's'||key == 'S') {
        y++;
      }
    
      if (key == 'u'||key == 'U') {
        x--;
      }
    
      if (key == 'i'||key == 'I') {
        x++;
      }
    }
    
    void checkBoundaries() {
      if (x > 20 && y > 221) {
        state = FAIL;
      }
    
      if (x > 340 && y < 222 && y > 192) {
        state = FAIL;
      }
    
      if (x > 180 && y < 200) {
        state = FAIL;
      }
    
      if (x > 90 && x < 181 && y < 200 && y > 140) {
        state = FAIL;
      }
    
      if (x > 90 && x < 130 && y < 141 && y > 110) {
        state = FAIL;
      }
    
      if (x > 110 && x < 181 && y < 50 && y > 0) {
        state = FAIL;
      }
    
      if (x > -1 && x < 80 && y < 200 && y > -1) {
        state = FAIL;
      }
    
      if (x > 79 && x < 100 && y < 100 && y > -1) {
        state = FAIL;
      }
    
      if (x > 99 && x < 170 && y < 100 && y > 60) {
        state = FAIL;
      }
    
      if (x > 140 && x < 170 && y > 99 && y < 130) {
        state = FAIL;
      }
    }
    
    void reset() {
      state=NORMAL;
      count_at_reset = millis();
      x = 10;
      y = 380;
    }
    
  • Looks good

  • Chrisir, why does NORMAL have formatting?

  • It seems to be a pre-defined constant like LEFT or RIGHT.

    Work around: have all 3 constants (not only NORMAL!) with a prefix STATE_

  • Thank you, Chrisir.

  • @Vaz123 -- these pre-defined constant keywords are special values that have specific meanings to certain built-in Processing functions.

    You can find a complete list of them here:

    and here in the source code:

    ...but in general, which constants are relevant is discussed in the documentation of a particular method.

  • edited May 2018

    It's coincidence that Vaz named a variable same as a constant that already exists. The new variable is overshadowing the existing one, but the color routine for the code (better word needed) still highlights the word (falsely) as an internal special constant (keyword).

    Obviously the color routine for the code is not connected to the parser (hence this minor error in the formatting of the word NORMAL).

    ;-)

  • In PDE, when you type the name of a recognized built-in constant that word will turn gray-green. This gives you a hint that you are shadowing a built-in keyword if this is something that you want to avoid.

    Screen Shot 2018-05-18 at 12.07.35 PM

  • I'm now trying to add a minute digit into the timer, but it's not working. Can you please help me?

        int min = 0;
    //...
    difference = millis() - count_at_reset;
          sec = difference /1000;
          fill(255);
          textSize(26);
          text(sec, 300, 30);
          text(min +":", 275, 30);
          if (difference > 60) {
            sec = 0;
            min++;
          }
    
  • edited May 2018
    void setup() {
      size(200, 200);
    }
    
    void draw() {
      background(0);
      int  difference = millis()/1000 - 0;  //Time difference wrt the beginning of times
      int currentSecs=difference%60;
      int currentMins= (difference-currentSecs)/60;  //*** EDITED
      text("Time lapsed: "+nfs(currentMins, 2, 0)+":"+nfs(currentSecs, 2, 0), width/2, height/2);
    }
    

    Kf

  • edited May 2018

    Thank you kfrajer. But could you make it so that mins has only one digit? I would also appreciate it if you could help me put the text "SLOW" once the time reaches 9:59.

    difference = millis()/1000 - 0;
      sec = difference%60;
      min = (difference-sec)%60;
      fill(255);
      textSize(26);
      text(nfs(min,2,0)+":"+nfs(sec,2,0), 300, 30);
    
  • edited May 2018

    You can check the documentation for nfs(). So it will be

    text(nfs(min,1,0)+":"+nfs(sec,2,0), 300, 30); //NOTICE change 2 to 1

    For the text SLOW, it is like this (untested):

    final int SLOWTIME=9*60+59; // 9:59 in seconds
    
    void setup() {
      size(200, 200);
    }
    
    void draw() {
      background(0);
      int  difference = millis()/1000 - 0;  //Time difference wrt the beginning of times
    
      if(difference<SLOWTIME){
        int currentSecs=difference%60;
        int currentMins= (difference-currentSecs)/60;  //*** EDITED
        text("Time lapsed: "+nfs(currentMins, 2, 0)+":"+nfs(currentSecs, 2, 0), width/2, height/2);
      }
      else{
        text("SLOW", width/2, height/2);
      }
    }
    

    Kf

  • edited May 2018

    Now the reset function isn't working. See my code so you can know how things are.

    /*
     Press 'q' to move up.
     Press 's' to move down.
     Press 'u' to move left.
     Press 'i' to move right.
     Press 'v' to restart.
     Get to the end as fast as possible.
     If you go on to the red areas, you will fail.
     */
    
    // The variable state
    // the constants the variable state can have as values
    final int MIN_DISTA = 3; // unique numbers
    final PVector eggLocation = new PVector(38, 14);
    final int STATE_NORMAL = 0;
    final int STATE_FAIL = 1;
    final int STATE_WON = 2;
    final int STATE_SLOW = 3;
    final int SLOW_TIME = 9*60+59;
    int state = STATE_NORMAL;
    
    float x = 10;
    float y = 380;
    
    int difference, sec, count_at_reset;
    int min;
    
    void setup() {
      size(400, 400);
      frameRate(60);
    }
    
    void draw() {
    
      switch(state) {
    
      case STATE_NORMAL:
        drawForNormal();
        break;
    
      case STATE_FAIL:
        drawForFail();
        break;
    
      case STATE_WON:
        drawForWon();
        break;
    
      case STATE_SLOW:
        drawForSlow();
        break;
    
      default:
        state = NORMAL;
        println("Error");
        break;
      }
    }
    
    // ----------------------------------------
    // functions called by draw() 
    
    void drawForNormal() {
      background(255, 0, 0);
      fill(0, 255, 255);
      noStroke();
      rect(0, 200, 30, 201);
      rect(30, 200, 320, 30);
      rect(80, 200, 20, -100);
      rect(100, 100, 30, 20);
      rect(130, 100, 20, 50);
      rect(150, 130, 20, 20);
      rect(170, 150, 20, -100);
      rect(170, 50, -50, 20);
      rect(120, 70, -20, -70);
    
      checkBoundaries();
    
      if (difference<SLOW_TIME) {
        difference = millis()/1000 - 0;
        sec = difference%60;
        min = (difference-sec)%60;
        fill(255);
        textSize(26);
        text(nfs(min, 2, 0)+":"+nfs(sec, 2, 0), 300, 30);
      } else {
        state = STATE_SLOW;
      }
      // PLAYER
      stroke(0);
      fill(0, 255, 0);
      rect(x, y, 10, 10);
    
      if (y < 1) {
        state=STATE_WON;
      }
    
      inputs();
    
      if (x < 0 ) { 
        x = 0;
      }
    
      if (y > height-10 ) {
        y = height-10;
      }
    
      if (dist(mouseX, mouseY, eggLocation.x, eggLocation.y)< MIN_DISTA) {
        stroke(#F26E72);
        fill(#10FF58);
        rect(368, 92, 76, 165);
        stroke(#12B6F8);
        fill(#46FA47);
        ellipse(82, 78, 71, 71);
        stroke(#EB10BA);
        fill(#87FE65);
        triangle(284, 350, 142, 72, 50, 1);
        stroke(#A4BFC0);
        fill(#86ED91);
        quad(47, 97, 58, 4, 157, 149, 49, 28);
      }
      if (x == 219) {
        stroke(#FF7460); 
        line(35, 55, 245, 23);
      }
    }
    
    void drawForFail() {
      fill(255);
      textSize(140);
      text("FAIL", 60, 370);
      textSize(22);
      text("Press v to replay", 210, 70);
      inputs();
    }
    
    void drawForWon() {
      fill(255);
      textSize(30);
      text("YOU WON\n"
        +sec
        +"\nPress v to reset.", 80, 270);
      inputs();
    }
    
    void drawForSlow() {
      text("SLOW", 210, 70);
      inputs();
    }
    
    // -----------------------------------------------
    // Tools
    
    void inputs() {
    
      if (!keyPressed) {
        return;
      }
    
      if (key == 'v'||key == 'V') {
        reset();
      }
    
      if (key == 'q'||key == 'Q') {
        y--;
      }
    
      if (key == 's'||key == 'S') {
        y++;
      }
    
      if (key == 'u'||key == 'U') {
        x--;
      }
    
      if (key == 'i'||key == 'I') {
        x++;
      }
    }
    
    void checkBoundaries() {
      if (x > 20 && y > 221) {
        state = STATE_FAIL;
      }
    
      if (x > 340 && y < 222 && y > 192) {
        state = STATE_FAIL;
      }
    
      if (x > 180 && y < 200) {
        state = STATE_FAIL;
      }
    
      if (x > 90 && x < 181 && y < 200 && y > 140) {
        state = STATE_FAIL;
      }
    
      if (x > 90 && x < 130 && y < 141 && y > 110) {
        state = STATE_FAIL;
      }
    
      if (x > 110 && x < 181 && y < 50 && y > 0) {
        state = STATE_FAIL;
      }
    
      if (x > -1 && x < 80 && y < 200 && y > -1) {
        state = STATE_FAIL;
      }
    
      if (x > 79 && x < 100 && y < 100 && y > -1) {
        state = STATE_FAIL;
      }
    
      if (x > 99 && x < 170 && y < 100 && y > 60) {
        state = STATE_FAIL;
      }
    
      if (x > 140 && x < 170 && y > 99 && y < 130) {
        state = STATE_FAIL;
      }
    }
    
    void reset() {
      state = STATE_NORMAL;
      count_at_reset = millis();
      x = 10;
      y = 380;
    }
    
  • Don’t be lazy

    Tty to fix it

    Write exactly what happens falsely and what you want to happen instead

    Which line numbers?

    Does the code run or not?

    What error happens?

  • I managed to get the reset function working properly again, kind of. If I HOLD 'v' instead of just press it, the time will go from 0:00 to 0:01 for a brief moment. Also, there were no errors, so the sketch ran as usual, except the function void reset() didn't work as intended. Also, the minute digit will not increment once the second digit reaches 60. Could you help me with that?

    /*
     Press 'q' to move up.
     Press 's' to move down.
     Press 'u' to move left.
     Press 'i' to move right.
     Press 'v' to restart.
     Get to the end as fast as possible.
     If you go on to the red areas, you will fail.
     */
    
    // The variable state
    // the constants the variable state can have as values
    final int MIN_DISTA = 3; // unique numbers
    final PVector eggLocation = new PVector(38, 14);
    final int STATE_NORMAL = 0;
    final int STATE_FAIL = 1;
    final int STATE_WON = 2;
    final int SLOW_TIME = 9*60+59;
    int state = STATE_NORMAL;
    
    float x = 10;
    float y = 380;
    
    int difference, sec, count_at_reset;
    int min;
    
    void setup() {
      size(400, 400);
      frameRate(60);
    }
    
    void draw() {
    
      switch(state) {
    
      case STATE_NORMAL:
        drawForNormal();
        break;
    
      case STATE_FAIL:
        drawForFail();
        break;
    
      case STATE_WON:
        drawForWon();
        break;
    
      default:
        state = STATE_NORMAL;
        println("Error");
        break;
      }
    }
    
    // ----------------------------------------
    // functions called by draw() 
    
    void drawForNormal() {
      background(255, 0, 0);
      fill(0, 255, 255);
      noStroke();
      rect(0, 200, 30, 201);
      rect(30, 200, 320, 30);
      rect(80, 200, 20, -100);
      rect(100, 100, 30, 20);
      rect(130, 100, 20, 50);
      rect(150, 130, 20, 20);
      rect(170, 150, 20, -100);
      rect(170, 50, -50, 20);
      rect(120, 70, -20, -70);
    
      checkBoundaries();
    
      if (difference<SLOW_TIME) {
        difference = millis()/1000 - count_at_reset/1000;
        sec = difference%60;
        min = (difference-sec)%60;
        fill(255);
        textSize(26);
        text(nfs(min, 1, 0)+":"+nfs(sec, 2, 0), 300, 30);
      } else {
        textSize(140);
        text("SLOW", 60, 370);
      }
      // PLAYER
      stroke(0);
      fill(0, 255, 0);
      rect(x, y, 10, 10);
    
      if (y < 1) {
        state=STATE_WON;
      }
    
      inputs();
    
      if (x < 0 ) { 
        x = 0;
      }
    
      if (y > height-10 ) {
        y = height-10;
      }
    
      if (dist(mouseX, mouseY, eggLocation.x, eggLocation.y)< MIN_DISTA) {
        stroke(#F26E72);
        fill(#10FF58);
        rect(368, 92, 76, 165);
        stroke(#12B6F8);
        fill(#46FA47);
        ellipse(82, 78, 71, 71);
        stroke(#EB10BA);
        fill(#87FE65);
        triangle(284, 350, 142, 72, 50, 1);
        stroke(#A4BFC0);
        fill(#86ED91);
        quad(47, 97, 58, 4, 157, 149, 49, 28);
      }
      if (x == 219) {
        stroke(#FF7460); 
        line(35, 55, 245, 23);
      }
    }
    
    void drawForFail() {
      fill(255);
      textSize(140);
      text("FAIL", 60, 370);
      textSize(22);
      text("Press v to replay", 210, 70);
      inputs();
    }
    
    void drawForWon() {
      fill(255);
      textSize(30);
      text("YOU WON\n"
        +sec
        +"\nPress v to reset.", 80, 270);
      inputs();
    }
    
    // -----------------------------------------------
    // Tools
    
    void inputs() {
    
      if (!keyPressed) {
        return;
      }
    
      if (key == 'v'||key == 'V') {
        reset();
      }
    
      if (key == 'q'||key == 'Q') {
        y--;
      }
    
      if (key == 's'||key == 'S') {
        y++;
      }
    
      if (key == 'u'||key == 'U') {
        x--;
      }
    
      if (key == 'i'||key == 'I') {
        x++;
      }
    }
    
    void checkBoundaries() {
      if (x > 20 && y > 221) {
        state = STATE_FAIL;
      }
    
      if (x > 340 && y < 222 && y > 192) {
        state = STATE_FAIL;
      }
    
      if (x > 180 && y < 200) {
        state = STATE_FAIL;
      }
    
      if (x > 90 && x < 181 && y < 200 && y > 140) {
        state = STATE_FAIL;
      }
    
      if (x > 90 && x < 130 && y < 141 && y > 110) {
        state = STATE_FAIL;
      }
    
      if (x > 110 && x < 181 && y < 50 && y > 0) {
        state = STATE_FAIL;
      }
    
      if (x > -1 && x < 80 && y < 200 && y > -1) {
        state = STATE_FAIL;
      }
    
      if (x > 79 && x < 100 && y < 100 && y > -1) {
        state = STATE_FAIL;
      }
    
      if (x > 99 && x < 170 && y < 100 && y > 60) {
        state = STATE_FAIL;
      }
    
      if (x > 140 && x < 170 && y > 99 && y < 130) {
        state = STATE_FAIL;
      }
    }
    
    void reset() {
      state = STATE_NORMAL;
      count_at_reset = millis();
      x = 10;
      y = 380;
    }
    
  • My mistake, this line min = (difference-sec)%60; needs to be changed to

    min = (difference-sec)/60;

    I have updated my posts above.

    except the function void reset() didn't work as intended

    If a press v, the game resets. So I don't see a problem there. Also consider using AWSD letters to move your player instead of using u+i for lateral movement, unless you have a specific reason to do it using those keys instead.

    Kf

Sign In or Register to comment.