Tds
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tower Defense Infinite</title>
<style>
*{box-sizing:border-box}
body{margin:0;background:#111827;color:white;font-family:Arial;overflow:hidden}
canvas{display:block;width:100vw;height:100vh}
.panel{background:#202d40;border:2px solid #53677f;border-radius:10px;padding:12px}
#top{position:absolute;top:8px;left:8px;right:8px;display:flex;justify-content:space-between;gap:8px;pointer-events:none}
#top .panel{pointer-events:auto}
#stats{font-size:13px;line-height:1.8}
#right{position:absolute;right:8px;top:80px;width:225px;max-height:75vh;overflow:auto}
#bottom{position:absolute;bottom:8px;left:50%;transform:translateX(-50%);display:flex;gap:6px;max-width:98%;overflow-x:auto}
button{background:#385d86;color:white;border:2px solid #7795b7;border-radius:7px;padding:9px;cursor:pointer;font-weight:bold;white-space:nowrap}
button:hover{background:#537eac}
button:disabled{opacity:.4}
.towerbtn{min-width:100px;display:flex;flex-direction:column;align-items:center;gap:4px}
.towerbtn span{font-size:12px}
#message{position:absolute;top:23%;left:50%;transform:translateX(-50%);background:#000c;padding:12px 20px;border-radius:8px;display:none;text-align:center}
#overlay{position:absolute;inset:0;background:#07111eeF;display:flex;align-items:center;justify-content:center;overflow:auto}
#overlay .box{width:min(95%,550px);max-height:95vh;overflow:auto;text-align:center}
h1{color:#ffd36a;font-size:35px}
h2{color:#ffd36a}
.mode{display:block;width:100%;margin:8px 0;padding:14px}
#upgradeButtons{display:grid;gap:7px;margin-top:10px}
#towerInfo{font-size:13px;line-height:1.7}
#bottom button.selected{border:3px solid #ffe18a;background:#705d34}
.small{font-size:11px;color:#cbd5e1}
@media(max-width:650px){
#right{width:175px;top:95px;font-size:12px}
#top{font-size:12px}
#bottom{width:100%;justify-content:flex-start}
.towerbtn{min-width:80px;padding:7px}
h1{font-size:26px}
}
</style>
</head>
<body>
<canvas id="game"></canvas>
<div id="top">
<div class="panel" id="stats">
<b style="color:#ffd36a">TOWER DEFENSE: INFINITE</b><br>
❤️ Lives: <span id="lives">100</span><br>
💰 Coins: <span id="coins">500</span><br>
🌊 Wave: <span id="wave">0</span><br>
🏆 Mode: <span id="modeName">Easy</span>
</div>
<div class="panel" style="text-align:right">
<button onclick="startWave()">START WAVE</button>
<button onclick="pauseGame()">⏯</button>
<button onclick="saveGame()">SAVE</button>
</div>
</div>
<div id="right" class="panel">
<h3 id="rightTitle">Tower Shop</h3>
<div id="towerInfo">Select a tower to place it on the battlefield. Protect the base!</div>
<div id="upgradeButtons"></div>
<button onclick="deselect()" style="width:100%;margin-top:8px">CLOSE / DESELECT</button>
</div>
<div id="bottom">
<button class="towerbtn" onclick="chooseTower('scout')" id="btn-scout">
👮 <b>Scout</b><span>$200</span>
</button>
<button class="towerbtn" onclick="chooseTower('sniper')" id="btn-sniper">
🎯 <b>Sniper</b><span>$350</span>
</button>
<button class="towerbtn" onclick="chooseTower('minigunner')" id="btn-minigunner">
🔫 <b>Minigunner</b><span>$600</span>
</button>
<button class="towerbtn" onclick="chooseTower('demolisher')" id="btn-demolisher">
💣 <b>Demolisher</b><span>$500</span>
</button>
<button class="towerbtn" onclick="chooseTower('commander')" id="btn-commander">
📢 <b>Commander</b><span>$750</span>
</button>
</div>
<div id="message"></div>
<div id="overlay">
<div class="box panel">
<h1>TOWER DEFENSE<br>INFINITE</h1>
<p>Build your defense. Survive every wave. Defeat the final boss.</p>
<h3>SELECT YOUR GAME MODE</h3>
<button class="mode" onclick="newGame('Easy')">🟢 EASY MODE<br><small>15 waves · Beginner enemies</small></button>
<button class="mode" onclick="newGame('Normal')">🔵 NORMAL MODE<br><small>20 waves · Stronger enemies</small></button>
<button class="mode" onclick="newGame('Hard')">🔴 HARD MODE<br><small>25 waves · Powerful enemies</small></button>
<button class="mode" onclick="newGame('Endless')">♾ ENDLESS MODE<br><small>Unlimited waves · Endless challenge</small></button>
<p class="small">Original offline tower defense game inspired by the tower defense genre.</p>
</div>
</div>
<script>
"use strict";
const canvas=document.getElementById("game");
const ctx=canvas.getContext("2d");
let W=innerWidth,H=innerHeight;
canvas.width=W;canvas.height=H;
window.addEventListener("resize",()=>{
W=innerWidth;H=innerHeight;
canvas.width=W;canvas.height=H;
});
const path=[
{x:.02,y:.42},{x:.20,y:.42},{x:.20,y:.76},
{x:.43,y:.76},{x:.43,y:.22},{x:.69,y:.22},
{x:.69,y:.62},{x:.91,y:.62},{x:.91,y:.42},
{x:.99,y:.42}
];
const definitions={
scout:{name:"Scout",cost:200,range:115,damage:12,rate:.65,color:"#4da2e8",desc:"Affordable and reliable starter tower."},
sniper:{name:"Sniper",cost:350,range:260,damage:45,rate:1.8,color:"#f1d45a",desc:"Long range and powerful single shots."},
minigunner:{name:"Minigunner",cost:600,range:140,damage:8,rate:.12,color:"#7ad77c",desc:"Rapid firing tower."},
demolisher:{name:"Demolisher",cost:500,range:125,damage:35,rate:1.4,color:"#f28b49",desc:"Explosive area damage."},
commander:{name:"Commander",cost:750,range:145,damage:0,rate:1,color:"#b58bff",desc:"Boosts nearby towers' attack speed."}
};
let towers=[],enemies=[],bullets=[],particles=[];
let coins=500,lives=100,wave=0;
let mode="Easy",maxWaves=15;
let selectedType=null,selectedTower=null;
let placing=false,running=false,paused=false;
let spawnQueue=0,spawnTimer=0,waveActive=false;
let gameOver=false,won=false;
let last=performance.now(),gameTime=0;
let towerId=0;
function point(p){
return {x:p.x*W,y:p.y*H};
}
function pathPoint(t){
let total=0,segments=[];
for(let i=0;i<path.length-1;i++){
let a=point(path[i]),b=point(path[i+1]);
let len=Math.hypot(b.x-a.x,b.y-a.y);
segments.push({a,b,len});
total+=len;
}
let target=t*total;
for(let s of segments){
if(target<=s.len){
let k=target/s.len;
return {x:s.a.x+(s.b.x-s.a.x)*k,y:s.a.y+(s.b.y-s.a.y)*k};
}
target-=s.len;
}
return point(path[path.length-1]);
}
function notify(text){
let el=document.getElementById("message");
el.textContent=text;
el.style.display="block";
clearTimeout(notify.timer);
notify.timer=setTimeout(()=>el.style.display="none",2200);
}
function updateUI(){
document.getElementById("lives").textContent=lives;
document.getElementById("coins").textContent=coins;
document.getElementById("wave").textContent=wave;
document.getElementById("modeName").textContent=mode;
for(let id in definitions){
let el=document.getElementById("btn-"+id);
el.classList.toggle("selected",selectedType===id);
el.disabled=coins<definitions[id].cost;
}
}
function newGame(selectedMode){
mode=selectedMode;
maxWaves={Easy:15,Normal:20,Hard:25,Endless:Infinity}[mode];
towers=[];enemies=[];bullets=[];particles=[];
coins=500;
lives=mode==="Easy"?100:mode==="Normal"?75:mode==="Hard"?50:100;
wave=0;
waveActive=false;
selectedType=null;
selectedTower=null;
gameOver=false;
won=false;
paused=false;
running=true;
gameTime=0;
towerId=0;
document.getElementById("overlay").style.display="none";
document.getElementById("rightTitle").textContent="Tower Shop";
document.getElementById("towerInfo").textContent="Select a tower to place it on the battlefield.";
document.getElementById("upgradeButtons").innerHTML="";
updateUI();
notify("Welcome! Build your defense!");
}
function startWave(){
if(!running||paused||gameOver||waveActive)return;
if(mode!=="Endless"&&wave>=maxWaves){
return;
}
wave++;
waveActive=true;
let multiplier=mode==="Easy"?1:mode==="Normal"?1.4:mode==="Hard"?1.9:1.15;
spawnQueue=Math.floor((5+wave*2)*multiplier);
spawnTimer=0;
notify("WAVE "+wave+" INCOMING!");
updateUI();
}
function enemyStats(){
let m=mode==="Easy"?1:mode==="Normal"?1.35:mode==="Hard"?1.8:1.2;
let types=["normal","normal","fast"];
if(wave>=3)types.push("tank");
if(wave>=5)types.push("fast");
if(wave%5===0)types.push("boss");
let type=types[Math.floor(Math.random()*types.length)];
if(wave%10===0&&spawnQueue===1)type="boss";
let stats={
normal:{hp:30,speed:.055,reward:15,color:"#7ed36e",size:13},
fast:{hp:20,speed:.105,reward:18,color:"#e8df61",size:10},
tank:{hp:120,speed:.027,reward:35,color:"#777d91",size:18},
boss:{hp:600,speed:.018,reward:150,color:"#9e4fd4",size:25}
}[type];
return {...stats,type,hp:stats.hp* (1+wave*.17)*m,maxHp:stats.hp*(1+wave*.17)*m,progress:0,alive:true};
}
function spawnEnemy(){
enemies.push(enemyStats());
}
function chooseTower(type){
if(!running||paused||gameOver)return;
if(coins<definitions[type].cost){
notify("Not enough coins!");
return;
}
selectedType=type;
selectedTower=null;
placing=true;
updateUI();
document.getElementById("rightTitle").textContent="Place "+definitions[type].name;
document.getElementById("towerInfo").textContent=definitions[type].desc+" Cost: "+definitions[type].cost;
document.getElementById("upgradeButtons").innerHTML="";
notify("Click an empty area away from the path to place.");
}
function distanceToSegment(p,a,b){
let dx=b.x-a.x,dy=b.y-a.y;
let t=Math.max(0,Math.min(1,((p.x-a.x)*dx+(p.y-a.y)*dy)/(dx*dx+dy*dy)));
return Math.hypot(p.x-(a.x+t*dx),p.y-(a.y+t*dy));
}
function onPath(x,y){
let p={x,y};
for(let i=0;i<path.length-1;i++){
if(distanceToSegment(p,point(path[i]),point(path[i+1]))<27)return true;
}
return false;
}
function placeTower(x,y){
if(!selectedType)return;
let d=definitions[selectedType];
if(coins<d.cost)return;
if(onPath(x,y)){
notify("You cannot place a tower on the road!");
return;
}
if(towers.some(t=>Math.hypot(t.x-x,t.y-y)<32)){
notify("Too close to another tower!");
return;
}
coins-=d.cost;
let tower={
id:++towerId,type:selectedType,x,y,
level:0,damage:d.damage,range:d.range,
rate:d.rate,cooldown:0,totalSpent:d.cost
};
towers.push(tower);
selectedTower=tower;
selectedType=null;
placing=false;
updateUI();
showTower(tower);
notify(d.name+" placed!");
}
function showTower(t){
selectedTower=t;
selectedType=null;
placing=false;
let d=definitions[t.type];
document.getElementById("rightTitle").textContent=d.name+" · Level "+t.level;
document.getElementById("towerInfo").innerHTML=
"Damage: "+t.damage+"<br>Range: "+Math.round(t.range)+
"<br>Fire rate: "+t.rate.toFixed(2)+"s<br>Total spent: "+t.totalSpent;
let box=document.getElementById("upgradeButtons");
box.innerHTML="";
if(t.level<4){
let cost=Math.floor(d.cost*(.65+t.level*.55));
let b=document.createElement("button");
b.textContent="⬆ UPGRADE · $"+cost;
b.disabled=coins<cost;
b.onclick=()=>upgrade(t);
box.appendChild(b);
}else{
let max=document.createElement("p");
max.textContent="MAX LEVEL REACHED";
box.appendChild(max);
}
let sell=document.createElement("button");
sell.textContent="SELL TOWER · $"+Math.floor(t.totalSpent*.65);
sell.onclick=()=>sellTower(t);
box.appendChild(sell);
updateUI();
}
function upgrade(t){
if(!t||t.level>=4)return;
let d=definitions[t.type];
let cost=Math.floor(d.cost*(.65+t.level*.55));
if(coins<cost){notify("Not enough coins!");return}
coins-=cost;
t.totalSpent+=cost;
t.level++;
t.damage=Math.round(t.damage*1.55);
t.range*=1.12;
t.rate*=.84;
showTower(t);
notify("Tower upgraded to level "+t.level+"!");
}
function sellTower(t){
coins+=Math.floor(t.totalSpent*.65);
towers=towers.filter(x=>x!==t);
selectedTower=null;
document.getElementById("rightTitle").textContent="Tower Shop";
document.getElementById("towerInfo").textContent="Select a tower to place.";
document.getElementById("upgradeButtons").innerHTML="";
updateUI();
}
function deselect(){
selectedTower=null;
selectedType=null;
placing=false;
document.getElementById("rightTitle").textContent="Tower Shop";
document.getElementById("towerInfo").textContent="Select a tower to place.";
document.getElementById("upgradeButtons").innerHTML="";
updateUI();
}
function fire(t,e){
t.cooldown=t.rate;
let dx=e.x-t.x,dy=e.y-t.y;
let dist=Math.hypot(dx,dy);
let damage=t.damage;
if(t.type==="commander")return;
bullets.push({
x:t.x,y:t.y,target:e,
damage:damage,
speed:t.type==="sniper"?850:650,
explosive:t.type==="demolisher"
});
}
function updateTowers(dt){
towers.forEach(t=>{
t.cooldown-=dt;
if(t.type==="commander")return;
if(t.cooldown>0)return;
let target=null,best=-1;
enemies.forEach(e=>{
if(!e.alive)return;
let d=Math.hypot(e.x-t.x,e.y-t.y);
if(d<=t.range&&e.progress>best){
best=e.progress;
target=e;
}
});
if(target)fire(t,target);
});
}
function commanderBoost(t){
let boosted=towers.some(c=>c.type==="commander"&&
Math.hypot(c.x-t.x,c.y-t.y)<c.range&&c!==t);
return boosted?.7:1;
}
function updateBullets(dt){
bullets.forEach(b=>{
if(!b.target.alive){b.dead=true;return}
let dx=b.target.x-b.x,dy=b.target.y-b.y;
let d=Math.hypot(dx,dy);
let step=b.speed*dt;
if(d<=step+5){
b.target.hp-=b.damage;
if(b.explosive){
enemies.forEach(e=>{
if(e.alive&&Math.hypot(e.x-b.target.x,e.y-b.target.y)<65)e.hp-=b.damage*.65;
});
for(let i=0;i<10;i++)particles.push({
x:b.target.x,y:b.target.y,vx:(Math.random()-.5)*120,vy:(Math.random()-.5)*120,life:.4
});
}
if(b.target.hp<=0){
b.target.alive=false;
coins+=b.target.reward;
}
b.dead=true;
}else{
b.x+=dx/d*step;
b.y+=dy/d*step;
}
});
bullets=bullets.filter(b=>!b.dead);
}
function updateEnemies(dt){
enemies.forEach(e=>{
if(!e.alive)return;
let next=Math.min(.999,e.progress+e.speed*dt);
let p=pathPoint(next);
e.progress=next;
e.x=p.x;e.y=p.y;
if(e.progress>=.999){
e.alive=false;
lives-=e.type==="boss"?15:e.type==="tank"?5:1;
if(lives<=0){
lives=0;
endGame(false);
}
}
});
enemies=enemies.filter(e=>e.alive);
}
function endGame(victory){
gameOver=true;
running=false;
won=victory;
document.getElementById("overlay").style.display="flex";
document.querySelector("#overlay .box").innerHTML=
"<h1>"+(victory?"VICTORY!":"GAME OVER")+"</h1>"+
"<h2>"+(victory?"You defended the base!":"Your base was destroyed.")+"</h2>"+
"<p>Mode: "+mode+"</p><p>Waves survived: "+wave+"</p>"+
"<p>Coins remaining: "+coins+"</p>"+
'<button class="mode" onclick="newGame(\\''+mode+'\\')">PLAY AGAIN</button>'+
'<button class="mode" onclick="location.reload()">MAIN MENU</button>';
}
function update(dt){
if(!running||paused||gameOver)return;
gameTime+=dt;
if(waveActive){
spawnTimer-=dt;
if(spawnQueue>0&&spawnTimer<=0){
spawnEnemy();
spawnQueue--;
spawnTimer=Math.max(.25,.9-wave*.015);
}
if(spawnQueue===0&&enemies.length===0){
waveActive=false;
coins+=50+wave*5;
notify("Wave "+wave+" completed! Bonus coins awarded.");
if(mode!=="Endless"&&wave>=maxWaves){
endGame(true);
}
}
}
updateEnemies(dt);
updateTowers(dt);
updateBullets(dt);
particles.forEach(p=>{
p.x+=p.vx*dt;p.y+=p.vy*dt;p.life-=dt;
});
particles=particles.filter(p=>p.life>0);
// Commander attack speed aura
towers.forEach(t=>{
if(t.type!=="commander")return;
towers.forEach(other=>{
if(other!==t&&Math.hypot(other.x-t.x,other.y-t.y)<t.range){
other.cooldown-=dt*.15;
}
});
});
updateUI();
}
function drawPath(){
ctx.lineCap="round";
ctx.lineJoin="round";
ctx.beginPath();
path.forEach((p,i)=>{
let q=point(p);
if(i===0)ctx.moveTo(q.x,q.y);
else ctx.lineTo(q.x,q.y);
});
ctx.strokeStyle="#8b6844";
ctx.lineWidth=58;
ctx.stroke();
ctx.strokeStyle="#c4a071";
ctx.lineWidth=43;
ctx.stroke();
ctx.setLineDash([12,14]);
ctx.strokeStyle="#e5c69a";
ctx.lineWidth=2;
ctx.stroke();
ctx.setLineDash([]);
}
function drawBackground(){
ctx.fillStyle="#567b49";
ctx.fillRect(0,0,W,H);
// Grass patterns
for(let i=0;i<180;i++){
let x=(i*137.5)%W,y=(i*83.3)%H;
ctx.fillStyle=i%2?"#638d50":"#4c7040";
ctx.fillRect(x,y,3,3);
}
drawPath();
let base=point(path[path.length-1]);
ctx.fillStyle="#5e83ad";
ctx.fillRect(base.x-32,base.y-40,64,80);
ctx.fillStyle="#c3e3ff";
ctx.font="bold 14px Arial";
ctx.textAlign="center";
ctx.fillText("BASE",base.x,base.y+5);
}
function drawTower(t){
let d=definitions[t.type];
ctx.save();
ctx.translate(t.x,t.y);
if(selectedTower===t){
ctx.beginPath();
ctx.arc(0,0,t.range,0,Math.PI*2);
ctx.fillStyle="#ffffff12";
ctx.fill();
ctx.strokeStyle="#ffffff66";
ctx.lineWidth=2;
ctx.stroke();
}
ctx.fillStyle="#333";
ctx.beginPath();ctx.arc(0,5,19,0,Math.PI*2);ctx.fill();
ctx.fillStyle=d.color;
ctx.fillRect(-13,-19,26,28);
ctx.fillStyle="#e5e7eb";
ctx.fillRect(-6,-28,12,12);
if(t.type==="sniper"){
ctx.strokeStyle="#333";ctx.lineWidth=5;
ctx.beginPath();ctx.moveTo(0,-10);ctx.lineTo(24,-24);ctx.stroke();
}else if(t.type==="minigunner"){
ctx.fillStyle="#222";ctx.fillRect(5,-15,24,5);
}else if(t.type==="demolisher"){
ctx.fillStyle="#333";ctx.fillRect(-17,-24,34,8);
}else if(t.type==="commander"){
ctx.fillStyle="#eee";ctx.fillRect(-2,-39,4,16);
ctx.fillStyle="#b58bff";ctx.fillRect(2,-38,12,8);
}else{
ctx.fillStyle="#333";ctx.fillRect(5,-13,19,6);
}
ctx.fillStyle="white";
ctx.font="bold 10px Arial";
ctx.textAlign="center";
ctx.fillText("Lv."+t.level,0,32);
ctx.restore();
}
function drawEnemy(e){
ctx.save();
ctx.translate(e.x,e.y);
ctx.fillStyle=e.color;
ctx.beginPath();ctx.arc(0,0,e.size,0,Math.PI*2);ctx.fill();
ctx.fillStyle="#222";
ctx.fillRect(-e.size*.5,-e.size*.25,4,4);
ctx.fillRect(e.size*.2,-e.size*.25,4,4);
ctx.fillStyle="#222";
ctx.fillRect(-e.size,-e.size-8,e.size*2,5);
ctx.fillStyle="#e33";
ctx.fillRect(-e.size,-e.size-8,e.size*2*(e.hp/e.maxHp),5);
ctx.restore();
}
function draw(){
drawBackground();
towers.forEach(drawTower);
enemies.forEach(drawEnemy);
bullets.forEach(b=>{
ctx.fillStyle=b.explosive?"#ff8c36":"#fff3a6";
ctx.beginPath();ctx.arc(b.x,b.y,5,0,Math.PI*2);ctx.fill();
});
particles.forEach(p=>{
ctx.fillStyle="#ffd36a";
ctx.globalAlpha=Math.max(0,p.life/.4);
ctx.fillRect(p.x,p.y,5,5);
ctx.globalAlpha=1;
});
if(placing&&mouse.x){
ctx.globalAlpha=.5;
ctx.fillStyle=definitions[selectedType].color;
ctx.beginPath();ctx.arc(mouse.x,mouse.y,16,0,Math.PI*2);ctx.fill();
ctx.globalAlpha=1;
}
}
let mouse={x:0,y:0};
canvas.addEventListener("mousemove",e=>{
let r=canvas.getBoundingClientRect();
mouse.x=(e.clientX-r.left)*W/r.width;
mouse.y=(e.clientY-r.top)*H/r.height;
});
canvas.addEventListener("click",e=>{
if(!running||paused||gameOver)return;
let r=canvas.getBoundingClientRect();
let x=(e.clientX-r.left)*W/r.width;
let y=(e.clientY-r.top)*H/r.height;
if(placing){
placeTower(x,y);
return;
}
let found=[...towers].reverse().find(t=>Math.hypot(t.x-x,t.y-y)<24);
if(found){showTower(found);return}
deselect();
});
function pauseGame(){
if(!running)return;
paused=!paused;
notify(paused?"Game paused":"Game resumed");
}
function saveGame(){
try{
localStorage.setItem("tdInfiniteSave",JSON.stringify({
towers,enemies,coins,lives,wave,mode
}));
notify("Game saved!");
}catch(e){notify("Unable to save.");}
}
function loadGame(){
try{
let d=JSON.parse(localStorage.getItem("tdInfiniteSave"));
if(!d){notify("No save found!");return}
towers=d.towers||[];
enemies=d.enemies||[];
coins=d.coins;lives=d.lives;wave=d.wave;mode=d.mode;
maxWaves={Easy:15,Normal:20,Hard:25,Endless:Infinity}[mode];
running=true;gameOver=false;paused=false;
waveActive=enemies.length>0;
document.getElementById("overlay").style.display="none";
updateUI();
notify("World loaded!");
}catch(e){notify("Save could not be loaded.");}
}
function loop(now){
let dt=Math.min((now-last)/1000,.05);
last=now;
update(dt);
draw();
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
</script>
</body>
</html>
Featured
Advertisement
Category
Action