code
.game-container {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.sun {
height: 20px;
width: 20px;
background-color: yellow;
border-radius: 50%;
margin: 5px;
}
.plant {
height: 30px;
width: 30px;
margin: 5px;
background-color: #2ecc71;
display: flex;
justify-content: center;
align-items: center;
border-radius: 50%;
}
.peanut {
background-color: #e67e22;
}
.pea-shooter {
background-color: #27ae60;
}
.zombie {
height: 40px;
width: 40px;
background-color: #e74c3c;
display: flex;
justify-content: center;
align-items: flex-end;
border-radius: 50%;
}
.progress-bar {
height: 10px;
width: 300px;
background-color: #ddd;
border-radius: 5px;
margin: 10px 0;
}
.progress-bar-fill {
height: 100%;
width: 0;
background-color: #e74c3c;
border-radius: 5px;
}
.message-box {
display: none;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.7);
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 24px;
}
</style>
code
<!-- Plants section -->
<div id="plants"></div>
<!-- Zombie section -->
<div id="zombies"></div>
<!-- Progress bar -->
<div class="progress-bar">
<div class="progress-bar-fill"></div>
</div>
</div>
<!-- Message box -->
<div class="message-box" id="message-box">
<p id="message-text"></p>
<button onclick="restartGame()">Restart</button>
</div>
<script>
// Game variables
var waves = [
{
plants: [1, 0, 0],
zombies: 2,
zombieSpeed: 2,
sunInterval: 1000,
},
{
plants: [2, 0, 0],
zombies: 3,
zombieSpeed: 3,
sunInterval: 800,
},
{
plants: [1, 1, 1],
zombies: 4,
zombieSpeed: 4,
sunInterval: 600,
}
];
var currentWave = 0;
var sunnies = 50;
var intervalID;
var plants = [];
var zombies = [];
var zombieSpeed = waves[currentWave].zombieSpeed;
var progressBarWidth = 0;
// Plant classes
class Plant {
constructor(type) {
this.type = type;
this.health = 10; // Default health
}
draw() {
const plantDiv = document.createElement('div');
plantDiv.classList.add('plant');
if (this.type === 'sunflower') {
plantDiv.classList.add('sunflower');
} else if (this.type === 'peanut') {
plantDiv.classList.add('peanut');
} else if (this.type === 'pea-shooter') {
plantDiv.classList.add('pea-shooter');
}
document.getElementById('plants').appendChild(plantDiv);
this.element = plantDiv;
}
takeDamage() {
this.health--;
if (this.health === 0) {
this.die();
}
}
die() {
document.getElementById('plants').removeChild(this.element);
this.element = null;
}
}
// Zombie class
class Zombie {
constructor() {
this.health = 10;
}
draw() {
const zombieDiv = document.createElement('div');
zombieDiv.classList.add('zombie');
document.getElementById('zombies').appendChild(zombieDiv);
this.element = zombieDiv;
this.element.addEventListener('animationend', this.onAnimationEnd.bind(this));
}
onAnimationEnd() {
this.element.remove();
this.remove = true;
}
move() {
const plantsDivs = document.getElementById('plants').children;
for (let i = 0; i < plantsDivs.length; i++) {
const plant = plantsDivs[i];
if (plant.classList.contains('plant') && !plant.classList.contains('sunflower')) {
this.target = plant;
break;
}
}
let dx = this.target.offsetLeft - this.element.offsetLeft;
let dy = this.target.offsetTop - this.element.offsetTop;
this.element.style.transform = `translate(${dx}px, ${dy}px)`;
}
eatPlant() {
if (this.target) {
this.target.takeDamage();
}
}
takeDamage() {
this.health--;
if (this.health === 0) {
this.die();
}
}
}
// Function to update the game state
function updateGame() {
if (plants.length === 0 && zombies.length) {
// Player lost
clearInterval(intervalID);
document.getElementById('message-text').textContent = 'Game Over!';
document.getElementById('message-box').style.display = 'flex';
} else if (progressBarWidth === 300) {
// Wave completed
clearInterval(intervalID);
currentWave++;
if (currentWave < waves.length) {
setupWave();
} else {
document.getElementById('message-text').textContent = 'Congratulations! You Win!';
}
document.getElementById('message-box').style.display = 'flex';
} else {
// Update progress bar
progressBarWidth = plants.length * 100 / waves[currentWave].plants.reduce((a, b) => a + b, 0);
document.querySelector('.progress-bar-fill').style.width = progressBarWidth + '%';
// Move zombies
zombies.forEach(zombie => {
zombie.move();
zombie.eatPlant();
});
// Check plant health and remove if necessary
plants.forEach((plant, index) => {
plant.element.addEventListener('animationend', () => {
if (plant.health === 0) {
plants.splice(index, 1);
}
});
});
// Remove dead zombies
zombies = zombies.filter(zombie => !zombie.remove);
}
}
// Function to setup the wave
function setupWave() {
// Clear the garden
document.getElementById('plants').innerHTML = '';
plants = [];
// Add sunflowers
for (let i