Hello I’m stuck on my project I just need to end my game after the timer is 0 and display a screen that says game over with the score but the timer just keeps going to negative and wouldn’t stop. I would also like if anyone can help me with a replay button that resets the game after clicking it. Can anyone help me please?
My code is here:
PImage hammer;
PImage meerkat;
PImage smashed;
int x;
int y;
int x1;
int y1;
int x2;
int y2;
int screenWidth = 800, screenHeight = 800;
int lastTime;
int counter;
int counterLastTime;
int delayAmount;
int diameter = 120;
int score;
boolean codeCrushed = false;
boolean code1Crushed = false;
boolean code2Crushed = false;
boolean clickForScore = false;
//Create window
void setup()
{
size(1350,850);
//size(800, 600);
counter = 30;
counterLastTime = millis();
//resetTimer();
smooth();
noStroke();
noCursor();
score = 0;
hammer = loadImage ("hammer.png");
hammer.resize(0,120);
meerkat = loadImage ("meerkat.png");
meerkat.resize(0,120);
smashed = loadImage ("smashed.png");
smashed.resize(0,1);
}//end
void draw()
{
background(240, 203, 38);
stroke(0);
fill(206, 169, 5);
//First row of holes
ellipse(180,300,174,40);
ellipse(680,300,174,40);
ellipse(1180,300,174,40);
//Second row of holes
ellipse(180,500,174,40);
ellipse(680,500,174,40);
ellipse(1180,500,174,40);
//Third row of holes
ellipse(180,700,174,40);
ellipse(680,700,174,40);
ellipse(1180,700,174,40);
if (codeCrushed == true) {
image(smashed, x, y);
}
else {
image(meerkat, x, y);
}
if (code1Crushed == true) {
image(smashed, x1, y1);
}
else {
image(meerkat, x1, y1);
}
if (code2Crushed == true) {
image(smashed, x2, y2);
}
else {
image(meerkat, x2, y2);
}
//Hammer Time
pushMatrix();
if (mousePressed) {
rotate(-25);
}
image(hammer, mouseX-50, mouseY-120);
popMatrix();
//Score & Time
fill(0, 0, 0);
textSize(26);
text("Score: "+score, 800, 800);
text("Time: "+counter, 400, 800);
if ((mousePressed == true) && (dist(mouseX, mouseY, x, y) <= diameter)) {
codeCrushed = true;
}
else {
codeCrushed = false;
}
if ((mousePressed == true) && (dist(mouseX, mouseY, x1, y1) <= diameter)) {
code1Crushed = true;
}
else {
code1Crushed = false;
}
if ((mousePressed == true) && (dist(mouseX, mouseY, x2, y2) <= diameter)) {
code2Crushed = true;
}
else {
code2Crushed = false;
}
if (millis() - lastTime > delayAmount) {
int randomMole= int (random(1, 10));
if (randomMole == 1)
{
x = 140;
y = 175;
x1 = 1140;
y1 = 375;
x2 = 1140;
y2 = 175;
}
if (randomMole == 2)
{
x = 640;
y = 175;
x1 = 1140;
y1 = 575;
}
else if (randomMole == 3)
{
x = 1140;
y = 175;
x1 = 1140;
y1 = 375;
}
else if (randomMole == 4)
{
x = 140;
y = 375;
}
else if (randomMole == 5)
{
x = 640;
y = 375;
}
else if (randomMole == 6)
{
x = 1140;
y = 375;
}
else if (randomMole == 7)
{
x = 140;
y = 575;
x1 = 140;
y1 = 175;
x2 = 1140;
y2 = 575;
}
else if (randomMole == 8)
{
x = 640;
y = 575;
}
else if (randomMole == 9)
{
x = 1140;
y = 575;
}
else if (randomMole == 10)
{
x = 140;
y = 175;
x1 = 1140;
y1 = 375;
x2 = 1140;
y2 = 175;
}
resetTimer();
}//end of loop
noStroke();
fill(240, 203, 38);
rect(0,0,100,150);
if (millis() - counterLastTime > 1000) {
counter = counter - 1;
counterLastTime = millis();
}
if (counter == 0){
background(0);
cursor();
textSize(30);
text("Game Over", 590, 400);
text("Score = " + score, 598, 450);
}
}//end
void reset() {
score = 0;
counter = 30;
}
void resetTimer() {
lastTime = millis();
delayAmount = int(random(1, 0) * 1000);
}
void mouseClicked() {
clickForScore = true;
if ((clickForScore == true) && (codeCrushed == true)) {
score = score+ 1;
}
if ((clickForScore == true) && (code1Crushed == true)) {
score = score+ 1;
}
if ((clickForScore == true) && (code2Crushed == true)) {
score = score+ 1;
}
}