How can I restart the program with keypress

I’ll try and change it in a couple hours

Images should be on the github now

1 Like

the timer still seems to no work

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).
1 Like

try this

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

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.

Have you tried the code i posted?

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:

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

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

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

Also changed the text just to see time with one digit

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

1 Like

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

Which lines do I replace with it?

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?

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

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

Got it, I’ll test it out in a couple hours

yes, but maybe the visualization is not as you want (no decimals) :sweat_smile::sweat_smile:

It’s okay as long as the timer resets it is okay.
Do the red fireballs spawn when the timer reset?

Ohh, yes, the fireballs spawns and have just tried once again and when I win, the win screen pop up, but then, to start a new game I couldn’t clicking enter, had to click “q”.

Oh yes, that is a mistake on my part. Thank you very much for letting me know, I test it out in a bit

IT WORKS. Thank you so much.

1 Like

One simple way of approaching restart-timing is to rerun your setup each time you restart, and maintain a global variable with the last time you restarted. This lets you compute either the total run time (millis) or the current runtime (restart-millis).

Processing always runs setup on frame 0 – so one way to rerun it (and reset frameCount at the same time) is to set the frameCount to -1.

/**
 * Restart Frame 0
 * 2019-11 Processing 3.4 
 * Press key to restart -- setup() runs again on the following frame.
 * Sketch demonstrates computing a custom millis based on last restart.
 */

int start;
color bgcolor;
int level = 0;

void setup() {
  start = millis();
  bgcolor = color(random(255), random(255), random(255));
  level = level + 1;
}

int reMillis() {
  return millis()-start;
}

void draw() {
  background(bgcolor);
  text(level, 20, 20);
  text(frameCount, 20, 40);
  text(reMillis(), 20, 60);
  text(millis(), 20, 85);
}

void keyReleased() {
  frameCount = -1;
}

Note that this behavior isn’t officially documented in the reference – but it has been a part of Processing for a very long time.

1 Like