Im trying to make a simple game for school, basically its a simple aim trainer. All it needs to do is draw dots, have them killed when you click them then add to your score, then when you click on the background instead of the dots your game is over. Now i have the problem of ending the game, whenever i click dots it runs the code inside the gameend() function, it SHOULD only do this when the dots are clicked. Is there any way to make sure the gameend() function both stays as a constant and only activates when the background is clicked
I've tried to use a mouseClicked function but it broke my code
let x;
let y;
let circle;
let dots = [];
let score = 0;
function setup() {
createCanvas(1080, 800);
xycircle();
}
function draw() {
background(100, 100, 255);
click();
scorer();
for (let dot of dots) {
ellipse(dot.x, dot.y, dot.circle, dot.circle)
}
};
function xycircle() {
for (i = 0; i < 25; i += 1) {
dots.push({
x: random(1080),
y: random(100, 800),
circle: random(25, 50)
})
};
};
function click() {
for (let dot of dots) {
if (dist(dot.x, dot.y, mouseX, mouseY) < dot.circle / 2 &&
mouseIsPressed) {
dots = dots.filter(dot1 => dot1 !== dot)
score++
if (dots.length === 0) {
xycircle();
}
} else if (dist(dot.x, dot.y, mouseX, mouseY) != dot.circle / 2 &&
mouseIsPressed) {
}
};
};
function scorer() {
fill(20, 75, 200);
rect(0, 0, 1080, 75);
fill(0, 0, 0);
text(score, 950, 50)
fill(255, 255, 255);
}
function gameend() {
background(255, 0, 0);
fill(0, 0, 0);
text("GAME OVER", 540, 400);
text("Your score was " + score, 540, 420);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.js"></script>