xxxxxxxxxx
232
let chefImg;
let pizzaImg;
let meatballImg;
let ship;
let flowers = [];
let drops = [];
let startScreen = true;
let gameOverScreen = false;
let font;
let backImg;
let round = 1;
let lives = 3;
let score = 0;
function preload() {
chefImg = loadImage("chef.png");
pizzaImg = loadImage("pizza.png");
meatballImg = loadImage("meatball.png");
backImg = loadImage("Back.jpeg");
font = loadFont("Club.ttf");
}
function setup() {
createCanvas(600, 400);
textAlign(LEFT, CENTER);
}
function draw() {
if (startScreen) {
// Display the start screen
background(backImg);
textSize(36);
textFont(font);
fill(255, 255, 0);
text("KITCHEN INVADERS!", 15, height / 6 - 30);
textSize(18);
text("Press ENTER to start", 20, height / 15 + 50);
} else if (gameOverScreen) {
// Display the game over screen
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);
} else {
// Main game loop
background(100, 200, 150);
ship.show();
ship.move();
// Update and check each meatball's movement and collisions
for (let i = drops.length - 1; i >= 0; i--) {
drops[i].show();
drops[i].move();
for (let j = flowers.length - 1; j >= 0; j--) {
if (drops[i].hits(flowers[j])) {
flowers.splice(j, 1);
drops.splice(i, 1);
score++; // Increment score
break;
}
}
}
// Update and check each pizza's (flower's) movement and collisions
for (let flower of flowers) {
flower.show();
flower.move();
if (flower.hits(ship)) {
lives--; // Decrease lives if a pizza hits the ship
flower.reset(); // Reset pizza position
ship.reset(); // Reset ship position
if (lives <= 0) {
gameOverScreen = true; // Trigger game over
}
}
}
// Add more pizzas when all are defeated
if (flowers.length === 0) {
round++;
let newPizzas = int(random(3, 5));
for (let i = 0; i < newPizzas; i++) {
flowers.push(new Flower(i * 80 + 80, 60));
}
}
// Display lives and score
displayLives();
displayScore();
}
}
// Ship class (represents the chef)
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) {
this.x -= 5;
} else if (keyIsDown(RIGHT_ARROW) && this.x < width - 60) {
this.x += 5;
}
};
this.reset = function () {
this.x = width / 2;
};
}
// Flower class (represents pizza enemies)
function Flower(x, y) {
this.x = x;
this.y = y;
this.xSpeed = random(-1, 1);
this.ySpeed = random(1, 2);
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;
if (this.x <= 60 || this.x >= width - 60) {
this.xSpeed *= -1;
}
if (this.y > height) {
this.reset();
}
};
this.hits = function (ship) {
let d = dist(this.x, this.y, ship.x, height - 60);
return d < 50;
};
this.reset = function () {
this.y = random(-50, -10);
this.x = random(60, width - 60);
};
}
// Drop class (represents meatballs shot by the chef)
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 (flower) {
let d = dist(this.x, this.y, flower.x, flower.y);
return d < 20;
};
}
// Display remaining lives as dots
function displayLives() {
fill(0);
noStroke();
let xPos = 20;
for (let i = 0; i < lives; i++) {
ellipse(xPos, 20, 10, 10);
xPos += 15;
}
textSize(20);
text("Lifes", xPos + 10, 20);
}
// Display score in the top-right corner
function displayScore() {
textSize(20);
fill(0);
textAlign(RIGHT, TOP);
text("Score: " + score, width - 20, 20);
}
// Handle key presses for starting game, restarting, and shooting
function keyPressed() {
if (startScreen && keyCode == ENTER) {
startScreen = false;
gameOverScreen = false;
ship = new Ship();
flowers = [];
drops = [];
lives = 3;
score = 0;
for (let i = 0; i < 6; i++) {
flowers.push(new Flower(i * 80 + 80, 60));
}
} else if (gameOverScreen && keyCode == ENTER) {
gameOverScreen = false;
lives = 3;
flowers = [];
drops = [];
score = 0;
ship.reset();
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));
}
}