Restart using key pressed

//for the menu
int toggle =1;

int width=640;
int height=360;

void setup() {
size(1280, 720);
}
void draw() {

background(255);

noStroke();
fill(0);
float s=5.0;
for (int i=0; i<height; i++) {
for (int j=0; j<width; j++) {

  //noise is being used instead of random to get a more natural and harmonic succession
  
  //creation of the art
  float x=noise(s*(j+5*frameCount)/width, s*i/height);
  float y=noise(s*j/width, s*(i+3*frameCount)/height);
  float x1=j + 18*map(x, 0, 1, -0.6, 0.6);
  float y1=i + 18*map(y, 0, 1, -0.6, 0.6);
  float t= map(cos(0.007*frameCount), -1, 1, 0.01, 0.0005);  //0.02, 0.0005
  float theta= lerp(noise(t*x1, t*y1 ), noise(4*t*x1, 4*t*y1 ), 0.2);
  
  fill(map( cos(theta*0.04*(1+0.004*frameCount)/t), -1, 1, 0, 255));
  rect( j*2, i*2, 2, 2);
}

 if(toggle == 1){

fill(150,10);
rect(width+435,400,200,height);
fill(0);
textSize(20);
text(" R = Restart",1120,500);
text(" S = Snapshot",1120,550);
text(" H = Hide menu",1120,600);
text(" U = Show menu",1120,650);


fill(random(255),random(255),random(255));
textSize(40);
text(" Through art, everything is possible",width/2,100);

}
}
}
void keyPressed()
{
if(key==‘R’ || key == ‘r’)
{
keyPressed = true;
background(255);

}

I have a problem, every other thing is good but i don’t know how to restart the sketch using r as key. any help?

1 Like

it looks very nice! Congrats!

You had one misplaced } in draw so that the text was shown many many times. Corrected this.

I also wouldn’t recommend width, height as variable name since they are inbuilt variables. Corrected this.

I think R already works to restart. Otherwise, I guess, you need to replace frameCount with a new variable t that you increase and increase in manually. Then you can say t00; to reset

Please learn how to post code better here in the forum

//for the menu
int toggle = 1;

// bad naming! 
//int width1=640;
//int height1=360;

// -------------------------------------------------------------------------------

void setup() {
  size(1280, 720);
  background(255);
}

void draw() {
  background(255);

  noStroke();
  fill(0);
  float s=5.0;

  for (int i=0; i<height/2; i+=1) {
    for (int j=0; j<width/2; j+=1) {

      //noise is being used instead of random to get a more natural and harmonic succession
      //creation of the art
      float x=noise(s*(j+5*frameCount)/width, s*i/height); 
      float y=noise(s*j/width, s*(i+3*frameCount)/height); 
      float x1=j + 18*map(x, 0, 1, -0.6, 0.6); 
      float y1=i + 18*map(y, 0, 1, -0.6, 0.6); 
      float t= map(cos(0.007*frameCount), -1, 1, 0.01, 0.0005); //0.02, 0.0005
      float theta= lerp(noise(t*x1, t*y1 ), noise(4*t*x1, 4*t*y1 ), 0.2); 

      fill(map( cos(theta*0.04*(1+0.004*frameCount)/t), -1, 1, 0, 255)); 
      rect( j*2, i*2, 2, 2);
    }//for
  }//for 

  if (toggle == 1) {
    fill(150, 100); 
    rect(width-165, 400, 200, height); 
    fill(0); 
    textSize(20); 
    text(" R = Restart", 1120, 500); 
    text(" S = Snapshot", 1120, 550); 
    text(" H = Hide menu", 1120, 600); 
    text(" U = Show menu", 1120, 650); 

    fill(random(255), random(255), random(255)); 
    textSize(40); 
    textAlign(CENTER); 
    text(" Through art, everything is possible", width/2, 100); 
    textAlign(LEFT);
  }//if
}//func 

void keyPressed() {
  if (key=='R' || key == 'r') {
    // keyPressed = true;
    background(255);
  }

  if (key=='S' || key == 's')
  {
    saveFrame("line-######.png");
  }
  if (key=='H' || key == 'h')
  {
    toggle = 0;
  }
  if (key=='U' || key == 'u')
  {
    toggle = 1;
  }
}
//

1 Like

hello and thank you for helping out.

My problem is with keyPressed R, I want to restart the sketch once it’s pressed. in other word, when I press R on my keyboard, i want the sketch to start over from the beginning or restart and produce a different output.

Otherwise, I guess, you need to replace frameCount with a new variable t that you increase manually. Then you can say t=0; to reset

1 Like

Hi, change the seed value for the noise by using this instead

if (key=='R' || key == 'r') {
    // keyPressed = true;
    background(255);
    noiseSeed(int(random(1000))); //this will reset the background and change the value of the noise
  }

Hope that helps

1 Like