# How can I restart the program with keypress

**URL:** https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446
**Category:** Coding Questions
**Created:** [November 13, 2019, 4:59am UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446 "2019-11-13T04:59:57Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Kitty](https://avatars.discourse-cdn.com/v4/letter/k/278dde/32.png) [@Kitty](https://discourse.processing.org/u/Kitty)
#### Post date: [November 13, 2019, 4:59am UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/1 "2019-11-13T04:59:57Z")

</div>

I want the key “Q” to restart the program, but the reset function isn’t working.

> <https://github.com/kitkat09-crypto/01/blob/master/Game%202>

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [November 13, 2019, 5:08am UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/2 "2019-11-13T05:08:14Z")

</div>

Check out the reset function in my code, and the mousepressed function should give you an idea of how to reset your sketch

[https://www.openprocessing.org/sketch/783497](https://www.openprocessing.org/sketch/783497)

---

<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: [November 13, 2019, 5:24am UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/3 "2019-11-13T05:24:04Z")

</div>

your basic idea works here:

```auto
float ang=0;

void setup() {
  println("click on canvas and press key [s] stop or [q] run");
}

void draw() {
  background(200, 200, 0);
  translate(width/2,height/2);
  rotate(ang);
  ang +=0.01;
  line(10, 10, 20, 20);
}

void keyPressed() {
  if ( key == 's' ) noLoop();  
  if ( key == 'q' ) loop();
}

```

but sorry, will not check all that code…

also working with `noLoop();` at all is a unusual concept.

---

<div class="post-metadata">

### Author: ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)
#### Post date: [November 13, 2019, 7:55am UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/4 "2019-11-13T07:55:10Z")

</div>

**edit**  
Apologies, seems my suggestion was already implemented. Thanks for pointing it out @kll

---

<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: [November 13, 2019, 8:20am UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/5 "2019-11-13T08:20:38Z")

</div>

pls see his line 540 where this actually is done that way,

i try:

```auto
float ang=-PI/2;

void setup() {
  println("click on canvas and press key [s] stop or [q] run or [r] reset");
}

void draw() {
  background(200, 200, 0);
  translate(width/2, height/2);
  rotate(ang+=0.01);
  line(10, 0, 40, 0);
}

void keyPressed() {
  if ( key == 's' ) noLoop(); 
  if ( key == 'q' ) loop();
  if ( key == 'r' ) reset();
}

void reset() {
  println("reset");
  ang = -PI/2;
  loop();
}

```

and my short form also works that way  
so must be an other logic problem ???

@Kitty, you are aware that we are limited to read your code  
as you not store the whole project ( 12 pictures ) at github  
we can not run it.

---

<div class="post-metadata">

### Author: ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)
#### Post date: [November 13, 2019, 8:31am UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/6 "2019-11-13T08:31:06Z")

</div>

Inside `void reset()` you seem to be redeclaring global variables:

```auto
// line 560 and upwards:
int rad = 60;
float xpos, ypos;
float xspeed = 2.8;
// ... rest of the code

```

Instead of redeclaring them, only reinitialise them. Does that solve your issue?

---

<div class="post-metadata">

### Author: ![Kitty](https://avatars.discourse-cdn.com/v4/letter/k/278dde/32.png) [@Kitty](https://discourse.processing.org/u/Kitty)
#### Post date: [November 13, 2019, 1:12pm UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/7 "2019-11-13T13:12:50Z")

</div>

My apologizes, I completely forgot that I’d need to do that. I’ll upload them as soon as possible. Maybe in about 3-4 hours. Sorry for delays.

---

<div class="post-metadata">

### Author: ![Kitty](https://avatars.discourse-cdn.com/v4/letter/k/278dde/32.png) [@Kitty](https://discourse.processing.org/u/Kitty)
#### Post date: [November 13, 2019, 1:13pm UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/8 "2019-11-13T13:13:41Z")

</div>

I’ll try and change it in a couple hours

---

<div class="post-metadata">

### Author: ![Kitty](https://avatars.discourse-cdn.com/v4/letter/k/278dde/32.png) [@Kitty](https://discourse.processing.org/u/Kitty)
#### Post date: [November 13, 2019, 4:39pm UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/9 "2019-11-13T16:39:50Z")

</div>

Images should be on the github now

---

<div class="post-metadata">

### Author: ![Kitty](https://avatars.discourse-cdn.com/v4/letter/k/278dde/32.png) [@Kitty](https://discourse.processing.org/u/Kitty)
#### Post date: [November 13, 2019, 5:30pm UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/10 "2019-11-13T17:30:43Z")

</div>

the timer still seems to no work

---

<div class="post-metadata">

### Author: ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)
#### Post date: [November 13, 2019, 5:45pm UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/11 "2019-11-13T17:45:56Z")

</div>

So the reset function works now, but only the timer doesn’t?

I think it wouldn’t hurt to take another look at your code and clean it. It’s filled with points of improvements which all might influence the expected outcome. Some examples:

- Inside `void reset()` I still see variables getting declared that are already global variables;
- Inside `void reset()` the same variables get appointed new values several times;
- These two issues are happening elsewhere in your sketch as well (such as redeclaring `float timer` even though it’s a global variable).

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [November 13, 2019, 5:49pm UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/12 "2019-11-13T17:49:24Z")

</div>

try this

```auto
PImage im[] = new PImage[8];
String imFile[] = {"Start Screen.jpg", "Game Over.jpg", "Introduction.jpg", "Heart.jpg", "Fireball.png", "Fireball1.png", "Fireball2.png", "Win.jpg"};
long t = 0;
int toggle = 0;

void reset(){
  if(toggle==1){
    preset();
    initialise();
  }
}

void preset(){
  t = millis();
  frameRate(1000);
  ellipseMode(RADIUS);
    xpos = width/2;
    ypos = height/2;
    
     for (int i = 0; i < 4; i = i +1) {
       im[0] = loadImage(imFile[0]);
  mouseY = height - 135;
  }
  toggle = 0;
}
float timer = (millis() + t)/1000;
  
int rad = 60;
float xpos, ypos;
float xspeed = 2.8;
float yspeed = 2.2;
int xdirection = 1;
int ydirection = 1;

float aY = 0;
int aX = 15;
float aV = 0;
float aA = 0.4;

float bY = 0;
int bX = 15;
float bV = 0;
float bA = 0.5;

float cY = 0;
int cX = 15;
float cV = 0;
float cA = 0.5;

float dY = 0;
int dX = 15;
float dV = 0;
float dA = 0.6;

float eY = 0;
int eX = 15;
float eV = 0;
float eA = 0.1;

float fY = 0;
int fX = 15;
float fV = 0;
float fA = 0.2;

float gY = 0;
int gX = 15;
float gV = 0;
float gA = 0.3;

float hY = 0;
int hX = 15;
float hV = 0;
float hA = 0.4;

float iY = 0;
int iX = 15;
float iV = 0;
float iA = 0.5;

float jY = 0;
int jX = 15;
float jV = 0;
float jA = 0.6;

void initialise(){
 timer = (millis() + t)/1000;
  
 rad = 60;
 xspeed = 2.8;
 yspeed = 2.2;
 xdirection = 1;
 ydirection = 1;

 aY = 0;
 aX = 15;
 aV = 0;
aA = 0.4;
 bY = 0;
 bX = 15;
 bV = 0;
 bA = 0.5;

 cY = 0;
 cX = 15;
 cV = 0;
 cA = 0.5;

 dY = 0;
 dX = 15;
 dV = 0;
 dA = 0.6;

 eY = 0;
 eX = 15;
 eV = 0;
 eA = 0.1;

 fY = 0;
 fX = 15;
 fV = 0;
 fA = 0.2;

 gY = 0;
 gX = 15;
 gV = 0;
 gA = 0.3;

 hY = 0;
 hX = 15;
 hV = 0;
 hA = 0.4;

 iY = 0;
 iX = 15;
 iV = 0;
 iA = 0.5;

 jY = 0;
 jX = 15;
 jV = 0;
 jA = 0.6;
}

boolean isIntroScreen = true;

void setup(){
  size(1000,660);
  preset();
}

void draw(){
  reset();
  if (isIntroScreen)
  {
    background(0);
    image(im[0],0,0,width,height);
  }
  else{
    background(0);
  rect(mouseX,mouseY,1,1);
  im[3]=loadImage(imFile[3]);
 {
  image(im[3],mouseX,mouseY,25,25);
 }
 if(timer < 20){
 xpos = xpos + (xspeed * xdirection);
 ypos = ypos + (yspeed * ydirection);
 if (ypos > height-rad || ypos < rad){
   xdirection *= -1;
 }
 if (ypos > height-rad || xpos < rad){
   ydirection *= -1;
 }
 }
 ellipse(xpos,ypos,rad,rad);
 
   float timer = (millis() + t)/1000;
  fill(255);
  textSize(20);
  text("Time: " + (0 + timer), 10, 20);
 if (isIntroScreen) {noLoop();}
 
 if(timer < 50){
  im[4]=loadImage(imFile[4]);
  image(im[4],aX,aY,100,100);
  aV = aV + aA;
  aY = aY + aV;
  if (aY > height) {
    aY = 15;
    aX = int(random(width - 0));
    aV = 0;
    }
 }
 if(timer < 50){
im[5]=loadImage(imFile[5]);
  image(im[5],bX,bY,50,50);
  bV = bV + bA;
  bY = bY + bV;
  if (bY > height) {
    bY = 15;
    bX = int(random(width - 0));
    bV = 0;
  }
 }
 if(timer < 50){
   fill(255);
 ellipse(cX,cY,50,50);
  cV = cV + cA;
  cY = cY + cV;
  if (cY > height) {
    cY = 15;
    cX = int(random(width - 0));
    cV = 0;
  }
 }
 if(timer < 50){
   im[6]=loadImage(imFile[6]);
  image(im[6],dX,dY,25,25);
  dV = dV + dA;
  dY = dY + dV;
  if (dY > height) {
    dY = 15;
    dX = int(random(width - 0));
    dV = 0;
  }
 }
  if (timer > 50){
  fill(255);
  rect (eX,eY,10,10);
  eV = eV + eA;
  eY = eY + eV;
  if (eY > height) {
    eY = 15;
    eX = int(random(width - 0));
    eV = 0;
  }
  }
   if (timer > 50){
  fill(255);
  rect (fX,fY,10,10);
  fV = fV + fA;
  fY = fY + fV;
  if (fY > height) {
    fY = 15;
    fX = int(random(width - 0));
    fV = 0;
  }
   }
   if (timer > 50){
  fill(255);
  rect (gX,gY,10,10);
  gV = gV + gA;
  gY = gY + gV;
  if (gY > height) {
    gY = 15;
    gX = int(random(width - 0));
    gV = 0;
  }
   }
    if (timer > 50){
  fill(255);
  rect (hX,hY,10,10);
  hV = hV + hA;
  hY = hY + hV;
  if (hY > height) {
    hY = 15;
    hX = int(random(width - 0));
    hV = 0;
  }
   }
       if (timer > 50){
  fill(255);
  rect (iX,iY,10,10);
  iV = iV + iA;
  iY = iY + iV;
  if (iY > height) {
    iY = 15;
    iX = int(random(width - 0));
    iV = 0;
  }
   }
       if (timer > 50){
  fill(255);
  rect (jX,jY,10,10);
  jV = jV + jA;
  jY = jY + jV;
  if (jY > height) {
    jY = 15;
    jX = int(random(width - 0));
    jV = 0;
  }
   }

  fill(255);
  if (aY + 50 > mouseY && aY < mouseY + 135) {
    if (aX + 40 > mouseX && aX < mouseX + 128) {
       im[1] = loadImage(imFile[1]);
       {
       image(im[1],0,0,width,height);
       noLoop();
       }
      }
    }
      fill(255);
  if (bY + 50 > mouseY && bY < mouseY + 135) {
    if (bX + 40 > mouseX && bX < mouseX + 128) {
       im[1] = loadImage(imFile[1]);
       {
       image(im[1],0,0,width,height);
       noLoop();
       }
      }
    }
      fill(255);
  if (cY + 50 > mouseY && cY < mouseY + 135) {
    if (cX + 40 > mouseX && cX < mouseX + 128) {
       im[1] = loadImage(imFile[1]);
       {
       image(im[1],0,0,width,height);
       noLoop();
       }
      }
    }
      fill(255);
  if (dY + 50 > mouseY && dY < mouseY + 135) {
    if (dX + 40 > mouseX && dX < mouseX + 128) {
       im[1] = loadImage(imFile[1]);
       {
       image(im[1],0,0,width,height);
       noLoop();
       }
      }
    }
          fill(255);
  if (eY + 50 > mouseY && eY < mouseY + 135) {
    if (eX + 40 > mouseX && eX < mouseX + 128) {
       im[1] = loadImage(imFile[1]);
       {
       image(im[1],0,0,width,height);
       noLoop();
       }
      }
    }
          fill(255);
  if (fY + 50 > mouseY && fY < mouseY + 135) {
    if (fX + 40 > mouseX && fX < mouseX + 128) {
       im[1] = loadImage(imFile[1]);
       {
       image(im[1],0,0,width,height);
       noLoop();
       }
      }
    }
          fill(255);
  if (gY + 50 > mouseY && gY < mouseY + 135) {
    if (gX + 40 > mouseX && gX < mouseX + 128) {
       im[1] = loadImage(imFile[1]);
       {
       image(im[1],0,0,width,height);
       noLoop();
       }
      }
    }
          fill(255);
  if (hY + 50 > mouseY && hY < mouseY + 135) {
    if (hX + 40 > mouseX && hX < mouseX + 128) {
       im[1] = loadImage(imFile[1]);
       {
       image(im[1],0,0,width,height);
       noLoop();
       }
      }
    }
          fill(255);
  if (iY + 50 > mouseY && iY < mouseY + 135) {
    if (iX + 40 > mouseX && iX < mouseX + 128) {
       im[1] = loadImage(imFile[1]);
       {
       image(im[1],0,0,width,height);
       noLoop();
       }
      }
    }
          fill(255);
  if (jY + 50 > mouseY && jY < mouseY + 135) {
    if (jX + 40 > mouseX && jX < mouseX + 128) {
       im[1] = loadImage(imFile[1]);
       {
       image(im[1],0,0,width,height);
       noLoop();
       }
      }
    }
        fill(0,0,255);
  if (ypos + 50 > mouseY && ypos < mouseY + 135) {
    if (xpos + 40 > mouseX && xpos < mouseX + 128) {
       im[1] = loadImage(imFile[1]);
       {
       image(im[1],0,0,width,height);
       noLoop();
       }
      }
    }
    if (timer > 300){
      im[7] = loadImage(imFile[7]);
      image(im[7],0,0,width,height);
    }
  }
}

void keyPressed() {
  if (keyCode == ENTER&& isIntroScreen){
    for (int i = 0; i < 4; i = i +1) {
    im[2] = loadImage(imFile[2]); 
    image(im[2],0,0,width,height);
    noLoop();
    }
  }
 else if (keyCode == TAB && isIntroScreen){
  background(0);
  fill(255);
  noStroke();
  
  isIntroScreen = false;
 }
  float timer = (millis() + t)/1000;
  fill(255);
  textSize(20);
  text("Time: " + (0 + timer), 10, 20);
 if (isIntroScreen) {noLoop();}
 else {loop();}
 
 if (key == 'q' || key == 'Q'){
   toggle++;
 }
 
 if(toggle==2){
   toggle = 0;
 }
}

```

---

<div class="post-metadata">

### Author: ![Kitty](https://avatars.discourse-cdn.com/v4/letter/k/278dde/32.png) [@Kitty](https://discourse.processing.org/u/Kitty)
#### Post date: [November 14, 2019, 1:10am UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/13 "2019-11-14T01:10:48Z")

</div>

Here is the updated code, the timer still doesn’t reset.  
Another problem is that the win screen when time reaches 30 it doesn’t work anymore.

> <https://github.com/kitkat09-crypto/01/blob/master/Game%203>

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [November 14, 2019, 8:13am UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/14 "2019-11-14T08:13:18Z")

</div>

Have you tried the code i posted?

---

<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: [November 14, 2019, 8:36am UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/15 "2019-11-14T08:36:38Z")

</div>

Well just taking a fast look, the timer can’t reset because in the line 154 (if I’m not mistaken) you had something like:

```auto
float timer = (millis() + t)/1000;

```

That takes the time since the beginning of the program, using this, the timer resets:

```auto
   timer += 1/frameRate; //float timer = (millis() + t)/1000;

```

Also changed the text just to see time with one digit

```auto
   text("Time: " + (0 + (int)timer), 10, 20);

```

And trying a little bit the game, I’m not totally sure, but when I reached the 34 secs without die, no more obstacles appeared 🤔🤔

---

<div class="post-metadata">

### Author: ![Kitty](https://avatars.discourse-cdn.com/v4/letter/k/278dde/32.png) [@Kitty](https://discourse.processing.org/u/Kitty)
#### Post date: [November 14, 2019, 12:30pm UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/16 "2019-11-14T12:30:20Z")

</div>

The goal of the game is to survive 30 seconds.  
But after 30 seconds the win screen won’t load. So yeah, that’s one of my problems

---

<div class="post-metadata">

### Author: ![Kitty](https://avatars.discourse-cdn.com/v4/letter/k/278dde/32.png) [@Kitty](https://discourse.processing.org/u/Kitty)
#### Post date: [November 14, 2019, 12:31pm UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/17 "2019-11-14T12:31:08Z")

</div>

Which lines do I replace with 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: [November 14, 2019, 12:37pm UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/18 "2019-11-14T12:37:18Z")

</div>

Okay, I saw the win screen isn’t implemented, but thought the cause of the obstacles stopped was because it was being restarted.

Its the time okay now?

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [November 14, 2019, 12:51pm UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/19 "2019-11-14T12:51:53Z")

</div>

Remove the line from the preset function and move it outside of the function and remove the line which reinitializes it.

---

<div class="post-metadata">

### Author: ![Kitty](https://avatars.discourse-cdn.com/v4/letter/k/278dde/32.png) [@Kitty](https://discourse.processing.org/u/Kitty)
#### Post date: [November 14, 2019, 1:06pm UTC](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446/20 "2019-11-14T13:06:07Z")

</div>

I’m not sure, I’ll have to test it out at a later time.  
It runs fine when changed on your side, correct?

[Next page](https://discourse.processing.org/t/how-can-i-restart-the-program-with-keypress/15446.md?page=2)
