How do I make the timer stop and reset?

13»

Answers

  • edited May 2018

    If a press v, the game resets. So I don't see a problem there.

    Hold 'v'. Look at the timer. You should see something wrong. And another thing; if you wait until the time goes all the way to 9:59, the new state that I called STATE_SLOW will be there forever. 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, STATE_NORMAL = 0, STATE_FAIL = 1, STATE_WON = 2, STATE_SLOW = 3, SLOW_TIME = 9*60+59; // unique numbers
    final PVector eggLocation = new PVector(38, 14);
    float x = 10, y = 380;
    
    int difference, sec, count_at_reset, min, state = STATE_NORMAL;
    
    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 = 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 {
        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() {
      fill(255);
      textSize(130);
      text("SLOW", 40, 370);
      textSize(30);
      text("Try again", 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;
    }
    
  • Could you help me with that?

    What have you tried??

  • Maybe if you describe your game better. For example, create a full description of what the game does. Describe the states. What should happen.

    the new state which I called STATE_SLOW will be there forever

    It is going to state there forever unless there is a trigger condition that will change it to another state, either by user input, another timer, etc. This is gracious handling that is done by you. We don't know how to fix it because we don't know what is the design. step back, provide a full description and then describe your problem again and it should make senses.

    I press the v key before and hold it and the only thing I noticed is that the time resets and sometimes you can see that the time changes from 0 to 1 second but it goes back to 0. I am using Win10 OS. In your case, when pressing and holding the v key, describe your problem as this problem could be OS dependent.

    Kf

  • I can only agree.

    You need to say clearly what you want to achieve

    e.g. the state_slow: if this is like the normal game but just with the text SLOW, then don't make it an extra state in the first place.

    Instead make it part of state STATE_NORMAL and drawForNormal();

  • the time changes from 0 to 1 second but it goes back to 0

    That's the problem I had, and I am using windows 7.

    Also, I don't understand what you mean by

    This is gracious handling that is done by you. We don't know how to fix it because we don't know what is the design. step back, provide a full description and then describe your problem again and it should make senses.

    Chrisir, I don't want the character to move once the timer reaches 9:59. So I had to make an extra function that only happens when the state is STATE_SLOW.

  • edited May 2018

    Again, did you notice that kfrajer and I both write longer than you did? But it's your interest not ours to get help...

    the new state that I called STATE_SLOW will be there forever. Could you help me with that?

    How do you want the state STATE_SLOW to end? I mean when, under which circumstances? When the user hits 'v'?

  • Chrisir, I don't want the character to move once the timer reaches 9:59. So I had to make an extra function that only happens when the state is STATE_SLOW.

    Understood. Makes sense.

  • STATE_SLOW should better be named

    STATE_TIME_OUT

  • How do you want the state STATE_SLOW to end? I mean when, under which circumstances? When the user hits 'v'?

    Yes, I would like STATE_SLOW to end once the user hits 'v'.

  • That should work already since you call inputs() from drawForSlow()

    Does it?

    You could also distinguish in inputs() between the keys for the states using a switch () like in draw()

  • edited May 2018

    That should work already since you call inputs() from drawForSlow()

    It should, but it doesn't. When I press 'v' while the state is STATE_SLOW, the 'SLOW' text disappears but I don't get control of my character. After I let go of 'v', the 'SLOW' text appears again. 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, STATE_NORMAL = 0, STATE_FAIL = 1, STATE_WON = 2, STATE_SLOW = 3, SLOW_TIME = 9*60+59; // unique numbers
    final PVector eggLocation = new PVector(38, 14);
    float x = 10, y = 380;
    
    int difference, sec, count_at_reset, min, state = STATE_NORMAL;
    
    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 = 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 {
        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() {
      fill(255);
      textSize(130);
      text("SLOW", 40, 370);
      textSize(30);
      text("Try again", 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;
    }
    
  • Oh, one problem is: inputs() is designed to receive keys throughout.

    This is because of usage of key directly.

    Therefore one key press is registered multiple times.

    This is good for movement but not for ‚v‘.

    Instead you could make a news function keyPressed () which gets called automatically and does register a key only once. Good for v.

    Keep your other letters in inputs ().

  • edited May 2018

    I put 'v' in void keyPressed() but if I hold 'v', it still goes from 0 to 1 for a brief moment then back to 0. 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, STATE_NORMAL = 0, STATE_FAIL = 1, STATE_WON = 2, STATE_SLOW = 3, SLOW_TIME = 9*60+59; // unique numbers
    final PVector eggLocation = new PVector(38, 14);
    float x = 10, y = 380;
    
    int difference, sec, count_at_reset, min, state = STATE_NORMAL;
    
    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 = 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 {
        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() {
      fill(255);
      textSize(130);
      text("SLOW", 40, 370);
      textSize(30);
      text("Try again", 210, 70);
      inputs();
    }
    
    // -----------------------------------------------
    // Tools
    
    void inputs() {
    
      if (!keyPressed) {
        return;
      }
    
      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 keyPressed() {
      if (key == 'v'||key == 'V') {
        reset();
      }
    }
    
    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;
    }
    
  • In mousePressed you can disable the counter (and set it to zero). On keyReleased, you enable the counter back again.

    Kf

  • In mousePressed you can disable the counter (and set it to zero)

    Sorry, but I don't understand what you mean.

  • Or replace mousePressed with keyPressed() in that statement?

  • I don't see mousePressed anywhere.

  • I meant replace mousePressed with keyPressed in kfrajer‘s statement

  • In mousePressed you can disable the counter (and set it to zero)

    Could anyone explain this?

  • edited June 2018

    those lines:

    // The variable state
    // the constants the variable state can have as values
    final int MIN_DISTA = 3, STATE_NORMAL = 0, STATE_FAIL = 1, STATE_WON = 2, STATE_SLOW = 3, SLOW_TIME = 9*60+59; // unique numbers
    

    are not good practice.

    Because the comments do not match the code:

    • MIN_DISTA and SLOW_TIME have nothing to do with states.

    (and // unique numbers doesn't make sense in terms of SLOW_TIME; only the states must be unique numbers)

    Just because they are all constants, you shouldn't put them in one line.

    Better separate them.

    Also, don't go for as few lines as possible.

    Better go for good readability, good comments.

    Also you separated state = STATE_NORMAL; from its state constants.

    (some would argue to have first ALL constants (with final) and then the variables, but I think in this case we can leave constants for states and the variable state together)

    Suggestion for this code section:

    // the constants the variable state can have as values:
    final int STATE_NORMAL = 0;  // unique numbers
    final int STATE_FAIL   = 1;
    final int STATE_WON    = 2;
    final int STATE_SLOW   = 3;
    int state = STATE_NORMAL; // The variable state
    
    // other constants  
    final int MIN_DISTA = 3; 
    final int SLOW_TIME = 9*60+59;
    

    The empty line

    the empty line after int state = STATE_NORMAL; // The variable state suggests beautifully that a new section starts here (new section because the topic of states is now over). It's a good habit to leave empty lines throughout the code where a new topic starts.

    Rule of Thumb

    See, don't try to save lines. Try to save the time of the guy who will read your code later (or your own time when you have to read your own code in 6 months).

  • edited June 2018

    Please use wasd and not some keys qsui nobody understands

    instead of SLOW it should say GAME OVER

  • edited June 2018

    you had the issue, that 'v' didn't work after SLOW:

    if you look at this line

      if (difference<SLOW_TIME) {
    

    it decides if we are in SLOW or not. So the error must be here or in a variable used here!!!

    There are 2 variables:

    • SLOW_TIME and

    • difference.

    Which one causes the error??

    We first look at

    • SLOW_TIME. It never changes, since should be ok.

    • Then at difference: it is calculated as difference = millis()/1000 - count_at_reset/1000; but only AFTER our if statement if (difference<SLOW_TIME) {!!!

    So we need to reset difference in reset() function to make if (difference<SLOW_TIME) { work after 'v' in SLOW.

    I don't post my working version here but leave it to you.

    Best, Chrisir ;-)

  • See, don't try to save lines. Try to save the time of the guy who will read your code later (or your own time when you have to read your own code in 6 months).

    Thank you, Chrisir.

    instead of SLOW it should say GAME OVER

    I don't wan't there to be a game over. I don't think the game should have a game over. So instead I made it show the text 'SLOW'.

    Also I got the reset() function working simply by putting the calculation for difference outside of the if statement if (difference<SLOW_TIME) {. But now I have another problem. If I hold 'v', the timer will go from 0 to 1 then back to 0. This also causes the timer to occasionally start sooner than it is supposed to. Could you help me with that?

  • edited June 2018

    You should not hold v but just press it briefly.

    To hold it is wrong.

    Post your entire code please

  • edited June 2018

    You should not hold v but just press it briefly. To hold it os wrong.

    I know, but the other problem:

    This also causes the timer to occasionally start sooner than it is supposed to

    will still happen. 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 STATE_NORMAL = 0;  // unique numbers
    final int STATE_FAIL   = 1;
    final int STATE_WON    = 2;
    final int STATE_SLOW   = 3;
    int state = STATE_NORMAL; // The variable state
    
    // other constants  
    final int MIN_DISTA = 3; 
    final int SLOW_TIME = 9*60+59;
    final PVector eggLocation = new PVector(38, 14);
    float x = 10, y = 380;
    
    int difference, sec, count_at_reset, 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 = 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()/1000 - count_at_reset/1000;
    
      if (difference<SLOW_TIME) {
        sec = difference%60;
        min = (difference-sec)/60;
        fill(255);
        textSize(26);
        text(nfs(min, 1, 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(196, 3, 45, 7);
      }
    }
    
    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() {
      fill(255);
      textSize(130);
      text("SLOW", 40, 370);
      textSize(30);
      text("Try again", 210, 70);
      inputs();
    }
    
    // -----------------------------------------------
    // Tools
    
    void inputs() {
    
      if (!keyPressed) {
        return;
      }
    
      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 keyPressed() {
      if (key == 'v'||key == 'V') {
        reset();
      }
    }
    
    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;
    }
    
  • edited June 2018

    you can delete the call of inputs() in drawForSlow(),drawForWon() and drawForFail() now

    it's all in keypressed ()

    This:

    This also causes the timer to occasionally start sooner than it is supposed to...will still happen. Could you help me with that?

    I can not see this error. It doesn't happen to me.

    Of course when you press and hold v it starts and starts again...

    New version:

    /*
     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 STATE_NORMAL = 0;  // unique numbers
    final int STATE_FAIL   = 1;
    final int STATE_WON    = 2;
    final int STATE_SLOW   = 3;
    int state = STATE_NORMAL; // The variable state
    
    // other constants  
    final int MIN_DISTA = 3; 
    final int SLOW_TIME = 9*60+59;
    final PVector eggLocation = new PVector(38, 14);
    
    // variables 
    float x = 10, y = 380;
    
    int difference, sec, count_at_reset, min;
    
    // ----------------------------------------
    
    void setup() {
      size(400, 400);
      count_at_reset = millis();
      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 = 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()/1000 - count_at_reset/1000;
    
      if (difference<SLOW_TIME) {
        // normal play
        sec = difference%60;
        min = (difference-sec)/60;
        fill(255);
        textSize(26);
        text(nfs(min, 1, 0)+":"+trim(nfs(sec, 2, 0)), 300, 30);
      } else {
        // player was too slow 
        state = STATE_SLOW;
      }
    
      // PLAYER
      stroke(0);
      fill(0, 255, 0);
      rect(x, y, 10, 10);
    
      if (x < 0 ) { 
        x = 0;
      }
      if (y < 1) {
        state=STATE_WON;
      }
      if (y > height-10 ) {
        y = height-10;
      }
    
      inputs();
    
      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(196, 3, 45, 7);
      }
    }
    
    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() {
      fill(255);
      textSize(130);
      text("SLOW", 40, 370);
      textSize(30);
      text("Try again", 210, 70);
      //inputs();
    }
    
    // -----------------------------------------------
    // Tools
    
    void inputs() {
    
      if (!keyPressed) {
        return;
      }
    
      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 keyPressed() {
      if (key == 'v'||key == 'V') {
        reset();
      }
    }
    
    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;
    }
    //
    
  • edited June 2018

    I can not see this error

    Run the code and press 'v' a few times and also look at the timer. You should see it occasionally increment to 1 right after it resets.

    /*
     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 STATE_NORMAL = 0;  // unique numbers
    final int STATE_FAIL   = 1;
    final int STATE_WON    = 2;
    final int STATE_SLOW   = 3;
    int state = STATE_NORMAL; // The variable state
    
    // other constants  
    final int MIN_DISTA = 3; 
    final int SLOW_TIME = 9*60+59;
    final PVector eggLocation = new PVector(38, 14);
    float x = 10, y = 380;
    
    int difference, sec, count_at_reset, 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 = 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()/1000 - count_at_reset/1000;
    
      if (difference<SLOW_TIME) {
        sec = difference%60;
        min = (difference-sec)/60;
        fill(255);
        textSize(26);
        text(nfs(min, 1, 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(196, 3, 45, 7);
      }
    }
    
    void drawForFail() {
      fill(255);
      textSize(140);
      text("FAIL", 60, 370);
      textSize(22);
      text("Press v to replay", 210, 70);
    }
    
    void drawForWon() {
      fill(255);
      textSize(30);
      text("YOU WON\n"
        +sec
        +"\nPress v to reset.", 80, 270);
    }
    
    void drawForSlow() {
      fill(255);
      textSize(130);
      text("SLOW", 40, 370);
      textSize(30);
      text("Try again", 210, 70);
    }
    
    // -----------------------------------------------
    // Tools
    
    void inputs() {
    
      if (!keyPressed) {
        return;
      }
    
      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 keyPressed() {
      if (key == 'v'||key == 'V') {
        reset();
      }
    }
    
    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;
    }
    
  • On keyReleased, you enable the counter back again.

    Kfrajer, how do I do that?

  • Run the code and press 'v' a few times and also look at the timer. You should see it occasionally increment to 1 right after it resets.

    that's true.

    millis is counted right from the start, so when you look at this line:

     millis()/1000 - count_at_reset/1000
    

    and millis() is almost dividable by 1000 the first second you expect is almost over in no time

  • On keyReleased, you enable the counter back again.

    see variable vIsDown

    also I used text(nfs(min, 1, 0)+":"+trim(nfs(sec, 2, 0)), 310, 30); with trim

    /*
     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 STATE_NORMAL = 0;  // unique numbers
    final int STATE_FAIL   = 1;
    final int STATE_WON    = 2;
    final int STATE_SLOW   = 3;
    int state = STATE_NORMAL; // The variable state
    
    // other constants  
    final int MIN_DISTA = 3; 
    final int SLOW_TIME = 9*60+59;
    final PVector eggLocation = new PVector(38, 14);
    float x = 10, y = 380;
    
    int difference, sec, count_at_reset, min;
    
    boolean vIsDown=false;
    
    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 = 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 (vIsDown)
        difference = 0;
      else 
      difference = millis()/1000 - count_at_reset/1000;
    
      if (difference<SLOW_TIME) {
        sec = difference%60;
        min = (difference-sec)/60;
        fill(255);
        textSize(26);
        text(nfs(min, 1, 0)+":"+trim(nfs(sec, 2, 0)), 310, 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(196, 3, 45, 7);
      }
    }
    
    void drawForFail() {
      fill(255);
      textSize(140);
      text("FAIL", 60, 370);
      textSize(22);
      text("Press v to replay", 210, 70);
    }
    
    void drawForWon() {
      fill(255);
      textSize(30);
      text("YOU WON\n"
        +sec
        +"\nPress v to reset.", 80, 270);
    }
    
    void drawForSlow() {
      fill(255);
      textSize(130);
      text("SLOW", 40, 370);
      textSize(30);
      text("Try again", 210, 70);
    }
    
    // -----------------------------------------------
    // Tools
    
    void inputs() {
    
      if (!keyPressed) {
        return;
      }
    
      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 keyPressed() {
      if (key == 'v'||key == 'V') {
        reset();
        vIsDown=true;
      }
    }
    
    void keyReleased() {
      if (key == 'v'||key == 'V') {
        reset();
        vIsDown=false;
      }
    }
    
    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;
    }
    //
    
  • edited June 2018

    Run the code and press 'v' a few times and also look at the timer. You should see it occasionally increment to 1 right after it resets.

    The problem is still happening and I don't know how to fix this line of code.

    difference = millis()/1000 - count_at_reset/1000;

    There's also another problem. If you hold 'v' (that's like the sixth time I'm saying that), the timer will not increment. At first I thought it wouldn't be a problem, but then I found out that the player could still move allowing the player to complete the maze in 0:00. Could you help me with all of these?

    /*
     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 STATE_NORMAL = 0;  // unique numbers
    final int STATE_FAIL   = 1;
    final int STATE_WON    = 2;
    final int STATE_SLOW   = 3;
    int state = STATE_NORMAL; // The variable state
    
    // other constants  
    final int MIN_DISTA = 3; 
    final int SLOW_TIME = 9*60+59;
    final PVector eggLocation = new PVector(38, 14);
    float x = 10, y = 380;
    
    int difference, sec, count_at_reset, min;
    
    boolean vIsDown=false;
    
    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 = 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 (vIsDown)
        difference = 0;
      else
        difference = millis()/1000 - count_at_reset/1000;
    
      if (difference<SLOW_TIME) {
        sec = difference%60;
        min = (difference-sec)/60;
        fill(255);
        textSize(26);
        text(nfs(min, 1, 0)+":"+trim(nfs(sec, 2, 0)), 310, 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(196, 3, 45, 7);
      }
    }
    
    void drawForFail() {
      fill(255);
      textSize(140);
      text("FAIL", 60, 370);
      textSize(22);
      text("Press v to replay", 210, 70);
    }
    
    void drawForWon() {
      fill(255);
      textSize(30);
      text("YOU WON\n"
        +sec
        +"\nPress v to reset.", 80, 270);
    }
    
    void drawForSlow() {
      fill(255);
      textSize(130);
      text("SLOW", 40, 370);
      textSize(30);
      text("Try again", 210, 70);
    }
    
    // -----------------------------------------------
    // Tools
    
    void inputs() {
    
      if (!keyPressed) {
        return;
      }
    
      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 keyPressed() {
      if (key == 'v'||key == 'V') {
        reset();
        vIsDown=true;
      }
    }
    
    void keyReleased() {
      if (key == 'v'||key == 'V') {
        reset();
        vIsDown=false;
      }
    }
    
    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;
    }
    
  • Why don’t you try it yourself?

    I am not at home now

  • edited June 2018

    Run the code and press 'v' a few times and also look at the timer. You should see it occasionally increment to 1 right after it resets.

    this formula is better

     difference = ( millis() - count_at_reset ) /1000;
    

    (before we had millis()/1000 - count_at_reset/1000; and millis()/1000 was rounded to an int and therefore lead to this bug)

  • If you hold 'v' (that's like the sixth time I'm saying that), the timer will not increment.

    I think that's good. Normally user should hit v only briefly and not hold it down. When he holds it down, nothing happens until he releases.

    I found out that the player could still move allowing the player to complete the maze in 0:00.

    That is bad.

    I made a new version where inputs checks if v is down, using variable vIsDown

    /*
     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 STATE_NORMAL = 0;  // unique numbers
    final int STATE_FAIL   = 1;
    final int STATE_WON    = 2;
    final int STATE_SLOW   = 3;
    int state = STATE_NORMAL; // The variable state
    
    // other constants  
    final int MIN_DISTA = 3; 
    final int SLOW_TIME = 9*60+59;
    final PVector eggLocation = new PVector(38, 14);
    float x = 10, y = 380;
    
    int difference, sec, count_at_reset, min;
    
    boolean vIsDown=false;
    
    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 = 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 (vIsDown)
        difference = 0;
      else
        difference = ( millis() - count_at_reset ) /1000;
    
      if (difference<SLOW_TIME) {
        sec = difference%60;
        min = (difference-sec)/60;
        fill(255);
        textSize(26);
        text(nfs(min, 1, 0)+":"+trim(nfs(sec, 2, 0)), 310, 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(196, 3, 45, 7);
      }
    }
    
    void drawForFail() {
      fill(255);
      textSize(140);
      text("FAIL", 60, 370);
      textSize(22);
      text("Press v to replay", 210, 70);
    }
    
    void drawForWon() {
      fill(255);
      textSize(30);
      text("YOU WON\n"
        +sec
        +"\nPress v to reset.", 80, 270);
    }
    
    void drawForSlow() {
      fill(255);
      textSize(130);
      text("SLOW", 40, 370);
      textSize(30);
      text("Try again", 210, 70);
    }
    
    // -----------------------------------------------
    // Tools
    
    void inputs() {
    
      if (!keyPressed) {
        return;
      }
    
      if (vIsDown) {
        return;
      }
    
      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 keyPressed() {
      if (key == 'v'||key == 'V') {
        reset();
        vIsDown=true;
      }
    }
    
    void keyReleased() {
      if (key == 'v'||key == 'V') {
        reset();
        vIsDown=false;
      }
    }
    
    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;
    }
    //
    
  • edited June 2018

    Thank you, Chrisir. I also simplified these lines of code:

    if (!keyPressed) {
        return;
      }
    
      if (vIsDown) {
        return;
      }
    

    to just:

    if (!keyPressed || vIsDown) {
            return;
          }
    
  • Yes. Well spotted.

  • Hello again. Long time no see. I'm currently having this weird but annoying glitch. If I am holding in a direction (for example I hold 'q' to move up) and then I press in another direction (for example 's' to move down), the first input will still be active for quite a while (so I hold 'q' to move up then immediately let go of 'q' and start holding 's' to move down. I will still be moving up for about a half second).

    Could you help me with this?

    /*
     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 STATE_NORMAL = 0;  // unique numbers
    final int STATE_FAIL   = 1;
    final int STATE_WON    = 2;
    final int STATE_SLOW   = 3;
    int state = STATE_NORMAL; // The variable state
    
    // other constants  
    final int MIN_DISTA = 3; 
    final int SLOW_TIME = 9*60+59;
    final PVector eggLocation = new PVector(38, 14);
    float x = 10, y = 380;
    float l = 3375;
    
    int difference, sec, count_at_reset, min;
    
    boolean vIsDown = false;
    boolean e = false;
    
    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 = 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 (vIsDown) {
        difference = 0;
      } else {
        difference = ( millis() - count_at_reset ) /1000;
      }
    
      if (difference<SLOW_TIME) {
        sec = difference%60;
        min = (difference-sec)/60;
        fill(255);
        textSize(26);
        text(nfs(min, 1, 0)+":"+trim(nfs(sec, 2, 0)), 310, 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);
        sngdt();
        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(196, 3, 45, 7);
      }
    
      if (e) {
        fill(0);
        textSize(52);
        text("Bylb", l, 189);
      }
    
      if (sec > 10) {
        s();
      }
      l--;
    }
    
    void drawForFail() {
      fill(255);
      textSize(140);
      text("FAIL", 60, 370);
      textSize(22);
      text("Press v to replay", 210, 70);
    }
    
    void drawForWon() {
      fill(255);
      textSize(30);
      text("YOU WON\n"
        +sec
        +"\nPress v to reset.", 80, 270);
    }
    
    void drawForSlow() {
      fill(255);
      textSize(130);
      text("SLOW", 40, 370);
      textSize(30);
      text("Try again", 210, 70);
    }
    
    // -----------------------------------------------
    // Tools
    
    void inputs() {
    
      if (!keyPressed || vIsDown) {
        return;
      }
    
      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 keyPressed() {
      if (key == 'v'||key == 'V') {
        reset();
        vIsDown = true;
        l = 3375;
      }
    }
    
    void keyReleased() {
      if (key == 'v'||key == 'V') {
        reset();
        vIsDown = false;
      }
    }
    
    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;
    }
    
    void sngdt() {
      rect(25, 200, 90, 90);
    }
    
    void s() {
      e = true;
    }
    
  • Hard to say

    Look at function inputs

    connect the main ifs with else if so only one of them can be executed at a time

    Also at the end of the function say key=0;

    Note that there is a new forum

  • I did what you asked (I think) and it partially fixed it. When I put key=0;, it makes me go twice as slow. At first I thought, "Well this will be easy to fix; just make the square go 2 pixels when I press q/s/u/i instead of 1" but then it made the square get this jittery movement. Also, if I press "q" then immediately let go and press "s", then the same problem will happen, but in a different way. Instead of the movement getting applied longer than it should, this happens to the stop (so I press "q" then "s" like I mentioned before then I will stop for half a second instead of continue moving for half a second). Could you help me with this?

    /*
     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 STATE_NORMAL = 0;  // unique numbers
    final int STATE_FAIL   = 1;
    final int STATE_WON    = 2;
    final int STATE_SLOW   = 3;
    int state = STATE_NORMAL; // The variable state
    
    // other constants  
    final int MIN_DISTA = 3; 
    final int SLOW_TIME = 9*60+59;
    final PVector eggLocation = new PVector(38, 14);
    float x = 10, y = 380;
    float l = 3375;
    
    int difference, sec, count_at_reset, min;
    
    boolean vIsDown = false;
    boolean e = false;
    
    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 = 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 (vIsDown) {
        difference = 0;
      } else {
        difference = ( millis() - count_at_reset ) /1000;
      }
    
      if (difference<SLOW_TIME) {
        sec = difference%60;
        min = (difference-sec)/60;
        fill(255);
        textSize(26);
        text(nfs(min, 1, 0)+":"+trim(nfs(sec, 2, 0)), 310, 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);
        sngdt();
        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(196, 3, 45, 7);
      }
    
      if (e) {
        fill(0);
        textSize(52);
        text("Bylb", l, 189);
      }
    
      if (sec > 10) {
        s();
      }
      l--;
    }
    
    void drawForFail() {
      fill(255);
      textSize(140);
      text("FAIL", 60, 370);
      textSize(22);
      text("Press v to replay", 210, 70);
    }
    
    void drawForWon() {
      fill(255);
      textSize(30);
      text("YOU WON\n"
        +sec
        +"\nPress v to reset.", 80, 270);
    }
    
    void drawForSlow() {
      fill(255);
      textSize(130);
      text("SLOW", 40, 370);
      textSize(30);
      text("Try again", 210, 70);
    }
    
    // -----------------------------------------------
    // Tools
    
    void inputs() {
    
      if (!keyPressed || vIsDown) {
        return;
      }
    
      if (key == 'q'||key == 'Q') {
        y-=2;
      }
    
      else if (key == 's'||key == 'S') {
        y+=2;
      }
    
     else if (key == 'u'||key == 'U') {
        x-=2;
      }
    
     else if (key == 'i'||key == 'I') {
        x+=2;
      }
      key=0;
    }
    
    void keyPressed() {
      if (key == 'v'||key == 'V') {
        reset();
        vIsDown = true;
        l = 3375;
      }
    }
    
    void keyReleased() {
      if (key == 'v'||key == 'V') {
        reset();
        vIsDown = false;
      }
    }
    
    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;
    }
    
    void sngdt() {
      rect(25, 200, 90, 90);
    }
    
    void s() {
      e = true;
    }
    

    Note that there is a new forum

    What do you mean by this?

  • Hm

    Try deleting key =0

    and change

    if (!keyPressed || vIsDown) {

    To

    if (vIsDown) {

  • Still happening.

    /*
     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 STATE_NORMAL = 0;  // unique numbers
    final int STATE_FAIL   = 1;
    final int STATE_WON    = 2;
    final int STATE_SLOW   = 3;
    int state = STATE_NORMAL; // The variable state
    
    // other constants  
    final int MIN_DISTA = 3; 
    final int SLOW_TIME = 9*60+59;
    final PVector eggLocation = new PVector(38, 14);
    float x = 10, y = 380;
    float l = 3375;
    
    int difference, sec, count_at_reset, min;
    
    boolean vIsDown = false;
    boolean e = false;
    
    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 = 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 (vIsDown) {
        difference = 0;
      } else {
        difference = ( millis() - count_at_reset ) /1000;
      }
    
      if (difference<SLOW_TIME) {
        sec = difference%60;
        min = (difference-sec)/60;
        fill(255);
        textSize(26);
        text(nfs(min, 1, 0)+":"+trim(nfs(sec, 2, 0)), 310, 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);
        sngdt();
        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(196, 3, 45, 7);
      }
    
      if (e) {
        fill(0);
        textSize(52);
        text("Bylb", l, 189);
      }
    
      if (sec > 10) {
        s();
      }
      l--;
    }
    
    void drawForFail() {
      fill(255);
      textSize(140);
      text("FAIL", 60, 370);
      textSize(22);
      text("Press v to replay", 210, 70);
    }
    
    void drawForWon() {
      fill(255);
      textSize(30);
      text("YOU WON\n"
        +sec
        +"\nPress v to reset.", 80, 270);
    }
    
    void drawForSlow() {
      fill(255);
      textSize(130);
      text("SLOW", 40, 370);
      textSize(30);
      text("Try again", 210, 70);
    }
    
    // -----------------------------------------------
    // Tools
    
    void inputs() {
    
      if (!keyPressed) {
        return;
      }if (vIsDown) {
        return;
      }
    
      if (key == 'q'||key == 'Q') {
        y--;
      }
    
      else if (key == 's'||key == 'S') {
        y++;
      }
    
     else if (key == 'u'||key == 'U') {
        x--;
      }
    
     else if (key == 'i'||key == 'I') {
        x++;
      }
    }
    
    void keyPressed() {
      if (key == 'v'||key == 'V') {
        reset();
        vIsDown = true;
        l = 3375;
      }
    }
    
    void keyReleased() {
      if (key == 'v'||key == 'V') {
        reset();
        vIsDown = false;
      }
    }
    
    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;
    }
    
    void sngdt() {
      rect(25, 200, 90, 90);
    }
    
    void s() {
      e = true;
    }
    

    Also, I made a simple and quick sketch just to test this glitch and I found that it is literally came out of nowhere. I am not kidding. This is the code of that other sketch.

    int x = 200;
    int y = 200;
    
    void setup() {
      size(400, 400); 
      frameRate(60);
    }
    
    void draw() {
      background(0);
      rect(x, y, 20, 20);
      inputs();
    }
    
    void inputs() {
      if (!keyPressed) {
        return;
      }
      if (key == 'w') {
        y--;
      }
      if (key == 's') {
        y++;
      }
      if (key == 'a') {
        x--;
      }
      if (key == 'd') {
        x++;
      }
    }
    
  • The hard way is to use the function keyPressed instead inputs

    You can also use keypressed and keyreleased to work with boolean - often seen here in the forum

  • Now, what happens is the square will stop for a brief moment before moving again.

    int x = 200;
    int y = 200;
    
    void setup() {
      size(400, 400); 
      frameRate(60);
    }
    
    void draw() {
      background(0);
      rect(x, y, 20, 20);
    }
    
    void keyPressed() {
      if (!keyPressed) {
        return;
      }
      if (key == 'w') {
        y--;
      }
      if (key == 's') {
        y++;
      }
      if (key == 'a') {
        x--;
      }
      if (key == 'd') {
        x++;
      }
    }
    

    Also, just in case you don't understand what I mean, I'll give you a frame by frame timeline (note that I'm acting as if the numbers at the left is the frame count, not the code line number)

    W
    W
    W
    W
    No input
    No input
    S
    S
    S
    S
    

    I hope you understand.

  • Yes keep on trying

Sign In or Register to comment.