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