xxxxxxxxxx
328
let chefImg;
let pizzaImg;
let meatballImg;
let ship;
let flowers = [];
let drops = [];
let powerUps = [];
let startScreen = true;
let gameOverScreen = false;
let font;
let backImg;
let round = 1;
let lives = 3; // Starting with 3 lives
let score = 0;
let boss;
let bossActive = false;
let bossDefeated = false;
function preload() {
chefImg = loadImage("chef.png");
pizzaImg = loadImage("pizza.png");
meatballImg = loadImage("meatball.png");
backImg = loadImage("Back.jpeg"); // Load the background image
font = loadFont("Club.ttf");
bossImg = loadImage("Boss.png"); // Load the boss image
}
function setup() {
createCanvas(600, 400);
textAlign(LEFT, CENTER); // Align text to the left
}
function draw() {
if (startScreen) {
background(backImg); // Set the background image for the start screen
textSize(36); // Shrink the font size
textFont(font);
fill(255, 255, 0); // Set text color to yellow
text("KITCHEN INVADERS!", 15, height / 6 - 30); // Move the text to the left
textSize(18); // Shrink the font size for the second line
text("Press ENTER to start", 20, height / 15 + 50); // Move the text to the left
} else if (gameOverScreen) {
background(0);
textAlign(CENTER, CENTER);
textSize(48);
textFont(font);
fill(255, 0, 0);
text("GAME OVER", width / 2, height / 2 - 50);
textSize(24);
fill(255);
text("Press ENTER to restart", width / 2, height / 2 + 50);
textAlign(LEFT, CENTER); // Reset text alignment
} else {
background(100, 200, 150);
// Show and move the ship
ship.show();
ship.move();
// Meatballs
for (let i = drops.length - 1; i >= 0; i--) {
drops[i].show();
drops[i].move();
// Check for collisions with pizzas
for (let j = flowers.length - 1; j >= 0; j--) {
if (drops[i].hits(flowers[j])) {
flowers.splice(j, 1);
drops.splice(i, 1);
break;
}
}
// Check if the meatball hits the boss
if (bossActive && drops[i].hits(boss)) {
score += 10; // Increase score for hitting the boss
drops.splice(i, 1); // Remove the meatball
}
}
// Show and move the enemies
for (let flower of flowers) {
flower.show();
flower.move();
// Check for collision with chef (ship)
if (flower.hits(ship)) {
lives--; // Lose a life
flower.reset(); // Reset the pizza to the top
ship.reset(); // Reset the chef position
if (lives <= 0) {
gameOverScreen = true; // Trigger game over
}
}
}
// Power-ups
for (let i = powerUps.length - 1; i >= 0; i--) {
powerUps[i].show();
powerUps[i].move();
// Check for collision with the ship (chef)
if (powerUps[i].hits(ship)) {
score += 5; // Increase score or apply power-up effect
powerUps.splice(i, 1); // Remove the power-up
}
}
// Boss fight (appears after a certain round)
if (round >= 3 && !bossActive) {
boss = new Boss(width / 2, 50); // Create the boss
bossActive = true; // Activate the boss
}
// Show the boss if active
if (bossActive && !bossDefeated) {
boss.show();
boss.move();
}
// Check if the boss is defeated
if (bossActive && bossDefeated) {
bossActive = false; // Deactivate the boss
round++; // Increase round
}
// Check if all pizzas are defeated
if (flowers.length === 0 && !bossActive) {
round++;
let newPizzas = int(random(1, 3)); // Add 1 or 2 more pizzas
for (let i = 0; i < newPizzas; i++) {
flowers.push(new Flower(i * 80 + 80, 60));
}
}
// Display the lives (3 dots)
displayLives();
// Display the score
displayScore();
}
}
// Ship class
function Ship() {
this.x = width / 2;
this.show = function () {
imageMode(CENTER);
let imgWidth = 130;
let imgHeight = (chefImg.height / chefImg.width) * imgWidth;
image(chefImg, this.x, height - 60, imgWidth, imgHeight);
};
this.move = function () {
if (keyIsDown(LEFT_ARROW) && this.x > 60) { // Prevent going off the left
this.x -= 5;
} else if (keyIsDown(RIGHT_ARROW) && this.x < width - 60) { // Prevent going off the right
this.x += 5;
}
};
this.reset = function () {
this.x = width / 2; // Reset the position to the center
};
}
// Pizza enemies (flowers)
function Flower(x, y) {
this.x = x;
this.y = y;
this.xSpeed = random(-1, 1); // Slightly slower horizontal speed for movement
this.ySpeed = random(1, 2); // Slightly slower vertical speed for movement
this.show = function () {
imageMode(CENTER);
let imgWidth = 50;
let imgHeight = (pizzaImg.height / pizzaImg.width) * imgWidth;
image(pizzaImg, this.x, this.y, imgWidth, imgHeight);
};
this.move = function () {
this.x += this.xSpeed;
this.y += this.ySpeed;
// Bounce off the walls
if (this.x <= 60 || this.x >= width - 60) {
this.xSpeed *= -1;
}
// Reset position if they go off the bottom
if (this.y > height) {
this.reset();
}
};
this.hits = function (ship) {
let d = dist(this.x, this.y, ship.x, height - 60);
return d < 50; // Increase the radius to make collision detection easier
};
this.reset = function () {
this.y = random(-50, -10);
this.x = random(60, width - 60);
};
}
// Projectiles (meatballs)
function Drop(x, y) {
this.x = x + 15;
this.y = y;
this.show = function () {
imageMode(CENTER);
let imgWidth = 30;
let imgHeight = (meatballImg.height / meatballImg.width) * imgWidth;
image(meatballImg, this.x, this.y, imgWidth, imgHeight);
};
this.move = function () {
this.y -= 5;
};
this.hits = function (object) {
let d = dist(this.x, this.y, object.x, object.y);
return d < 20;
};
}
// Power-up (yellow orb)
function PowerUp(x, y) {
this.x = x;
this.y = y;
this.show = function () {
fill(255, 255, 0);
noStroke();
ellipse(this.x, this.y, 20, 20);
};
this.move = function () {
this.y += 2;
};
this.hits = function (ship) {
let d = dist(this.x, this.y, ship.x, height - 60);
return d < 30;
};
}
// Boss class
function Boss(x, y) {
this.x = x;
this.y = y;
this.xSpeed = 2;
this.show = function () {
imageMode(CENTER);
let imgWidth = 150;
let imgHeight = (bossImg.height / bossImg.width) * imgWidth * 2; // Double the size for the boss
image(bossImg, this.x, this.y, imgWidth, imgHeight);
};
this.move = function () {
this.x += this.xSpeed;
// Bounce off the walls
if (this.x <= 60 || this.x >= width - 60) {
this.xSpeed *= -1;
}
};
this.defeated = function () {
// Logic to check if the boss is defeated, e.g., hit by enough meatballs
return score > 100; // Example condition
};
}
// Display lives as dots
function displayLives() {
fill(0);
noStroke();
let xPos = 20;
for (let i = 0; i < lives; i++) {
ellipse(xPos, 20, 10, 10); // Draw a dot for each life
xPos += 15;
}
}
// Display the score
function displayScore() {
textSize(20);
fill(0);
text("Score: " + score, width - 100, 20);
}
// Handle key presses to start the game or shoot
function keyPressed() {
if (startScreen && keyCode == ENTER) {
startScreen = false; // Start the game when Enter is pressed
gameOverScreen = false; // Make sure the game over screen is reset
ship = new Ship(); // Recreate the ship in case the game is restarted
flowers = []; // Reset the enemies
drops = []; // Reset the meatballs
powerUps = []; // Reset the power-ups
lives = 3; // Reset lives
score = 0; // Reset score
round = 1; // Reset round
// Create initial enemies (pizza images) when starting the game
for (let i = 0; i < 6; i++) {
flowers.push(new Flower(i * 80 + 80, 60));
}
} else if (gameOverScreen && keyCode == ENTER) {
gameOverScreen = false; // Restart the game when Enter is pressed
lives = 3; // Reset lives
flowers = []; // Reset the enemies
drops = []; // Reset the meatballs
powerUps = []; // Reset the power-ups
ship.reset(); // Reset the chef position
// Create initial enemies again
for (let i = 0; i < 6; i++) {
flowers.push(new Flower(i * 80 + 80, 60));
}
} else if (key == " ") {
drops.push(new Drop(ship.x, height - 30));
}
}