Tower complete

Text Views: 23 • Nov 04, 2026 • Never expires
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tower Defense Infinite</title>
<style>
*{box-sizing:border-box}
body{margin:0;background:#101827;color:white;font-family:Arial;text-align:center}
header{background:#18263b;padding:12px}
h1{font-size:24px;margin:5px}
#stats{display:flex;justify-content:center;gap:18px;flex-wrap:wrap;margin:12px}
button,select{background:#263c58;color:white;border:1px solid #607b9c;border-radius:7px;padding:10px;margin:4px;font-weight:bold;cursor:pointer}
button:disabled{opacity:.4;cursor:not-allowed}
#speedBtn{background:#176b45}
#game{width:96%;max-width:900px;height:auto;display:block;margin:10px auto;border:3px solid #52677e;border-radius:8px;touch-action:manipulation}
#towers{max-width:950px;margin:10px auto}
#panel{background:#18263b;max-width:900px;margin:10px auto;padding:12px;border-radius:8px;min-height:100px}
#message{color:#ffe28a;min-height:24px;margin:8px}
.small{font-size:12px;color:#b8c9dc}
</style>
</head>
<body>

<header>
<h1>🏰 TOWER DEFENSE: INFINITE</h1>
<div id="stats">
<span>💰 Coins: <b id="coins">1000</b></span>
<span>❤️ Lives: <b id="lives">100</b></span>
<span>🌊 Wave: <b id="wave">0</b></span>
<span>👾 Enemies: <b id="enemies">0</b></span>
</div>
</header>

<div>
<select id="mode">
<option value="easy">Easy</option>
<option value="normal" selected>Normal</option>
<option value="hard">Hard</option>
<option value="endless">Endless</option>
</select>
<button onclick="startGame()">Start Game</button>
<button onclick="startWave()">Start Wave</button>
<button onclick="togglePause()" id="pauseBtn">Pause</button>
<button onclick="toggleSpeed()" id="speedBtn">⏩ 2× SPEED: OFF</button>
<button onclick="resetGame()">Restart</button>
</div>

<div id="towers">
<button onclick="selectTower('scout')">🔫 Scout ($200)</button>
<button onclick="selectTower('sniper')">🎯 Sniper ($350)</button>
<button onclick="selectTower('mini')">⚡ Minigunner ($500)</button>
<button onclick="selectTower('demo')">💥 Demolisher ($650)</button>
<button onclick="selectTower('commander')">📢 Commander ($700)</button>
<button onclick="selectTower('flame')">🔥 Flamethrower ($800)</button>
<button onclick="selectTower('freezer')">❄️ Freezer ($750)</button>
<button onclick="selectTower('ranger')">🏹 Ranger ($900)</button>
<button onclick="selectTower('farm')">💰 Farm ($500)</button>
</div>

<canvas id="game" width="900" height="520"></canvas>

<div id="panel">
<b id="panelTitle">Select a tower</b>
<p id="panelInfo" class="small">Choose a tower and tap an empty grass area to place it.</p>
<button id="upgradeBtn" onclick="upgradeTower()" disabled>Upgrade</button>
<button id="sellBtn" onclick="sellTower()" disabled>Sell</button>
</div>

<div id="message">Press Start Game to begin!</div>
<p class="small">Select a tower, then tap the map to place it. Tap placed towers to upgrade or sell them.</p>

<script>
"use strict";

const canvas=document.getElementById("game");
const ctx=canvas.getContext("2d");

const path=[
{x:0,y:260},{x:160,y:260},{x:160,y:100},
{x:380,y:100},{x:380,y:410},{x:620,y:410},
{x:620,y:210},{x:900,y:210}
];

const towerData={
scout:{name:"Scout",cost:200,damage:15,range:125,rate:.8,color:"#4b9cff",upgrade:150},
sniper:{name:"Sniper",cost:350,damage:45,range:260,rate:2,color:"#eeeeee",upgrade:250},
mini:{name:"Minigunner",cost:500,damage:9,range:145,rate:.15,color:"#e5b642",upgrade:300},
demo:{name:"Demolisher",cost:650,damage:35,range:115,rate:1.4,color:"#ed7547",upgrade:350},
commander:{name:"Commander",cost:700,damage:0,range:170,rate:0,color:"#b77aff",upgrade:400},
flame:{name:"Flamethrower",cost:800,damage:7,range:95,rate:.2,color:"#ff672d",upgrade:400},
freezer:{name:"Freezer",cost:750,damage:5,range:140,rate:.8,color:"#65e6ff",upgrade:375},
ranger:{name:"Ranger",cost:900,damage:100,range:290,rate:2.5,color:"#39c77b",upgrade:500},
farm:{name:"Farm",cost:500,damage:0,range:0,rate:0,color:"#d9bd42",upgrade:300}
};

let coins=1000,lives=100,wave=0;
let towers=[],enemies=[],selected=null,placing=null;
let gameStarted=false,waveActive=false,paused=false,gameOver=false;
let spawnQueue=[],spawnTimer=0,lastTime=0;
let mode="normal",speed=1;

function message(s){
 document.getElementById("message").textContent=s;
}

function updateUI(){
 document.getElementById("coins").textContent=Math.floor(coins);
 document.getElementById("lives").textContent=lives;
 document.getElementById("wave").textContent=wave;
 document.getElementById("enemies").textContent=enemies.length;

 document.querySelectorAll("#towers button").forEach(btn=>{
  const key=btn.getAttribute("onclick").match(/'([^']+)'/)[1];
  btn.disabled=coins<towerData[key].cost||gameOver;
 });

 if(selected){
  const d=towerData[selected.type];
  document.getElementById("panelTitle").textContent=d.name+" - Level "+selected.level;
  document.getElementById("panelInfo").textContent=
   "Damage: "+selected.damage+" | Range: "+selected.range+
   " | Upgrade: $"+(d.upgrade*selected.level);
  document.getElementById("upgradeBtn").disabled=
   coins<d.upgrade*selected.level||selected.level>=5;
  document.getElementById("sellBtn").disabled=false;
 }else{
  document.getElementById("panelTitle").textContent="Select a tower";
  document.getElementById("panelInfo").textContent=placing?
   "Tap an empty grass area to place "+towerData[placing].name:
   "Choose a tower and tap an empty grass area to place it.";
  document.getElementById("upgradeBtn").disabled=true;
  document.getElementById("sellBtn").disabled=true;
 }
}

function toggleSpeed(){
 speed=speed===1?2:1;
 document.getElementById("speedBtn").textContent=
  speed===2?"⏩ 2× SPEED: ON":"⏩ 2× SPEED: OFF";
}

function selectTower(type){
 if(!gameStarted||gameOver){
  message("Press Start Game first!");
  return;
 }
 if(coins<towerData[type].cost){
  message("Not enough coins!");
  return;
 }
 placing=type;
 selected=null;
 updateUI();
 message("Tap an empty area to place "+towerData[type].name+".");
}

function startGame(){
 resetGame();
 mode=document.getElementById("mode").value;
 message("Game started! Place towers and start a wave.");
}

function resetGame(){
 coins=1000;
 lives=100;
 wave=0;
 towers=[];
 enemies=[];
 selected=null;
 placing=null;
 spawnQueue=[];
 spawnTimer=0;
 waveActive=false;
 paused=false;
 gameOver=false;
 gameStarted=true;
 speed=1;
 document.getElementById("speedBtn").textContent="⏩ 2× SPEED: OFF";
 document.getElementById("pauseBtn").textContent="Pause";
 updateUI();
 message("New game! Place your towers.");
}

function startWave(){
 if(!gameStarted||gameOver){
  message("Start a new game first!");
  return;
 }
 if(waveActive){
  message("A wave is already running!");
  return;
 }

 wave++;
 waveActive=true;

 let amount=5+wave*2;
 if(mode==="easy")amount=4+wave;
 if(mode==="hard")amount=7+wave*3;
 if(mode==="endless")amount=6+wave*3;

 spawnQueue=[];
 for(let i=0;i<amount;i++){
  let type="normal";
  if(wave>=2&&i%4===0)type="fast";
  if(wave>=4&&i%5===0)type="tank";
  if(wave%5===0&&i===amount-1)type="boss";
  spawnQueue.push(type);
 }
 spawnTimer=0;
 message("Wave "+wave+" started!");
 updateUI();
}

function togglePause(){
 if(!gameStarted||gameOver)return;
 paused=!paused;
 document.getElementById("pauseBtn").textContent=paused?"Resume":"Pause";
 message(paused?"Game paused.":"Game resumed.");
}

function enemyStats(type){
 const difficulty=mode==="easy"?.8:mode==="hard"?1.5:1;
 if(type==="fast")return {hp:35*wave*difficulty,speed:105,reward:20,radius:10,color:"#ffe45c"};
 if(type==="tank")return {hp:130*wave*difficulty,speed:35,reward:35,radius:16,color:"#a65b38"};
 if(type==="boss")return {hp:500*wave*difficulty,speed:25,reward:100,radius:22,color:"#a53cba"};
 return {hp:55*wave*difficulty,speed:65,reward:12,radius:12,color:"#e94d4d"};
}

function spawnEnemy(type){
 const s=enemyStats(type);
 enemies.push({
  type:type,hp:s.hp,maxHp:s.hp,speed:s.speed,
  reward:s.reward,radius:s.radius,color:s.color,
  segment:0,progress:0,x:path[0].x,y:path[0].y,
  slowTimer:0,burnTimer:0,burnDamage:0
 });
}

function onPath(x,y){
 for(let i=0;i<path.length-1;i++){
  const a=path[i],b=path[i+1];
  const dx=b.x-a.x,dy=b.y-a.y;
  const length=dx*dx+dy*dy;
  let t=((x-a.x)*dx+(y-a.y)*dy)/length;
  t=Math.max(0,Math.min(1,t));
  const px=a.x+t*dx,py=a.y+t*dy;
  if(Math.hypot(x-px,y-py)<38)return true;
 }
 return false;
}

function placeTower(x,y){
 if(!placing)return;
 const d=towerData[placing];

 if(coins<d.cost){
  message("Not enough coins!");
  return;
 }
 if(onPath(x,y)){
  message("You cannot place a tower on the road!");
  return;
 }
 if(towers.some(t=>Math.hypot(t.x-x,t.y-y)<35)){
  message("Too close to another tower!");
  return;
 }

 coins-=d.cost;
 const t={
  type:placing,x:x,y:y,level:1,
  damage:d.damage,range:d.range,rate:d.rate,
  cooldown:0,incomeTimer:5
 };
 towers.push(t);
 selected=t;
 placing=null;
 updateUI();
 message(d.name+" placed!");
}

canvas.addEventListener("click",function(event){
 if(!gameStarted||gameOver)return;

 const rect=canvas.getBoundingClientRect();
 const x=(event.clientX-rect.left)*canvas.width/rect.width;
 const y=(event.clientY-rect.top)*canvas.height/rect.height;

 if(placing){
  placeTower(x,y);
  return;
 }

 selected=null;
 for(let i=towers.length-1;i>=0;i--){
  if(Math.hypot(x-towers[i].x,y-towers[i].y)<23){
   selected=towers[i];
   break;
  }
 }
 updateUI();
});

function upgradeTower(){
 if(!selected)return;
 const d=towerData[selected.type];
 const cost=d.upgrade*selected.level;

 if(selected.level>=5||coins<cost)return;

 coins-=cost;
 selected.level++;
 selected.damage=Math.round(selected.damage*1.5);
 selected.range+=12;
 selected.rate=selected.rate>0?selected.rate*.88:0;
 selected.incomeTimer=Math.max(.5,selected.incomeTimer);
 updateUI();
 message(d.name+" upgraded to level "+selected.level+"!");
}

function sellTower(){
 if(!selected)return;
 coins+=Math.floor(towerData[selected.type].cost*.6);
 towers=towers.filter(t=>t!==selected);
 selected=null;
 updateUI();
 message("Tower sold!");
}

function update(dt){
 if(!gameStarted||paused||gameOver)return;

 // 2x speed affects the entire simulation.
 dt*=speed;

 // Spawn enemies.
 if(waveActive&&spawnQueue.length>0){
  spawnTimer-=dt;
  if(spawnTimer<=0){
   spawnEnemy(spawnQueue.shift());
   spawnTimer=.75;
  }
 }

 // Move enemies and apply effects.
 for(let i=enemies.length-1;i>=0;i--){
  const e=enemies[i];

  if(e.slowTimer>0)e.slowTimer-=dt;
  if(e.burnTimer>0){
   e.burnTimer-=dt;
   e.hp-=e.burnDamage*dt;
  }

  let enemySpeed=e.speed*(e.slowTimer>0?.5:1);
  let distance=enemySpeed*dt;

  while(distance>0&&e.segment<path.length-1){
   const a=path[e.segment],b=path[e.segment+1];
   const length=Math.hypot(b.x-a.x,b.y-a.y);
   const remaining=length-e.progress;
   const step=Math.min(distance,remaining);
   e.progress+=step;
   distance-=step;

   const ratio=e.progress/length;
   e.x=a.x+(b.x-a.x)*ratio;
   e.y=a.y+(b.y-a.y)*ratio;

   if(e.progress>=length){
    e.segment++;
    e.progress=0;
   }
  }

  if(e.segment>=path.length-1){
   enemies.splice(i,1);
   lives-=e.type==="boss"?20:e.type==="tank"?8:5;
   if(lives<=0){
    lives=0;
    endGame();
   }
  }
 }

 // Towers attack.
 for(const t of towers){
  if(t.type==="farm"){
   t.incomeTimer-=dt;
   if(t.incomeTimer<=0){
    const income=50*t.level;
    coins+=income;
    t.incomeTimer=5;
    message("Farm earned $"+income+"!");
   }
   continue;
  }

  if(t.cooldown>0)t.cooldown-=dt;
  if(t.type==="commander"||t.cooldown>0)continue;

  let target=null;
  let best=-1;

  for(const e of enemies){
   const dist=Math.hypot(e.x-t.x,e.y-t.y);
   const progress=e.segment+e.progress/1000;
   if(dist<=t.range&&progress>best){
    target=e;
    best=progress;
   }
  }

  if(!target)continue;

  if(t.type==="demo"){
   for(const e of enemies){
    if(Math.hypot(e.x-target.x,e.y-target.y)<55){
     e.hp-=t.damage;
    }
   }
  }
  else if(t.type==="flame"){
   for(const e of enemies){
    if(Math.hypot(e.x-t.x,e.y-t.y)<=t.range){
     e.hp-=t.damage;
     e.burnTimer=2;
     e.burnDamage=3*t.level;
    }
   }
  }
  else if(t.type==="freezer"){
   target.hp-=t.damage;
   target.slowTimer=2;
  }
  else{
   target.hp-=t.damage;
  }

  t.cooldown=t.rate;
 }

 // Commander boosts nearby towers.
 for(const c of towers){
  if(c.type!=="commander")continue;
  for(const t of towers){
   if(t!==c&&t.type!=="farm"&&Math.hypot(t.x-c.x,t.y-c.y)<c.range){
    t.cooldown=Math.max(0,t.cooldown-dt*.35);
   }
  }
 }

 // Remove defeated enemies.
 for(let i=enemies.length-1;i>=0;i--){
  if(enemies[i].hp<=0){
   coins+=enemies[i].reward;
   enemies.splice(i,1);
  }
 }

 // Finish wave.
 if(waveActive&&spawnQueue.length===0&&enemies.length===0){
  waveActive=false;
  const bonus=50+wave*10;
  coins+=bonus;
  message("Wave "+wave+" completed! Bonus: $"+bonus);
 }

 updateUI();
}

function endGame(){
 gameOver=true;
 waveActive=false;
 message("GAME OVER! You ran out of lives. Press Restart.");
}

function drawBackground(){
 ctx.fillStyle="#75a85a";
 ctx.fillRect(0,0,900,520);

 ctx.fillStyle="rgba(255,255,255,.07)";
 for(let x=15;x<900;x+=45){
  for(let y=15;y<520;y+=45){
   ctx.fillRect(x,y,3,3);
  }
 }

 ctx.lineCap="round";
 ctx.lineJoin="round";
 ctx.strokeStyle="#b9a17a";
 ctx.lineWidth=76;
 ctx.beginPath();
 ctx.moveTo(path[0].x,path[0].y);
 for(let i=1;i<path.length;i++)ctx.lineTo(path[i].x,path[i].y);
 ctx.stroke();

 ctx.strokeStyle="#d7c49d";
 ctx.lineWidth=3;
 ctx.setLineDash([15,15]);
 ctx.beginPath();
 ctx.moveTo(path[0].x,path[0].y);
 for(let i=1;i<path.length;i++)ctx.lineTo(path[i].x,path[i].y);
 ctx.stroke();
 ctx.setLineDash([]);

 ctx.fillStyle="#fff";
 ctx.font="bold 15px Arial";
 ctx.fillText("START",10,225);
 ctx.fillText("END",850,175);
}

function drawTower(t){
 const d=towerData[t.type];

 if(selected===t&&t.range>0){
  ctx.beginPath();
  ctx.arc(t.x,t.y,t.range,0,Math.PI*2);
  ctx.fillStyle="rgba(255,255,255,.12)";
  ctx.fill();
  ctx.strokeStyle="#fff";
  ctx.lineWidth=1;
  ctx.stroke();
 }

 ctx.fillStyle="#263747";
 ctx.beginPath();
 ctx.arc(t.x,t.y,19,0,Math.PI*2);
 ctx.fill();

 ctx.fillStyle=d.color;
 ctx.beginPath();
 ctx.arc(t.x,t.y,14,0,Math.PI*2);
 ctx.fill();

 const symbols={
  scout:"S",sniper:"N",mini:"M",demo:"D",
  commander:"C",flame:"F",freezer:"I",
  ranger:"R",farm:"$"
 };

 ctx.fillStyle="#fff";
 ctx.font="bold 12px Arial";
 ctx.textAlign="center";
 ctx.fillText(symbols[t.type],t.x,t.y+4);
 ctx.font="10px Arial";
 ctx.fillText("Lv."+t.level,t.x,t.y+29);
 ctx.textAlign="left";
}

function drawEnemies(){
 for(const e of enemies){
  ctx.fillStyle=e.burnTimer>0?"#ff8b36":e.slowTimer>0?"#72eaff":e.color;
  ctx.beginPath();
  ctx.arc(e.x,e.y,e.radius,0,Math.PI*2);
  ctx.fill();

  const width=e.radius*2.2;
  ctx.fillStyle="#4b2525";
  ctx.fillRect(e.x-width/2,e.y-e.radius-9,width,4);
  ctx.fillStyle="#45e36a";
  ctx.fillRect(e.x-width/2,e.y-e.radius-9,
   width*Math.max(0,e.hp/e.maxHp),4);
 }
}

function draw(){
 drawBackground();

 for(const t of towers)drawTower(t);
 drawEnemies();

 if(placing){
  ctx.fillStyle="white";
  ctx.font="bold 16px Arial";
  ctx.fillText("PLACEMENT MODE: "+towerData[placing].name,20,30);
 }

 if(speed===2){
  ctx.fillStyle="#ffe28a";
  ctx.font="bold 17px Arial";
  ctx.fillText("2× SPEED",760,35);
 }

 if(paused||gameOver){
  ctx.fillStyle="rgba(0,0,0,.6)";
  ctx.fillRect(0,0,900,520);
  ctx.fillStyle="white";
  ctx.font="bold 42px Arial";
  ctx.textAlign="center";
  ctx.fillText(paused?"PAUSED":"GAME OVER",450,250);
  ctx.textAlign="left";
 }
}

function gameLoop(timestamp){
 const dt=Math.min((timestamp-lastTime)/1000,.05);
 lastTime=timestamp;
 update(dt);
 draw();
 requestAnimationFrame(gameLoop);
}

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