xxxxxxxxxx
let fairyCharacter;
let star;
let mushroomImage;
let backgroundImage; // Declare a variable for the background image
let fairyX, fairyY, fairySpeed, jumpSpeed, velocity, gravity;
let mushroom = [];
let stars = [];
let starSize;
let lives;
let isJumping = false;
let starCounter = 0; // To keep track of how many stars the fairy collects
let gameStarted = false; // Flag to track if the game has started
let gameOver = false; // Flag to track if the game is over
let mainMenu = true; // Flag to track if we are at the main menu
function preload() {
// Load images
fairyCharacter = loadImage('fairy-2.png');
star = loadImage('star.png');
mushroomImage = loadImage('mushroom.png'); // Load mushroom image
backgroundImage = loadImage('background.jpg'); // Load the background image
}
function setup() {
createCanvas(800, 600); // Set the canvas size
// Initialize variables
fairyX = width / 2;
fairyY = height - 100; // Start at the bottom of the screen
fairySpeed = 5; // How fast the fairy moves left/right
jumpSpeed = -15; // How high the fairy jumps
velocity = 0; // Vertical speed (initially 0)
gravity = 0.8; // Gravity to pull the fairy down
lives = 3; // Starting with 3 lives
// Star properties
starSize = 40;
// Create 5 stars with random positions
for (let i = 0; i < 5; i++) {
stars.push({
x: random(100, width - 100),
y: random(100, height - 200),
size: starSize,
xSpeed: random(-1, 1), // Horizontal speed
ySpeed: random(-0.5, 0.5), // Vertical speed
});
}
// Create initial 4 mushrooms with random positions and properties
for (let i = 0; i < 4; i++) {
mushroom.push({
x: random(100, width - 100),
y: random(100, height - 200),
size: 60,
xSpeed: random(-2, 2), // Horizontal movement speed
ySpeed: random(-2, 2), // Vertical movement speed
direction: random([-1, 1]), // Direction to float (up or down)
amplitude: random(20, 50), // The amount of vertical floating
});
}
}
function draw() {
if (gameOver) {
displayGameOverScreen();
return; // Skip the rest of the drawing code if the game is over
}
if (mainMenu) {
// Display the Start Screen
displayStartScreen();
return; // Skip the rest of the drawing code
}
background(0); // Set the background color to black for gameplay
// Draw the background image
image(backgroundImage, 0, 0); // Fill the entire canvas with the background image
// Handle movement of the fairy
if (keyIsDown(LEFT_ARROW)) {
fairyX -= fairySpeed; // Move left
}
if (keyIsDown(RIGHT_ARROW)) {
fairyX += fairySpeed; // Move right
}
// Move up and down with arrow keys
if (keyIsDown(UP_ARROW)) {
fairyY -= fairySpeed;
}
if (keyIsDown(DOWN_ARROW)) {
fairyY += fairySpeed;
}
// Check for collisions with mushrooms
for (let i = 0; i < mushroom.length; i++) {
if (dist(fairyX, fairyY, mushroom[i].x, mushroom[i].y) < (mushroom[i].size / 2 + 25)) {
// Fairy touches the mushroom, lose 1 life
lives--;
if (lives <= 0) {
gameOver = true; // End the game when lives reach 0
}
resetGame(); // Call the reset function after losing a life
break; // Stop checking other mushrooms after collision
}
// Move the mushrooms around
mushroom[i].x += mushroom[i].xSpeed;
mushroom[i].y += mushroom[i].ySpeed;
// Reverse direction when mushrooms hit the canvas edges
if (mushroom[i].x <= 0 || mushroom[i].x >= width - mushroom[i].size) {
mushroom[i].xSpeed *= -1; // Reverse horizontal direction
}
if (mushroom[i].y <= 0 || mushroom[i].y >= height - mushroom[i].size) {
mushroom[i].ySpeed *= -1; // Reverse vertical direction
}
// Make mushrooms float (up and down like balloons)
mushroom[i].y += mushroom[i].direction * 0.5; // Float up/down
if (mushroom[i].y > height - 200 + mushroom[i].amplitude || mushroom[i].y < height - 200 - mushroom[i].amplitude) {
mushroom[i].direction *= -1; // Reverse floating direction
}
}
// Check for collision with the stars
for (let i = 0; i < stars.length; i++) {
if (dist(fairyX, fairyY, stars[i].x, stars[i].y) < (stars[i].size / 2 + 25)) {
// Fairy touches the star, increase lives by 1
lives++;
starCounter++; // Increase the star collection counter
// Move the star to a new position after collection
stars[i].x = random(100, width - 100);
stars[i].y = random(100, height - 200);
}
// Move the stars
stars[i].x += stars[i].xSpeed;
stars[i].y += stars[i].ySpeed;
// Reverse direction when stars hit the canvas edges
if (stars[i].x <= 0 || stars[i].x >= width - stars[i].size) {
stars[i].xSpeed *= -1; // Reverse horizontal direction
}
if (stars[i].y <= 0 || stars[i].y >= height - stars[i].size) {
stars[i].ySpeed *= -1; // Reverse vertical direction
}
}
// Add mushrooms every time the fairy collects 10 stars
if (starCounter >= 10) {
starCounter = 0; // Reset the star collection counter
mushroom.push({
x: random(100, width - 100),
y: random(100, height - 200),
size: 60,
xSpeed: random(-2, 2),
ySpeed: random(-2, 2),
direction: random([-1, 1]),
amplitude: random(20, 50),
});
}
// Draw the fairy
image(fairyCharacter, fairyX, fairyY); // Draw the fairy at position (fairyX, fairyY)
// Draw the mushrooms
for (let i = 0; i < mushroom.length; i++) {
image(mushroomImage, mushroom[i].x, mushroom[i].y, mushroom[i].size, mushroom[i].size); // Draw each mushroom
}
// Draw the stars
for (let i = 0; i < stars.length; i++) {
image(star, stars[i].x, stars[i].y, stars[i].size, stars[i].size); // Draw each star
}
// Display the lives with improved visibility
fill(255, 182, 193); // Pink background for the lives box (light pink)
stroke(255, 105, 180); // Hot pink border for extra visibility
strokeWeight(4); // Thicker border
rect(width - 120, 15, 100, 40, 10); // Draw the white rectangle with rounded corners
noStroke(); // No border for the text
fill(0); // Set text color to black for better contrast
textSize(18);
textAlign(CENTER, CENTER);
text("Lives: " + lives, width - 70, 35); // Draw the lives text inside the rectangle
// If lives are 0, trigger the game over screen
if (lives <= 0) {
gameOver = true; // Set the gameOver flag
}
}
// Function to reset the game when the fairy touches a mushroom
function resetGame() {
fairyX = width / 2; // Reset fairy position to center
fairyY = height - 100;
}
// Function to display the start screen with cute game rules
function displayStartScreen() {
background(0); // Keep the background black for the start screen
fill(255); // Set text color to white
textSize(30);
textAlign(CENTER, CENTER);
text("🌟 Fairy Adventure 🌟", width / 2, height / 2 - 150);
// Display the cute game rules
textSize(18);
text("Help our fairy collect stars and avoid the mushrooms!", width / 2, height / 2 - 80);
text("You have 3 lives. Be careful!", width / 2, height / 2 - 50);
text("Every 10 stars = more mushrooms!", width / 2, height / 2 - 20);
text("Ready for the adventure? ", width / 2, height / 2 + 10);
// Draw the start button
fill(255, 105, 180); // Light pink button for the start button
rect(width / 2 - 100, height / 2 + 50, 200, 50, 15); // Start button with rounded corners
fill(255); // White text inside button
textSize(24);
text("Start Game", width / 2, height / 2 + 75); // Text inside the button
}
// Function to display the game over screen
function displayGameOverScreen() {
background(0); // Keep the background black for the game over screen
fill(255); // Set text color to white
textSize(48);
textAlign(CENTER, CENTER);
text("Game Over", width / 2, height / 2 - 100); // Game over title
// Draw the start again button
fill(255, 105, 180); // Light pink button for the start button
rect(width / 2 - 100, height / 2, 200, 50, 15); // Start button with rounded corners
fill(255); // White text inside button
textSize(24);
text("Start Again", width / 2, height / 2 + 25); // Text inside the button
}
// Handle mouse click to start the game or restart
function mousePressed() {
// Check if the start button was clicked
if (mouseX > width / 2 - 100 && mouseX < width / 2 + 100 && mouseY > height / 2 + 50 && mouseY < height / 2 + 100) {
if (gameOver) {
mainMenu = true; // Set game to main menu state
gameOver = false; // Reset gameOver flag
} else {
gameStarted = true; // Set the gameStarted flag to true
mainMenu = false; // Exit main menu state
loop(); // Start the game loop
}
}
// Check if the "Start Again" button was clicked on the game over screen
if (gameOver) {
if (mouseX > width / 2 - 100 && mouseX < width / 2 + 100 && mouseY > height / 2 && mouseY < height / 2 + 50) {
// Reset the game to the main menu
mainMenu = true; // Show the start screen
gameOver = false; // Reset game over flag
resetGame(); // Reset the game state (lives, fairy position, etc.)
}
}
}