# A key that when clicked will increase the speed of the square

**URL:** <https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939>\
**Category:** Coding Questions\
**Created:** [January 7, 2020, 6:12pm UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939 "2020-01-07T18:12:16Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rango](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Rango](https://discourse.processing.org/u/Rango)\
**Post date:** [January 7, 2020, 6:12pm UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/1 "2020-01-07T18:12:16Z")

</div>

```auto

int x=20;
int y=650;
int w=20;
int h=20;

int c=1;
int d=20;

String a= "";
String b= "Warning: Wormhole detected ahead. Approach with caution.";

float rectX;
float rectY;

float rectA;
float rectB;

float rectC;
float rectD;

float rectE;
float rectF;

float rectG;
float rectH;

float rectWidth;
float rectHeight;

void setup () {
size(800,900);
  
rectX=100;
rectY=200;

rectWidth=30;
rectHeight=600;

rectA=250;
rectB=350;

rectC=350;
rectD=200;

rectE=420;
rectF=350;

rectG=510;
rectH=200;
}

void draw (){
smooth();
  background(0);
  noStroke();
  fill(255,0,0);
  rect(10,10,785,190);
  filter(BLUR, 6);
  stroke(0);
  fill(255,255,0);
  rect(10,10,780,180);
  
  
  fill(255);
rect(x,y,w,h);

 textSize(20);
fill(0);
text(a, 100,50);

fill(25,200,35);
rect(rectX,rectY,rectWidth,rectHeight);
rect(rectA,rectB,rectWidth,rectHeight);
rect(rectC,rectD,rectWidth,rectHeight);
rect(rectE,rectF,rectWidth,rectHeight);
rect(rectG,rectH,rectWidth,rectHeight);

if (x > rectX && x < rectX + rectWidth && y > rectY && y < rectY + rectHeight) {
  x=20;
  y=650;
  } else if (x > rectA && x < rectA + rectWidth && y > rectB && y < rectB + rectHeight) {
    x=20;
  y=650;
  } else if (x > rectC && x < rectC + rectWidth && y > rectD && y < rectD + rectHeight) {
    x=20;
  y=650;
  } else if (x > rectE && x < rectE + rectWidth && y > rectF && y < rectF + rectHeight) {
    x=20;
  y=650;
  } else if (x > rectG && x < rectG + rectWidth && y > rectH && y < rectH + rectHeight) {
    x=20;
  y=650;
  } 

}

void keyPressed() {
  

  if (key == CODED) {
    if (keyCode == UP) {
      y -= c;
    } else if (keyCode == DOWN) {
      y +=c;
    } else if (keyCode == LEFT) {
      x -= c;
    } else if (keyCode == RIGHT) {
      x += c;
    } else if (key == 'p') {   
     c=c+5; 
   
}
    }
      if(y<100){
      a=b;
      } else if(x>700){
       x=20;
       y=650;
      }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 8, 2020, 1:30am UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/2 "2020-01-08T01:30:53Z")

</div>

please format your code posting by pasting it into the

```auto
</> code button

```

of the editor header menu ( context name: Preformatted text )  
it looks like  
```  
type or paste code here  
```

also can use the ``` manually above and below your code.

thank you.

* * *

that is very difficult to read, but could be that your  
IF ELSE {{{{}}}}} structure  
put the key [p] inside the

```auto
if ( key == CODED ) {

```

so it can never be detected???

---

<div class="post-metadata">

**Author:** ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)\
**Post date:** [January 8, 2020, 12:37pm UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/3 "2020-01-08T12:37:11Z")

</div>

Please, really is needed to format your code with the \</\> button 🤯 🤯 🤯 🤯

Yep, as @kll said

> [@kll](#):
>
> if ( key == CODED ) {

put it out there:

```auto
if (key == 'p') 
  c+=5;
if (key == CODED) {
  if (keyCode == UP)
    y -= c;
  else if (keyCode == DOWN)
    y +=c;
  else if (keyCode == LEFT)
    x -= c;
  else if (keyCode == RIGHT)
    x += c; 
}

```

But I think you have a biigger problem with the frameRate, it runs so slow

```auto

  println("FrameRate: " + frameRate + " Speed: "+c + " X: " + x + " Y: " + y);

```

---

<div class="post-metadata">

**Author:** ![Rango](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Rango](https://discourse.processing.org/u/Rango)\
**Post date:** [January 8, 2020, 4:02pm UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/4 "2020-01-08T16:02:18Z")

</div>

Thank you for the key solution. I was wondering where I would place the framerate code and if there is a way where if I click ‘p’ it would prompt a stopwatch that would allow the speed to increase by 5 for only 5 seconds after clicking ‘p’.

---

<div class="post-metadata">

**Author:** ![Rango](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Rango](https://discourse.processing.org/u/Rango)\
**Post date:** [January 8, 2020, 4:02pm UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/5 "2020-01-08T16:02:57Z")

</div>

Thank you for the solution. I was wondering if there is a way where if I click ‘p’ it would prompt a stopwatch that would allow the speed to increase by 5 for only 5 seconds after clicking ‘p’.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 8, 2020, 4:21pm UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/6 "2020-01-08T16:21:32Z")

</div>

please start to learn a timer code in a extra little project first.

[https://processing.org/reference/millis\_.html](https://processing.org/reference/millis_.html)

```auto
long ts1, dt1= 5000; // ______________________ set 5 sec
boolean tenable1 = false;
int c =5;

void timer1(boolean enable) {
  if ( enable ) { 
       if ( millis() - ts1 > dt1 ) {
        println("action");
        tenable1 = false;
        c -=5;
      } 
  }
}

void setup() {
  size(100, 100);
  textSize(20);
  fill(0);
}

void draw() {
  background(200, 200, 0);
  timer1(tenable1);
  text("c = "+c,20,20);
}

void keyPressed() {
  if ( key == 'p' ) {
    tenable1 = true;
    ts1=millis();
    println("start");
    c +=5;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Rango](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Rango](https://discourse.processing.org/u/Rango)\
**Post date:** [January 8, 2020, 4:23pm UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/7 "2020-01-08T16:23:06Z")

</div>

Thank you for your time and support. When running this I already have an int c which equals 1 so` int c=5;` doesn’t work. Also, it doesn’t seem to display a timer. I understand the millis code, but I don’t see in here where I can place it in so that it appears on the window.

```auto
long ts1, dt1= 5000; // ______________________ set 5 sec
boolean tenable1 = false;
 int c =5;

void timer1(boolean enable) {
  if ( enable ) { 
       if ( millis() - ts1 > dt1 ) {
        println("action");
        tenable1 = false;
        c -=5;
      } 
  }
}

int x=20;
int y=650;
int w=20;
int h=20;

int c=1;
int d=20;

String a= "";
String b= "Warning: Wormhole detected ahead. Approach with caution.";

float rectX;
float rectY;

float rectA;
float rectB;

float rectC;
float rectD;

float rectE;
float rectF;

float rectG;
float rectH;

float rectWidth;
float rectHeight;

void setup () {
size(800,900);
  
rectX=100;
rectY=200;

rectWidth=30;
rectHeight=600;

rectA=250;
rectB=350;

rectC=350;
rectD=200;

rectE=420;
rectF=350;

rectG=510;
rectH=200;
}

void draw (){
  println("FrameRate: " + frameRate + " Speed: "+c + " X: " + x + " Y: " + y);
  
  smooth();
  background(0);
  noStroke();
  fill(255,0,0);
  rect(10,10,785,190);
  filter(BLUR, 6);
  stroke(0);
  fill(255,255,0);
  rect(10,10,780,180);
  
  
  fill(255);
rect(x,y,w,h);

 textSize(20);
fill(0);
text(a, 100,50);

fill(25,200,35);
rect(rectX,rectY,rectWidth,rectHeight);
rect(rectA,rectB,rectWidth,rectHeight);
rect(rectC,rectD,rectWidth,rectHeight);
rect(rectE,rectF,rectWidth,rectHeight);
rect(rectG,rectH,rectWidth,rectHeight);

if (x > rectX && x < rectX + rectWidth && y > rectY && y < rectY + rectHeight) {
  x=20;
  y=650;
  } else if (x > rectA && x < rectA + rectWidth && y > rectB && y < rectB + rectHeight) {
    x=20;
  y=650;
  } else if (x > rectC && x < rectC + rectWidth && y > rectD && y < rectD + rectHeight) {
    x=20;
  y=650;
  } else if (x > rectE && x < rectE + rectWidth && y > rectF && y < rectF + rectHeight) {
    x=20;
  y=650;
  } else if (x > rectG && x < rectG + rectWidth && y > rectH && y < rectH + rectHeight) {
    x=20;
  y=650;
 
} 

}

void keyPressed() {
  
if ( key == 'p' ) {
    tenable1 = true;
    ts1=millis();
    println("start");
    c +=5;
  }

  if (key == CODED) {
    if (keyCode == UP) {
      y -= c;
    } else if (keyCode == DOWN) {
      y +=c;
    } else if (keyCode == LEFT) {
      x -= c;
    } else if (keyCode == RIGHT) {
      x += c;
  
   
}
    }
      if(y<100){
      a=b;
      x=20;
      y=650;
      } else if(x>700){
       x=20;
       y=650;
      }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 9, 2020, 2:01am UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/8 "2020-01-09T02:01:55Z")

</div>

> [@Rango](#):
>
> Also, it doesn’t seem to display a timer.

you not say that you need the timer ( like a run down of remaining time ) in the display?  
[basic TIMER.pde ($1926921) · Snippets · GitLab](https://gitlab.com/snippets/1926921) ,

* * *

i can not find where you call the timer from draw() ??  
so it will never be executed?

```auto
long ts1, dt1= 5000; // ______________________ set 5 sec
boolean tenable1 = false;

void timer1(boolean enable) {
  if ( enable ) { 
    if ( millis() - ts1 > dt1 ) {
      println("T1 action");
      tenable1 = false;
      c =1;
    }
  }
}

int x=20, y=650, w=20, h=20;

int c=1;
int d=20;

String a= "",b= "Warning: Wormhole detected ahead. Approach with caution.";

float rectX=100; //__ 1
float rectY=200;
float rectA=250; //__ 2
float rectB=350;
float rectC=350; //__ 3
float rectD=200;
float rectE=420; //__ 4
float rectF=350;
float rectG=510; //__ 5
float rectH=200;
float rectWidth=30; //__ size
float rectHeight=600;

void setup () {
  size(800, 900);
}

void draw () {
  background(0);
  surface.setTitle("FrameRate: " + nf(frameRate, 0, 1) + " Speed: "+c + " X: " + x + " Y: " + y);
  timer1(tenable1);
  
  noStroke();
  fill(255, 0, 0);
  rect(10, 10, 785, 190);
  //filter(BLUR, 6);
  stroke(0);
  fill(255, 255, 0);
  rect(10, 10, 780, 180);

  fill(255); // _____________________?mover?
  rect(x, y, w, h);

  textSize(20);
  fill(0);
  text(a, 100, 50);

  fill(25, 200, 35);
  rect(rectX, rectY, rectWidth, rectHeight);
  rect(rectA, rectB, rectWidth, rectHeight);
  rect(rectC, rectD, rectWidth, rectHeight);
  rect(rectE, rectF, rectWidth, rectHeight);
  rect(rectG, rectH, rectWidth, rectHeight);
}

void keyPressed() {
  if ( key == 'p' ) {
    tenable1 = true;
    ts1=millis();
    println("T1 start");
    c = 5;
  }

  if (keyCode == UP) y -= c;
  else if (keyCode == DOWN) y += c;
  else if (keyCode == LEFT) x -= c;
  else if (keyCode == RIGHT) x += c;

  reset();
}

void reset() {
  if (y<100) a=b;
  if ( y<100 || x>700 ||
    (x > rectX && x < rectX + rectWidth && y > rectY && y < rectY + rectHeight) ||
    (x > rectA && x < rectA + rectWidth && y > rectB && y < rectB + rectHeight) ||
    (x > rectE && x < rectE + rectWidth && y > rectF && y < rectF + rectHeight) ||
    (x > rectG && x < rectG + rectWidth && y > rectH && y < rectH + rectHeight) ) {
    x=20;
    y=650;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)\
**Post date:** [January 9, 2020, 8:36am UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/9 "2020-01-09T08:36:03Z")

</div>

I didn’t know neither you wanted a timer to be displayed 😅 😅

Maybe this helps you:

```auto
boolean runing = false;
float time = 0,c = 1;

void setup(){
   textAlign(CENTER,CENTER);
}

void draw(){
  background(0);
  text("Speed: " + c,width/2,10);
  
  if(runing || time != 0){
    runing = false;
    time += 1/frameRate;
    text(5-time,width/2,height/2);
    
    if(time >= 5){
      c = 1;
      time = 0;
    }
    
  }   
}

void keyPressed() {
  
  if ( key == 'p' ) {
    runing = true;    
    c = 5;
  }
  
}

```

---

<div class="post-metadata">

**Author:** ![Rango](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Rango](https://discourse.processing.org/u/Rango)\
**Post date:** [January 9, 2020, 5:49pm UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/10 "2020-01-09T17:49:00Z")

</div>

Sorry for not being clear. Your solution worked out great thanks for the help.

---

<div class="post-metadata">

**Author:** ![Rango](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Rango](https://discourse.processing.org/u/Rango)\
**Post date:** [January 9, 2020, 5:49pm UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/11 "2020-01-09T17:49:35Z")

</div>

I apologize for not being clear, thanks for the solution.

---

<div class="post-metadata">

**Author:** ![Rango](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Rango](https://discourse.processing.org/u/Rango)\
**Post date:** [January 9, 2020, 6:04pm UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/12 "2020-01-09T18:04:52Z")

</div>

I just ran into a minor issue. The above code works when I launched it on a new file, it’s just that when I added it to my code it didn’t seem to appear.

```auto
boolean runing = false;
float time = 0,c = 1;

int x=20;
int y=650;
int w=20;
int h=20;

int d=20;

String a= "";
String b= "Warning: Wormhole detected ahead. Approach with caution.";

float rectX;
float rectY;

float rectA;
float rectB;

float rectC;
float rectD;

float rectE;
float rectF;

float rectG;
float rectH;

float rectWidth;
float rectHeight;

void setup(){
   size(800,900);
  textAlign(CENTER,CENTER);
rectX=100;
rectY=200;

rectWidth=30;
rectHeight=600;

rectA=250;
rectB=350;

rectC=350;
rectD=200;

rectE=420;
rectF=350;

rectG=510;
rectH=200;

}

void draw(){

 
  background(0);

  
  smooth();
  background(0);
  noStroke();
  fill(255,0,0);
  rect(10,10,785,190);
  filter(BLUR, 6);
  stroke(0);
  fill(255,255,0);
  rect(10,10,780,180);
  
  
  fill(255);
rect(x,y,w,h);

 textSize(20);
fill(0);
text(a, 100,50);

fill(25,200,35);
rect(rectX,rectY,rectWidth,rectHeight);
rect(rectA,rectB,rectWidth,rectHeight);
rect(rectC,rectD,rectWidth,rectHeight);
rect(rectE,rectF,rectWidth,rectHeight);
rect(rectG,rectH,rectWidth,rectHeight);

if (x > rectX && x < rectX + rectWidth && y > rectY && y < rectY + rectHeight) {
  x=20;
  y=650;
  } else if (x > rectA && x < rectA + rectWidth && y > rectB && y < rectB + rectHeight) {
    x=20;
  y=650;
  } else if (x > rectC && x < rectC + rectWidth && y > rectD && y < rectD + rectHeight) {
    x=20;
  y=650;
  } else if (x > rectE && x < rectE + rectWidth && y > rectF && y < rectF + rectHeight) {
    x=20;
  y=650;
  } else if (x > rectG && x < rectG + rectWidth && y > rectH && y < rectH + rectHeight) {
    x=20;
  y=650;

  
  if(runing || time != 0){
    runing = false;
    time += 1/frameRate;
    text(5-time,width/2,height/2);
    
    if(time >= 5){
      c = 1;
      time = 0;
    
    textSize(64);
    text("Speed: " + c,width/2,100);
}
    
  }   
}
}
void keyPressed() {
  
  if ( key == 'p' ) {
    runing = true;    
    c = 5;
  }
  if (key == CODED) {
    if (keyCode == UP) {
      y -= c;
    } else if (keyCode == DOWN) {
      y +=c;
    } else if (keyCode == LEFT) {
      x -= c;
    } else if (keyCode == RIGHT) {
      x += c;
  
   
}
    }
      if(y<100){
      a=b;
      x=20;
      y=650;
      } else if(x>700){
       x=20;
       y=650;
      }
}

```

---

<div class="post-metadata">

**Author:** ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)\
**Post date:** [January 10, 2020, 8:17am UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/13 "2020-01-10T08:17:29Z")

</div>

😮 Yep, be careful about when and where you display the text and the place of your curly brackets 🤔:

💡Try to format your code in a way that you can see where finish each bracket :

```auto
  } else if (x > rectG && x < rectG + rectWidth && y > rectH && y < rectH + rectHeight) {
    x=20;
  y=650;

  
  if(runing || time != 0){
    runing = false;
    time += 1/frameRate;
    text(5-time,width/2,height/2);
    
    if(time >= 5){
      c = 1;
      time = 0;
    
    textSize(64); 
    text("Speed: " + c,width/2,100); //when is this text appearing and how many time?
}
    
  } // those closed curly brackets are hard to see where they come
} // There is the problem, try to fix it 
}

```

If you solve this ⬆ ⬆ ⬆ you will get your text displayed 🤠.

Also, try to use spaces when can make code more readable 📖 ,

> **example:**
>
> ```auto
> }
> } // here is a good place to insert an enter, before a new function
> void keyPressed() {
>   
> if ( key == 'p' ) {
> runing = true
> ...
> 
> ```
> 
> into this:
> 
> ```auto
> }
> }
> 
> void keyPressed() {
>   
> if ( key == 'p' ) {
> runing = true
> ....
> 
> ```

Or don’t let excessive space 🕳:

> **Example**
>
> ```auto
> if ( key == 'p' ) {
> runing = true;    
> c = 5;
> } //Here you are starting a new big if statement, nice place for an enter too
> if (key == CODED) {
> if (keyCode == UP) {
> y -= c;
> } else if (keyCode == DOWN) {
> y +=c;
> } else if (keyCode == LEFT) {
> x -= c;
> } else if (keyCode == RIGHT) {
> x += c;
> //HERE ARE TOO MUCH SPACE FOR AN IF
>    
> }
> 
> } // And this bracket from where comes?, indent at same high
> 
> ```
> 
> I think this is better:
> 
> ```auto
> if ( key == 'p' ) {
> runing = true;    
> c = 5;
> }
> 
> if (key == CODED) {
> 
> if (keyCode == UP) {
> y -= c;
> } else if (keyCode == DOWN) {
> y +=c;
> } else if (keyCode == LEFT) {
> x -= c;
> } else if (keyCode == RIGHT) {
> x += c;
> }
> }
> 
> ```
> 
> Or even more comfortable:
> 
> ```auto
> if ( key == 'p' ) {
> runing = true;    
> c = 5;
> }
> 
> if (key == CODED) {
> 
> if (keyCode == UP) 
> y -= c;
> else if (keyCode == DOWN)
> y +=c;
> else if (keyCode == LEFT) 
> x -= c;
> else if (keyCode == RIGHT)
> x += c;
> }
> 
> ```

Well, the style of everyone may be different 👍, and is really not necessary to have the text formatted to make it works 😅, but for humans is easier to check what’s happening and fix the issues 🧐

Good luck 🍀, and if you try and don’t get it 🥴, feel free to ask again 😉

---

<div class="post-metadata">

**Author:** ![Rango](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Rango](https://discourse.processing.org/u/Rango)\
**Post date:** [January 10, 2020, 4:17pm UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/14 "2020-01-10T16:17:41Z")

</div>

Thanks for the solution. So I closed the else if statement at the very top. This allowed me to delete one curly bracket at the bottom. The timer shows up and works perfectly it’s now just the speed that doesn’t display.

```auto
  } else if (x > rectG && x < rectG + rectWidth && y > rectH && y < rectH + rectHeight) {
    x=20;
  y=650;
  }
  

  if(runing || time != 0){
    runing = false;
    time += 1/frameRate;
    text(5-time,600,400);
  

    if(time >= 5){
      c = 1;
      time = 0;
    
    

    text("Speed: " + c,600,350);
    }

}
}

```

---

<div class="post-metadata">

**Author:** ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)\
**Post date:** [January 13, 2020, 8:01am UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/15 "2020-01-13T08:01:53Z")

</div>

> [@Rango](#):
>
> if(time \>= 5){ c = 1; time = 0; text("Speed: " + c,600,350); }

If you analyze what’s happening here 🧐, you can see that if statement happens when time have reached 5 secs 💡, so it will display just for one frame (so hard to see 😑).

The question is, When you want to see the speed 🏃‍♂️ 💨?

Probably you want to see it when you press the key, isn’t it 🤨? Then, change your `text("Speed: " + c,600,350);` (what shows your text) when happens the condition you want (press the p key) 👍

---

<div class="post-metadata">

**Author:** ![Rango](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Rango](https://discourse.processing.org/u/Rango)\
**Post date:** [January 13, 2020, 5:40pm UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/16 "2020-01-13T17:40:03Z")

</div>

Of course. How did I miss that. Thank you for that solution. I appreciate you taking the time to walk me through this.😀

---

<div class="post-metadata">

**Author:** ![Rango](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Rango](https://discourse.processing.org/u/Rango)\
**Post date:** [January 15, 2020, 4:13pm UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/17 "2020-01-15T16:13:48Z")

</div>

Okay the solution makes sense and I have been playing around with it a bit, but i’m still a stuck on it.

---

<div class="post-metadata">

**Author:** ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)\
**Post date:** [January 16, 2020, 9:02am UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/18 "2020-01-16T09:02:15Z")

</div>

Okay, let’s see what’s happening in this part 🧐:

```auto
  if ( key == 'p' ) { // When you press the 'p' key
    runing = true; // A boolean variable (runing) is set to true
    c = 5; // And the speed (c) is set to 5
  }

```

Then, in the draw method… 🤔

```auto
  if(runing || time != 0){ // running is true, so this condition is true and the following code is executed:
    runing = false; // runing is set to false, so won't activate the previous condition infinitely
    time += 1/frameRate; // the timer starts (so the if statement will be executed even if runing is false, now is the timer who is maintaining this code executed)
    text(5-time,600,400); // Here we show the timer going to 0 in the screen
  
    if(time >= 5){ // When the time reaches 5 or more seconds, this condition is successful and is executed
      c = 1; // the speed (c) is set back to 1 ( I would call this variable speed or similar, instead of c :P)
      time = 0; //the timer is restarted, so won't execute the if(runing || time != 0) again
    text("Speed: " + c,600,350); // and here the speed is showed, but won't be showed more than one frame because the ifs aren't true
// This if statement is just executed once, because when time reaches 5 or more seconds, the time is set to 0, so the condition won't be true next frame
    }
  }

```

💡Well, seeing this, you should think when you want to show your speed,  
🤩¿always? 🤪¿When time reaches 5 seconds? 😍¿When speed is set to 5?

Depending on the answer, will be in one place or another:

- If you want it to be displayed always, just put it (the text) in the draw loop, outside any if statement, and over other things displayed (before rectangles) 😺

- In case of you want it when time reach 5 seconds, it is done, but if you want to be displayed every time once the time is 5, you can use a new if in the draw loop and set a boolean true when this condition happens, and put the text inside this new if 😽

- And to be displayed when speed is set to 5, just put the text together with the previous text `text(5-time,600,400);`, while the time is increasing, it will be displayed, but once the time reaches 5 and is set to 0, won’t be show till next time you start again the timer (pressing ‘p’) 😻

If you don’t find the solution with this, I will put the solution in next post, but try it before 😜

---

<div class="post-metadata">

**Author:** ![Rango](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Rango](https://discourse.processing.org/u/Rango)\
**Post date:** [January 16, 2020, 5:33pm UTC](https://discourse.processing.org/t/a-key-that-when-clicked-will-increase-the-speed-of-the-square/16939/19 "2020-01-16T17:33:15Z")

</div>

Yup got it. Thank for your help you have taught me a lot through this process, helping me dissect my code and analyze it. I wish you the best.
