xxxxxxxxxx
305
let gameState = "instructions";
let speedMultiplier = 15;
let score = 0;
let gameOver = false;
let startButton;
let snailImage; // Global variable for the snail image
let snails = []; // Array to hold snail instances
let sunflower = [];
let backgroundImage;
let gameOverSound;
let bonuses = [];
let bgMusic; // Global variable for the background music
let customCursorImg;
function preload() {
gameOverSound = loadSound("sounds/gameoversound.mp3");
// This URL is just an example and will not work directly. You need to use the URL provided by the p5.js Web Editor after uploading.
snailImage = loadImage("images/c2.png");
sunflowerImage = loadImage("images/flower.png");
backgroundImage = loadImage("images/gardenpic.jpeg");
bgMusic = loadSound("sounds/Gamemusic.mp3");
customCursorImg = loadImage("images/basket.png");
}
function setup() {
createCanvas(windowWidth, windowHeight);
startButton = createButton("Start Game");
startButton.position(width / 2 - startButton.width / 2, height / 2 + 320); // Center the button
startButton.mousePressed(startGame);
// Styling the button
startButton.style("background-color", "#005031"); // A dark green
startButton.style("color", "white");
startButton.style("font-size", "20px");
startButton.style("padding", "10px 20px");
startButton.style("border", "none");
startButton.style("border-radius", "5px");
startButton.style("cursor", "pointer");
startButton.style("font-family", "Georgia"); // Set the font to Georgia
}
function startGame() {
if (!fullscreen()) {
//Checks if the game is currently not in fullscreen mode
fullscreen(true); // Attempt to enter fullscreen mode
}
// Reset game state for a new game
gameOver = false; //game is active and not ended.
score = 0; //Resets the player's score to 0, starting from scratch for the new game session
speedMultiplier = 1.0; //Resets the speedMultiplier to 1.0.
snails = []; // Initialize or re-initialize the snails array. Resets for new game, start without leftover objects
sunflower = [];
gameState = "playing";
bgMusic.loop(); // Start playing the background music in a loop
startButton.hide(); // Hide the button once the game starts
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
startButton.position(width / 2 - startButton.width / 2, height / 2 + 320);
}
function draw() {
if (gameState === "instructions") {
displayInstructions();
startButton.show();
} else if (gameState === "playing") {
background(backgroundImage);
noCursor();
image(customCursorImg, mouseX - 75, mouseY - 75, 100, 100);
// cursor(customCursorImg);
if (!gameOver) {
handleSunflower();
handleSnails();
// Update and display each Bonus object
for (let i = bonuses.length - 1; i >= 0; i--) {
bonuses[i].update();
bonuses[i].display();
// Remove the Bonus object if it goes off screen, is popped, or has been visible for more than 2 seconds
if (millis() - bonuses[i].timestamp > 2000) {
// Check if 2 seconds have passed since creation
bonuses[i].toDelete = true; // Mark for deletion
}
if (bonuses[i].offScreen() || bonuses[i].toDelete) {
bonuses.splice(i, 1); // Remove the Bonus object from the array
}
}
// Generate Bonus objects when score is a multiple of 5 and no bonus is currently displayed
if (score % 5 === 0 && score !== 0 && bonuses.length === 0) {
bonuses.push(new Bonus()); // Add a new Bonus object
}
checkCollisions();
displayScore();
} else {
gameState = "gameOver";
displayGameOver();
gameOverSound.play();
}
} else if (gameState === "gameOver") {
displayGameOver();
//startButton.html('Play Again');
// startButton.show();
}
}
function displayInstructions() {
background("#DFF0E3"); // A soft green background
fill("#005030"); // Dark green text
textFont("Georgia"); //elegant font
textSize(24);
textAlign(CENTER, CENTER); // Center align the text
text(
"🌻 Welcome to Floral Frenzy 🌻\n\nThe game where your garden dreams and ninja reflexes come together! \n\nUse the basket to pick up flowers and earn points. \n\nAvoid snails and missing flowers, or it's game over. \n\nGood luck! \n\nReady, set, slice!",
width / 20,
height / 2 - 50,
width - 40
); // Center the text and provide width for wrapping
}
function checkCollisions() {
// Define the rectangular bounds of the cursor (basket) image
let cursorLeft = mouseX - 75;
let cursorRight = mouseX + 25;
let cursorTop = mouseY - 75;
let cursorBottom = mouseY + 25;
// Check collision with sunflowers
for (let i = sunflower.length - 1; i >= 0; i--) {
let flower = sunflower[i];
let flowerCenterX = flower.x;
let flowerCenterY = flower.y;
let flowerRadius = flower.size / 2;
// Find the closest point on the cursor rectangle to the center of the sunflower
let closestX = constrain(flowerCenterX, cursorLeft, cursorRight);
let closestY = constrain(flowerCenterY, cursorTop, cursorBottom);
// Calculate the distance between the sunflower's center and this closest point
let distanceX = flowerCenterX - closestX;
let distanceY = flowerCenterY - closestY;
let distance = sqrt(distanceX * distanceX + distanceY * distanceY);
// If the distance is less than the sunflower's radius, there's a collision
if (distance < flowerRadius) {
score++; // Increment score for collecting a sunflower
sunflower.splice(i, 1); // Remove the collected sunflower
speedMultiplier += 0.05; // Optionally adjust speed or other gameplay elements here
}
}
// Check collision with Bonus objects
for (let i = bonuses.length - 1; i >= 0; i--) {
let bonus = bonuses[i];
// Assuming Bonus objects have a method or properties to define their position and radius
let bonusCenterX = bonus.x;
let bonusCenterY = bonus.y;
let bonusRadius = bonus.size / 2; // Adjust this based on your Bonus object's properties
// Apply the same rectangle-circle collision detection logic
let closestX = constrain(bonusCenterX, cursorLeft, cursorRight);
let closestY = constrain(bonusCenterY, cursorTop, cursorBottom);
let distanceX = bonusCenterX - closestX;
let distanceY = bonusCenterY - closestY;
let distance = sqrt(distanceX * distanceX + distanceY * distanceY);
if (distance < bonusRadius) {
score += 2; // Adjust the score increment as needed
bonuses.splice(i, 1); // Remove the collected bonus
}
}
// Check collision with snails (bombs)
for (let i = snails.length - 1; i >= 0; i--) {
let snail = snails[i];
// Assuming Snail objects have a method or properties to define their position and radius
let snailCenterX = snail.x;
let snailCenterY = snail.y;
let snailRadius = snail.size / 2; // Adjust this based on your Snail object's properties
// Apply the same rectangle-circle collision detection logic
let closestX = constrain(snailCenterX, cursorLeft, cursorRight);
let closestY = constrain(snailCenterY, cursorTop, cursorBottom);
let distanceX = snailCenterX - closestX;
let distanceY = snailCenterY - closestY;
let distance = sqrt(distanceX * distanceX + distanceY * distanceY);
if (distance < snailRadius) {
gameOver = true; // End the game if a snail is "collected"
return; // Exit to prevent further checks once gameeover is triggered
}
}
}
function handleSunflower() {
// Generate new Sunflower instances periodically
if (frameCount % 60 === 0) {
// Adjust according to your game's difficulty
sunflower.push(new Sunflower()); // Use Sunflower class
}
// Update and display each sunflower
for (let i = sunflower.length - 1; i >= 0; i--) {
sunflower[i].update();
sunflower[i].display();
// Check if the sunflower goes off-screen
if (sunflower[i].offScreen()) {
gameOver = true;
break;
}
}
}
function handleSnails() {
if (frameCount % 150 === 0) {
// Adjust timing as needed
snails.push(new Snail());
}
snails.forEach((snail, index) => {
snail.update();
snail.display();
});
}
function displayScore() {
fill(0);
textSize(32);
text(`Score: ${score}`, 60, 40);
}
function displayGameOver() {
// Stop the background music if it's playing
if (bgMusic.isPlaying()) {
bgMusic.stop();
}
// Play the game over sound if it's not already playing
if (!gameOverSound.isPlaying()) {
gameOverSound.play();
}
// Set the background and text properties for the game over screen
background("#DFF0E3"); // Soft green background to match the rest of the game's theme
fill("#005030"); // Dark green text color
textFont("Georgia"); // Elegant font choice for better visual appeal
textAlign(CENTER, CENTER); // Center the text for better readability
// Display the game over message and the player's final score
textSize(48); // Larger text size for the main game over message
text(`GAME OVER - Score: ${score}`, width / 2, height / 2 - 100); // Display the message
// Instruction for restarting the game
textSize(24); // Smaller text size for the instruction
text("Click Anywhere to PLAY AGAIN!", width / 2, height / 2); // Display the instruction
}
function mousePressed() {
if (gameState === "gameOver") {
// Reset the game directly to the playing state
restartGame(); // Call a function that resets the game variables and state
}
}
function restartGame() {
gameOver = false;
score = 0;
speedMultiplier = 1.0; // Assuming 1.0 is the initial speed multiplier; adjust as needed
snails = []; // Reinitialize the snails array for a new game
sunflower = []; // Reinitialize the sunflower array for a new game
bonuses = []; // Clear the bonuses array if applicable
gameState = "playing"; // Set the gameState to 'playing'
// Optionally reset the background music and start it again
if (bgMusic.isPlaying()) {
bgMusic.stop();
}
bgMusic.loop();
}
function keyPressed() {
// Check for fullscreen toggle or gameState change first
if (key === "f" || key === "F") {
let fs = fullscreen();
fullscreen(!fs);
}
if (gameState === "instructions" && (key === " " || key === "Enter")) {
gameState = "playing";
}
// Log any key press for debugging
console.log(`Key pressed: ${key}`);
// Screenshot save img
if (key === "e" || key === "E") {
saveCanvas("myCanvas", "png");
}
}