# Timebonus doesn't reset

**URL:** https://discourse.processing.org/t/timebonus-doesnt-reset/5319
**Category:** Coding Questions
**Created:** [November 7, 2018, 2:58pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319 "2018-11-07T14:58:09Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![kinglucas](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@kinglucas](https://discourse.processing.org/u/kinglucas)
#### Post date: [November 7, 2018, 2:58pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/1 "2018-11-07T14:58:09Z")

</div>

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.

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

```

---

<div class="post-metadata">

### Author: ![kinglucas](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@kinglucas](https://discourse.processing.org/u/kinglucas)
#### Post date: [November 7, 2018, 3:02pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/2 "2018-11-07T15:02:08Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 7, 2018, 3:35pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/3 "2018-11-07T15:35:55Z")

</div>

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 :

```auto
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);

```

---

<div class="post-metadata">

### Author: ![kinglucas](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@kinglucas](https://discourse.processing.org/u/kinglucas)
#### Post date: [November 7, 2018, 3:44pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/4 "2018-11-07T15:44:49Z")

</div>

```auto

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

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 7, 2018, 3:46pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/5 "2018-11-07T15:46:07Z")

</div>

Why did you add a method timeStart() ?

---

<div class="post-metadata">

### Author: ![kinglucas](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@kinglucas](https://discourse.processing.org/u/kinglucas)
#### Post date: [November 7, 2018, 3:47pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/6 "2018-11-07T15:47:40Z")

</div>

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

```auto

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

```

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 7, 2018, 3:50pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/7 "2018-11-07T15:50:34Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 7, 2018, 3:55pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/8 "2018-11-07T15:55:37Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kinglucas](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@kinglucas](https://discourse.processing.org/u/kinglucas)
#### Post date: [November 7, 2018, 3:56pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/9 "2018-11-07T15:56:46Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 7, 2018, 4:00pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/10 "2018-11-07T16:00:22Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kinglucas](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@kinglucas](https://discourse.processing.org/u/kinglucas)
#### Post date: [November 7, 2018, 4:01pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/11 "2018-11-07T16:01:02Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 7, 2018, 4:01pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/12 "2018-11-07T16:01:30Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 7, 2018, 4:02pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/13 "2018-11-07T16:02:33Z")

</div>

Yup, 494 points and they reset to 0…

---

<div class="post-metadata">

### Author: ![kinglucas](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@kinglucas](https://discourse.processing.org/u/kinglucas)
#### Post date: [November 7, 2018, 4:03pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/14 "2018-11-07T16:03:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kinglucas](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@kinglucas](https://discourse.processing.org/u/kinglucas)
#### Post date: [November 7, 2018, 4:03pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/15 "2018-11-07T16:03:53Z")

</div>

> [@Lexyth](#):
>
> Try adding this line ` text(numberOfBallsRemaining, 100, 100);`  
> in your draw function to check if they really are all there…

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

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

```

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 7, 2018, 4:04pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/16 "2018-11-07T16:04:45Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 7, 2018, 4:05pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/17 "2018-11-07T16:05:32Z")

</div>

Right there, just that it is

```auto
text(numberOfBallsRemaining, 100, 100);

```

---

<div class="post-metadata">

### Author: ![kinglucas](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@kinglucas](https://discourse.processing.org/u/kinglucas)
#### Post date: [November 7, 2018, 4:12pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/18 "2018-11-07T16:12:52Z")

</div>

thanks for your help man 🙂

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 7, 2018, 4:14pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/19 "2018-11-07T16:14:10Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kinglucas](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@kinglucas](https://discourse.processing.org/u/kinglucas)
#### Post date: [November 7, 2018, 4:17pm UTC](https://discourse.processing.org/t/timebonus-doesnt-reset/5319/20 "2018-11-07T16:17:58Z")

</div>

i dont know what worked TBH
