let backgroundImg, playerImg;
let appleImg, bananaImg, broccoliImg, carrotImg, strawberryImg, saladImg;
let burgerImg, candyImg, colaImg, pizzaImg;
backgroundImg = loadImage("supermarket.jpg");
playerImg = loadImage("boy.png");
appleImg = loadImage("apple.png");
bananaImg = loadImage("banana.png");
broccoliImg = loadImage("broccoli.png");
carrotImg = loadImage("carrot.png");
strawberryImg = loadImage("strawberry.png");
saladImg = loadImage("salad.png");
burgerImg = loadImage("burger.png");
candyImg = loadImage("candy.png");
colaImg = loadImage("cola.png");
pizzaImg = loadImage("pizza.png");
healthyFoods = [appleImg, bananaImg, broccoliImg, carrotImg, strawberryImg, saladImg];
unhealthyFoods = [burgerImg, candyImg, colaImg, pizzaImg];
background(backgroundImg);
textAlign(CENTER, CENTER);
text("Game Over!", width / 2, height / 2);
text("Score: " + score, width / 2, height / 2 + 40);
text("Press R to Restart", width / 2, height / 2 + 70);
for (let food of foods) {
if (food.type === "healthy") {
text("Score: " + score, 10, 20);
if (key === 'R' || key === 'r') {
for (let i = 0; i < 6; i++) {
image(playerImg, this.x, this.y, this.size, this.size);
if (keyIsDown(LEFT_ARROW)) {
} else if (keyIsDown(RIGHT_ARROW)) {
this.x = constrain(this.x, 0, width - this.size);
this.x = random(0, width - this.size);
this.y = random(-200, -50);
this.type = random(["healthy", "unhealthy"]);
this.image = this.type === "healthy" ? random(healthyFoods) : random(unhealthyFoods);
this.speed = random(1, 2);
image(this.image, this.x, this.y, this.size, this.size);
player.x + player.size / 2,
player.y + player.size / 2
return d < this.size / 2 + player.size / 2;
this.x = random(0, width - this.size);
this.y = random(-200, -50);
this.type = random(["healthy", "unhealthy"]);
this.image = this.type === "healthy" ? random(healthyFoods) : random(unhealthyFoods);
this.speed = random(1, 2);