Below is a simple implementation of a basic Tower Defense game using HTML, CSS, and JavaScript. For simplicity, I'll use plain JavaScript and no external libraries. This example will include a game canvas, basic tower placement, and enemy movement.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tower Defense Game</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
canvas {
border: 1px solid #000;
background-color: #fff;
}
.controls {
position: absolute;
top: 10px;
left: 10px;
}
button {
padding: 10px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="controls">
<button id="placeTower">Place Tower</button>
</div>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const placeTowerButton = document.getElementById('placeTower');
let isPlacingTower = false;
// Game variables
const towers = [];
const enemies = [];
const path = [[100, 50], [700, 50], [700, 550], [100, 550]]; // Enemy path
let gameInterval;
// Tower class
class Tower {
constructor(x, y) {
this.x = x;
this.y = y;
this.range = 100;
this.color = 'blue';
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, 10, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
// Draw range
ctx.beginPath();
ctx.arc(this.x, this.y, this.range, 0, Math.PI * 2);
ctx.strokeStyle = this.color;
ctx.stroke();
ctx.closePath();
}
}
// Enemy class
class Enemy {
constructor(x, y) {
this.x = x;
this.y = y;
this.speed = 1;
this.health = 100;
this.color = 'red';
this.pathIndex = 0;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, 10, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
}
move() {
const [targetX, targetY] = path[this.pathIndex];
const dx = targetX - this.x;
const dy = targetY - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
const speedX = (dx / distance) * this.speed;
const speedY = (dy / distance) * this.speed;
this.x += speedX;
this.y += speedY;
if (distance < this.speed) {
this.pathIndex++;
if (this.pathIndex >= path.length) {
// Enemy reached the end
enemies.splice(enemies.indexOf(this), 1);
}
}
}
}
// Draw path
function drawPath() {
ctx.beginPath();
ctx.strokeStyle = 'black';
ctx.lineWidth = 5;
for (let i = 0; i < path.length - 1; i++) {
const [x1, y1] = path[i];
const [x2, y2] = path[i + 1];
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
}
ctx.stroke();
ctx.closePath();
}
// Game loop
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPath();
// Draw and update towers
towers.forEach(tower => tower.draw());
// Draw and update enemies
enemies.forEach(enemy => {
enemy.draw();
enemy.move();
// Check for tower attacks
towers.forEach(tower => {
const dx = enemy.x - tower.x;
const dy = enemy.y - tower.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < tower.range) {
enemy.health -= 1; // Simple damage
if (enemy.health <= 0) {
enemies.splice(enemies.indexOf(enemy), 1);
}
}
});
});
// Add new enemies periodically
if (Math.random() < 0.02) {
enemies.push(new Enemy(path[0][0], path[0][1]));
}
requestAnimationFrame(gameLoop);
}
// Canvas click event for placing towers
canvas.addEventListener('click', (e) => {
if (isPlacingTower) {
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
towers.push(new Tower(x, y));
}
});
// Toggle tower placement
placeTowerButton.addEventListener('click', () => {
isPlacingTower = !isPlacingTower;
placeTowerButton.textContent = isPlacingTower ? 'Stop Placing' : 'Place Tower';
});
// Start the game
gameLoop();
</script>
</body>
</html>
Explanation:
-
HTML Structure:
- A canvas element for rendering the game.
- A button to toggle tower placement.
-
CSS:
- Basic styling for centering the canvas and positioning the controls.
-
JavaScript:
- Tower Class: Represents a tower with a position, range, and drawing method.
- Enemy Class: Represents an enemy with a position, health, and movement along a predefined path.
- Game Loop: Continuously updates and renders the game state, including drawing the path, towers, and enemies.
- Tower Placement: Clicking on the canvas places a tower if the placement mode is active.
- Enemy Movement: Enemies move along the path and take damage if within a tower's range.
This is a basic implementation and can be expanded with more features like different tower types, enemy waves, UI improvements, and more sophisticated gameplay mechanics.