Timebonus doesn't reset

The time bonus doesn’t reset when I press ‘r’ to restart game how do I make sure that the timeBonus also resets with the points and amount of balls.

int screensizex, screensizey;
PImage img;
int mode = 0;
int INTRO = 0;
int NORMAL_PLAY = 1;
int GAME_OVER = 2;

int missClicks = 0;

float [] x;
float [] y;

float [] xDelta;
float [] yDelta;

int numberOfBalls = 15;
int numberOfBallsRemaining = numberOfBalls;
int points = 0;



void setup() {
  fullScreen();
  img = loadImage( "bubble.png");
  image(img, 0, 0);
  x = new float[numberOfBalls];
  y = new float[numberOfBalls];
  xDelta = new float[numberOfBalls];
  yDelta = new float[numberOfBalls];


  for (int i = 0; i < numberOfBalls; i++) {
    x[i] = random(200, 1060);
    y[i] = random(200, 1060);
    xDelta[i] = random(-3, 3);
    yDelta[i] = random(3, -3);
  }
}



void draw() {
  if (mode == INTRO) {
    DoIntroMode();
  } else if (mode == NORMAL_PLAY) {
    DoNormalPlay();
  } else if (mode == GAME_OVER) {
    DoGameOver();
  }
}


public void DoIntroMode() {  
  textSize(150);
  fill(0, 255, 255);
  text("BUBBLE TAP", 500, 540);

  textSize(50);
  text("Press spacebar to start", 700, 610);
  if (keyPressed) {
    if (key == ' ') mode = NORMAL_PLAY;
  }
}

public void DoNormalPlay() {
  background(135, 206, 235);
  fill(255, 0, 0);
  stroke(255, 0, 0);
  strokeWeight(2);

  for (int i = 0; i < numberOfBalls; i++) {
    ellipse(x[i], y[i], 40, 40);
    x[i] = x[i] + xDelta[i];
    y[i] = y[i] + yDelta[i];

    if ((x[i] > width - 20) || (x[i] < 20)) {
      xDelta[i] = -xDelta[i];
    }

    if ((y[i] > height - 20) || (y[i] < 20)) {
      yDelta[i] = -yDelta[i];
    }
    if (numberOfBallsRemaining == 0) {
      mode = GAME_OVER;
    }
  }

  textSize(32);
  fill(0);
  text("Points: " + points, 0, 30);
}

public void DoGameOver() {
  textSize(50);
  text("You got " + points + " points!!", 700, 350);
  textSize(300);
  fill(0, 255, 255);
  text("Game Over!", 170, 600);
  textSize(50);
  fill(0);
  text("Press r to play again", 700, 720);

  if (keyPressed) {
    if (key == 'r') {
      mode = NORMAL_PLAY;
      points = 0;
      numberOfBallsRemaining = numberOfBalls;



      for (int i = 0; i < numberOfBalls; i++) {
        x[i] = random(20, 1060);
        y[i] = random(20, 1060);
        xDelta[i] = random(-3, 3);
        yDelta[i] = random(3, -3);

        background(135, 206, 235);
        fill(255, 0, 0);
        stroke(255, 0, 0);
        strokeWeight(2);

        ellipse(x[i], y[i], 40, 40);
        x[i] = x[i] + xDelta[i];
        y[i] = y[i] + yDelta[i];

        if ((x[i] > width - 20) || (x[i] < 20)) {
          xDelta[i] = -xDelta[i];
        }

        if ((y[i] > height - 20) || (y[i] < 20)) {
          yDelta[i] = -yDelta[i];
        }
        if (numberOfBallsRemaining == 0) {
          mode = GAME_OVER;
        }
      }
    }
  }
}





    void mousePressed() {
      for (int i = numberOfBalls - 1; i >= 0; i--) {
        float dist = sqrt((x[i] - mouseX) * (x[i] - mouseX) + (y[i] - mouseY) * (y[i] - mouseY));
        if (dist <= 20) {
          x[i] = -100;
          y[i] = -100;
          int speedBonus = abs(int(xDelta[i] + xDelta[i]));
          int timeBonus = int((20000.0 / millis()) * 10);
          points = points + 10 + speedBonus + timeBonus;
          numberOfBallsRemaining--;
          break;
        }
      }
    }

Im still a beginner in programming and I got this from multiple turtorials :stuck_out_tongue:

The time bonus is set by millis, which in general it shouldn’t, because millis() calculates the milisecons since the sketch was started… so if you stay for an hour in the menu screen, it would still change to the timebonus. You can avoid this by adding a variable. timeStart, which you can set to millis() when you start the game (actual game, not menu screen or open the program) and just reset to timeStart = millis() when you reset. And then just do millis() minus timeStart, instead of just millis(). This way you get the time from when the game starts/restarts, instead of the time the sketch is executed.

Just change :

if (key=='r') { // in the doGameOver method
  timeStart = millis(); //add this line
}
//and in the mousePressed() method change this
int timeBonus = int((20000.0 / millis() - timeStart) * 10);

PImage img;
int mode = 0;
int INTRO = 0;
int NORMAL_PLAY = 1;
int GAME_OVER = 2;
int timeStart = millis();

float [] x;
float [] y;

float [] xDelta;
float [] yDelta;

int numberOfBalls = 15;
int numberOfBallsRemaining = numberOfBalls;
int points = 0;



void setup() {
  fullScreen();
  img = loadImage( "bubble.png");
  image(img, 0, 0);
  x = new float[numberOfBalls];
  y = new float[numberOfBalls];
  xDelta = new float[numberOfBalls];
  yDelta = new float[numberOfBalls];


  for (int i = 0; i < numberOfBalls; i++) {
    x[i] = random(200, 1060);
    y[i] = random(200, 1060);
    xDelta[i] = random(-3, 3);
    yDelta[i] = random(3, -3);
  }
}



void draw() {
  if (mode == INTRO) {
    DoIntroMode();
  } else if (mode == NORMAL_PLAY) {
    DoNormalPlay();
  } else if (mode == GAME_OVER) {
    DoGameOver();
  }
}


public void DoIntroMode() {  
  textSize(150);
  fill(0, 255, 255);
  text("BUBBLE TAP", 500, 540);

  textSize(50);
  text("Press spacebar to start", 700, 610);
  if (keyPressed) {
    if (key == ' ') mode = NORMAL_PLAY;
  }
}

public void DoNormalPlay() {
  background(135, 206, 235);
  fill(255, 0, 0);
  stroke(255, 0, 0);
  strokeWeight(2);

  for (int i = 0; i < numberOfBalls; i++) {
    ellipse(x[i], y[i], 40, 40);
    x[i] = x[i] + xDelta[i];
    y[i] = y[i] + yDelta[i];

    if ((x[i] > width - 20) || (x[i] < 20)) {
      xDelta[i] = -xDelta[i];
    }

    if ((y[i] > height - 20) || (y[i] < 20)) {
      yDelta[i] = -yDelta[i];
    }
    if (numberOfBallsRemaining == 0) {
      mode = GAME_OVER;
    }
  }

  textSize(32);
  fill(0);
  text("Points: " + points, 0, 30);
}

public void DoGameOver() {
  textSize(50);
  text("You got " + points + " points!!", 700, 350);
  textSize(300);
  fill(0, 255, 255);
  text("Game Over!", 170, 600);
  textSize(50);
  fill(0);
  text("Press r to play again", 700, 720);
  



  if (keyPressed) {
    if (key == 'r') {
      timeStart = millis();
      mode = NORMAL_PLAY;
      points = 0;
      numberOfBallsRemaining = numberOfBalls;



      for (int i = 0; i < numberOfBalls; i++) {
        x[i] = random(20, 1060);
        y[i] = random(20, 1060);
        xDelta[i] = random(-3, 3);
        yDelta[i] = random(3, -3);

        background(135, 206, 235);
        fill(255, 0, 0);
        stroke(255, 0, 0);
        strokeWeight(2);

        ellipse(x[i], y[i], 40, 40);
        x[i] = x[i] + xDelta[i];
        y[i] = y[i] + yDelta[i];

        if ((x[i] > width - 20) || (x[i] < 20)) {
          xDelta[i] = -xDelta[i];
        }

        if ((y[i] > height - 20) || (y[i] < 20)) {
          yDelta[i] = -yDelta[i];
        }
        if (numberOfBallsRemaining == 0) {
          mode = GAME_OVER;
        }
      }
    }
  }
}



void timeStart() {
  
}

void mousePressed() {
  for (int i = numberOfBalls - 1; i >= 0; i--) {
    float dist = sqrt((x[i] - mouseX) * (x[i] - mouseX) + (y[i] - mouseY) * (y[i] - mouseY));
    if (dist <= 20) {
      x[i] = -100;
      y[i] = -100;
      int speedBonus = abs(int(xDelta[i] + xDelta[i]));
      int timeBonus = int((20000.0 / millis() - timeStart) * 10);
      points = points + 10 + speedBonus + timeBonus;
      numberOfBallsRemaining--;
      break;
    }
  }
}

Is this correct because it’s still not working

Why did you add a method timeStart() ?

ye i deleted that one but it’s still not resetting, it’s going in the - even now


PImage img;
int mode = 0;
int INTRO = 0;
int NORMAL_PLAY = 1;
int GAME_OVER = 2;
int timeStart = millis();

float [] x;
float [] y;

float [] xDelta;
float [] yDelta;

int numberOfBalls = 15;
int numberOfBallsRemaining = numberOfBalls;
int points = 0;



void setup() {
  fullScreen();
  img = loadImage( "bubble.png");
  image(img, 0, 0);
  x = new float[numberOfBalls];
  y = new float[numberOfBalls];
  xDelta = new float[numberOfBalls];
  yDelta = new float[numberOfBalls];


  for (int i = 0; i < numberOfBalls; i++) {
    x[i] = random(200, 1060);
    y[i] = random(200, 1060);
    xDelta[i] = random(-3, 3);
    yDelta[i] = random(3, -3);
  }
}



void draw() {
  if (mode == INTRO) {
    DoIntroMode();
  } else if (mode == NORMAL_PLAY) {
    DoNormalPlay();
  } else if (mode == GAME_OVER) {
    DoGameOver();
  }
}


public void DoIntroMode() {  
  textSize(150);
  fill(0, 255, 255);
  text("BUBBLE TAP", 500, 540);

  textSize(50);
  text("Press spacebar to start", 700, 610);
  if (keyPressed) {
    if (key == ' ') mode = NORMAL_PLAY;
  }
}

public void DoNormalPlay() {
  background(135, 206, 235);
  fill(255, 0, 0);
  stroke(255, 0, 0);
  strokeWeight(2);

  for (int i = 0; i < numberOfBalls; i++) {
    ellipse(x[i], y[i], 40, 40);
    x[i] = x[i] + xDelta[i];
    y[i] = y[i] + yDelta[i];

    if ((x[i] > width - 20) || (x[i] < 20)) {
      xDelta[i] = -xDelta[i];
    }

    if ((y[i] > height - 20) || (y[i] < 20)) {
      yDelta[i] = -yDelta[i];
    }
    if (numberOfBallsRemaining == 0) {
      mode = GAME_OVER;
    }
  }

  textSize(32);
  fill(0);
  text("Points: " + points, 0, 30);
}

public void DoGameOver() {
  textSize(50);
  text("You got " + points + " points!!", 700, 350);
  textSize(300);
  fill(0, 255, 255);
  text("Game Over!", 170, 600);
  textSize(50);
  fill(0);
  text("Press r to play again", 700, 720);
  



  if (keyPressed) {
    if (key == 'r') {
      timeStart = millis();
      mode = NORMAL_PLAY;
      points = 0;
      numberOfBallsRemaining = numberOfBalls;



      for (int i = 0; i < numberOfBalls; i++) {
        x[i] = random(20, 1060);
        y[i] = random(20, 1060);
        xDelta[i] = random(-3, 3);
        yDelta[i] = random(3, -3);

        background(135, 206, 235);
        fill(255, 0, 0);
        stroke(255, 0, 0);
        strokeWeight(2);

        ellipse(x[i], y[i], 40, 40);
        x[i] = x[i] + xDelta[i];
        y[i] = y[i] + yDelta[i];

        if ((x[i] > width - 20) || (x[i] < 20)) {
          xDelta[i] = -xDelta[i];
        }

        if ((y[i] > height - 20) || (y[i] < 20)) {
          yDelta[i] = -yDelta[i];
        }
        if (numberOfBallsRemaining == 0) {
          mode = GAME_OVER;
        }
      }
    }
  }
}


void mousePressed() {
  for (int i = numberOfBalls - 1; i >= 0; i--) {
    float dist = sqrt((x[i] - mouseX) * (x[i] - mouseX) + (y[i] - mouseY) * (y[i] - mouseY));
    if (dist <= 20) {
      x[i] = -100;
      y[i] = -100;
      int speedBonus = abs(int(xDelta[i] + xDelta[i]));
      int timeBonus = int((20000.0 / millis() - timeStart) * 10);
      points = points + 10 + speedBonus + timeBonus;
      numberOfBallsRemaining--;
      break;
    }
  }
}

I though only the timebonus doesn’t reset… Ok, will look into it.

It’s because your code generates unreachable balls. If you look at it, you should generate 15 balls, but only around 12 are visible.

huh… for me all 15 balls are visible on my screen :confused:

K, that might have been my screen size that is not big enough… For me it works perfectly…

Also if you restart the game the points reset and they act normal, cause for me the points go far into the minus

Try adding this line text(numberOfBallsRemaining, 100, 100);
in your draw function to check if they really are all there…

Yup, 494 points and they reset to 0…

Don’t you get less points after you press r to restart the game?

sorry I don’t understand where to put it in draw (im still a beginner)

void draw() {
  numberOfBallsRemaining, 100, 100;
  if (mode == INTRO) {
    DoIntroMode();
  } else if (mode == NORMAL_PLAY) {
    DoNormalPlay();
  } else if (mode == GAME_OVER) {
    DoGameOver();
    
  }
}

No, exactly 0 Points. Try creating a new processing file, and just copying the code you have posted above. It works for me, even though i have to change the loop from 1060 to 500, because my screen is too small.

Right there, just that it is

text(numberOfBallsRemaining, 100, 100);

thanks for your help man :slight_smile:

So did it work? If so, was it because there weren’t all balls visible? Because i retried again, and just changing the 1060 to 500 and removing the img, because i don’t have the img file it just worked fine.

i dont know what worked TBH