xxxxxxxxxx
let gardener;
let mushrooms = [];
let gameActive = false;
let score = 10;
let redHitCount = 0;
let speedMultiplier = 1;
let gameTime = 0;
let startImage; // Variable to hold the starting image
function preload() {
startImage = loadImage("MUshrun.png"); // Ensure the image file is correctly uploaded
}
function setup() {
createCanvas(400, 600);
gardener = new Gardener();
}
function draw() {
background(50, 150, 50); // Green garden background
if (!gameActive) {
showStartScreen();
return;
}
gardener.update();
gardener.display();
if (frameCount % int(60 / speedMultiplier) === 0) {
mushrooms.push(new Mushroom());
}
for (let i = mushrooms.length - 1; i >= 0; i--) {
mushrooms[i].update();
mushrooms[i].display();
if (mushrooms[i].hits(gardener)) {
if (mushrooms[i].type === "beige") {
score++;
} else if (mushrooms[i].type === "red") {
redHitCount++;
} else if (mushrooms[i].type === "snail") {
endGame("You hit a snail mushroom!");
return;
}
mushrooms.splice(i, 1);
} else if (mushrooms[i].offScreen()) {
if (mushrooms[i].type === "beige") {
score -= 3;
}
mushrooms.splice(i, 1);
}
}
displayScore();
if (score <= 0) {
endGame("You reached 0 points!");
} else if (redHitCount >= 3) {
endGame("You hit 3 red mushrooms!");
}
gameTime++;
if (gameTime % 600 === 0) {
speedMultiplier += 0.2;
}
}
function keyPressed() {
if (key === ' ') {
if (!gameActive) {
resetGame(); // Start the game
gameActive = true; // Toggle the game state
}
}
}
function showStartScreen() {
background(50, 150, 50);
image(startImage, 0, 0, width, height); // Display the image as the background
textAlign(CENTER, CENTER);
fill(255);
textSize(24);
text("Press SPACE to start", width / 2, height - 50); // Display instructions over the image
}
function displayScore() {
fill(255);
textSize(16);
textAlign(LEFT, TOP);
text(`Score: ${score}`, 10, 20);
text(`Red Hits: ${redHitCount}/3`, 10, 40);
}
function endGame(message) {
gameActive = false;
mushrooms = [];
fill(255, 0, 0);
textSize(24);
textAlign(CENTER, CENTER);
text(message, width / 2, height / 2);
text("Press SPACE to restart", width / 2, height / 2 + 40);
}
function resetGame() {
// Reset all game variables to their initial state
score = 10;
redHitCount = 0;
speedMultiplier = 1;
gameTime = 0;
mushrooms = [];
gardener = new Gardener(); // Reinitialize gardener
}
class Gardener {
constructor() {
this.x = width / 2;
this.y = height - 50;
}
update() {
this.x = constrain(mouseX, 30, width - 30); // Constrain the gardener to stay within canvas boundaries
}
display() {
// Draw the head
fill(200, 200, 0); // Yellow for the head
ellipse(this.x, this.y - 25, 30, 30);
// Draw the body
fill(0, 0, 255); // Blue for the body
rectMode(CENTER);
rect(this.x, this.y, 20, 40);
// Draw the arms
stroke(0, 0, 255); // Blue for the arms
strokeWeight(4);
line(this.x - 15, this.y, this.x + 15, this.y); // Horizontal arms
// Draw the legs
line(this.x, this.y + 20, this.x - 10, this.y + 40); // Left leg
line(this.x, this.y + 20, this.x + 10, this.y + 40); // Right leg
// Reset stroke
noStroke();
}
}
class Mushroom {
constructor() {
this.x = random(20, width - 20);
this.y = -20;
this.size = 20;
let r = random(1);
if (r < 0.7) {
this.type = "beige";
this.color = color(200, 150, 100);
} else if (r < 0.9) {
this.type = "red";
this.color = color(255, 0, 0);
} else {
this.type = "snail";
this.color = color(100, 100, 100);
}
}
update() {
this.y += 5 * speedMultiplier;
}
display() {
// Draw the mushroom stem
fill(255); // White for the stem
rectMode(CENTER);
rect(this.x, this.y + this.size / 3, this.size / 4, this.size / 1.5);
// Draw the mushroom cap
fill(this.color);
arc(this.x, this.y, this.size, this.size, PI, TWO_PI);
// If the mushroom is a snail type, add a snail on top
if (this.type === "snail") {
// Draw the snail's shell
fill(150, 75, 0); // Brown for the shell
ellipse(this.x, this.y - this.size / 2, this.size / 2, this.size / 2);
// Draw the snail's body
fill(100, 200, 100); // Green for the body
ellipse(this.x, this.y - this.size / 3, this.size / 3, this.size / 6);
// Draw the snail's eyes
fill(0); // Black for eyes
ellipse(this.x - this.size / 6, this.y - this.size / 2 - 5, this.size / 10, this.size / 10);
ellipse(this.x + this.size / 6, this.y - this.size / 2 - 5, this.size / 10, this.size / 10);
}
}
hits(gardener) {
return (
this.y + this.size / 2 > gardener.y - 40 &&
this.y - this.size / 2 < gardener.y + 40 &&
this.x + this.size / 2 > gardener.x - 15 &&
this.x - this.size / 2 < gardener.x + 15
);
}
offScreen() {
return this.y > height + this.size / 2;
}
}