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


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;
      }
}

please format your code posting by pasting it into the

</> 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

if ( key == CODED ) {

so it can never be detected???

1 Like

Please, really is needed to format your code with the </> button :exploding_head: :exploding_head: :exploding_head: :exploding_head:

Yep, as @kll said

put it out there:

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


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

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’.

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’.

1 Like

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

https://processing.org/reference/millis_.html

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;
  }
}



1 Like

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.

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;
      }
}

you not say that you need the timer ( like a run down of remaining time ) in the display?
basic TIMER.pde ($1926921) · Snippets · GitLab ,


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

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;
  }
}

1 Like

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

Maybe this helps you:

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;
  }
  
}
2 Likes

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

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

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.

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;
      }
}

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

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

  }  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 :arrow_up: :arrow_up: :arrow_up: you will get your text displayed :cowboy_hat_face:.

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

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

into this:

}
}

void keyPressed() {
  
  if ( key == 'p' ) {
    runing = true
   ....

Or don’t let excessive space :hole::

Example
  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:

  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:

  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 :+1:, and is really not necessary to have the text formatted to make it works :sweat_smile:, but for humans is easier to check what’s happening and fix the issues :face_with_monocle:

Good luck :four_leaf_clover:, and if you try and don’t get it :woozy_face:, feel free to ask again :wink:

2 Likes

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.

  }  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);
    }



}
}
1 Like

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

The question is, When you want to see the speed :running_man: :dash:?

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

3 Likes

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

1 Like

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

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

  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… :thinking:

  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
    }
  }

:bulb:Well, seeing this, you should think when you want to show your speed,
:star_struck:¿always? :crazy_face:¿When time reaches 5 seconds? :heart_eyes:¿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) :smiley_cat:

  • 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 :kissing_cat:

  • 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’) :heart_eyes_cat:

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

3 Likes

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.