🎮ArcadeLab

Wave-Based Tower Defense

by PlasmaMeteor43
149 lines4.4 KB
▶ Play
!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Wave-Based Tower Defense</title>
<style>
    body { margin: 0; background: #222; color: white; font-family: sans-serif; }
    canvas { display: block; margin: auto; background: #333; }
    #info { text-align: center; padding: 10px; }
</style>
</head>
<body>
<div id="info">
    <h1>Wave-Based Tower Defense</h1>
    <p>Click on the canvas to place towers (Cost: 50 coins).</p>
    <p>Round: <span id="round">1</span> | Score: <span id="score">0</span> | Lives: <span id="lives">5</span> | Money: <span id="money">100</span></p>
</div>
<canvas id="gameCanvas" width="800" height="400"></canvas>

<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');

let score = 0;
let lives = 5;
let money = 100;
const towerCost = 50;
let round = 1;

// Enemy path points
const path = [
    {x: 0, y: 200},
    {x: 800, y: 200}
];

// Arrays
let enemies = [];
let towers = [];
let bullets = [];

// Spawn enemies every round
setInterval(() => {
    spawnEnemy();
    money += 20; // earn money each round
    document.getElementById('money').textContent = money;
    round++;
    document.getElementById('round').textContent = round;
}, 3000); // 3 seconds per round

function spawnEnemy() {
    // Enemy gets stronger each round
    const health = 3 + Math.floor(round * 0.5); // health increases
    const speed = 1 + round * 0.05; // speed increases
    enemies.push({x: path[0].x, y: path[0].y, speed: speed, health: health, reward: 10 + round});
}

// Tower placement
canvas.addEventListener('click', (e) => {
    if (money >= towerCost) {
        const rect = canvas.getBoundingClientRect();
        const x = e.clientX - rect.left;
        const y = e.clientY - rect.top;
        towers.push({x, y, range: 100, fireRate: 500, lastShot: 0});
        money -= towerCost;
        document.getElementById('money').textContent = money;
    } else {
        alert("Not enough money to place a tower!");
    }
});

// Game loop
function gameLoop(timestamp) {
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    // Draw path
    ctx.strokeStyle = 'yellow';
    ctx.beginPath();
    ctx.moveTo(path[0].x, path[0].y);
    ctx.lineTo(path[1].x, path[1].y);
    ctx.stroke();

    // Update enemies
    enemies.forEach((enemy, index) => {
        enemy.x += enemy.speed;
        ctx.fillStyle = 'red';
        ctx.fillRect(enemy.x - 10, enemy.y - 10, 20, 20);

        if (enemy.x >= path[1].x) {
            enemies.splice(index, 1);
            lives--;
            document.getElementById('lives').textContent = lives;
            if (lives <= 0) {
                alert("Game Over! Final Score: " + score);
                location.reload();
            }
        }
    });

    // Draw towers
    towers.forEach(tower => {
        ctx.fillStyle = 'blue';
        ctx.beginPath();
        ctx.arc(tower.x, tower.y, 10, 0, Math.PI * 2);
        ctx.fill();

        // Shoot bullets
        enemies.forEach(enemy => {
            const dist = Math.hypot(enemy.x - tower.x, enemy.y - tower.y);
            if (dist <= tower.range && timestamp - tower.lastShot > tower.fireRate) {
                bullets.push({x: tower.x, y: tower.y, target: enemy});
                tower.lastShot = timestamp;
            }
        });
    });

    // Update bullets
    bullets.forEach((bullet, index) => {
        const dx = bullet.target.x - bullet.x;
        const dy = bullet.target.y - bullet.y;
        const dist = Math.hypot(dx, dy);
        const speed = 4;
        bullet.x += dx / dist * speed;
        bullet.y += dy / dist * speed;

        ctx.fillStyle = 'white';
        ctx.beginPath();
        ctx.arc(bullet.x, bullet.y, 3, 0, Math.PI * 2);
        ctx.fill();

        if (dist < 5) {
            bullet.target.health--;
            if (bullet.target.health <= 0) {
                score += 10;
                money += bullet.target.reward; // reward increases with round
                document.getElementById('score').textContent = score;
                document.getElementById('money').textContent = money;
                enemies.splice(enemies.indexOf(bullet.target), 1);
            }
            bullets.splice(index, 1);
        }
    });

    requestAnimationFrame(gameLoop);
}

requestAnimationFrame(gameLoop);
</script>
</body>
</html>

Game Source: Wave-Based Tower Defense

Creator: PlasmaMeteor43

Libraries: none

Complexity: moderate (149 lines, 4.4 KB)

The full source code is displayed above on this page.

Remix Instructions

To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: wave-based-tower-defense-plasmameteor43" to link back to the original. Then publish at arcadelab.ai/publish.