TOWER
<!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:#101827;color:white;font-family:Arial,sans-serif;text-align:center}
header{padding:12px;background:#18263b}
h1{margin:5px;font-size:25px}
#stats{display:flex;justify-content:center;gap:20px;flex-wrap:wrap;margin:12px}
#game{width:min(96vw,900px);height:auto;border:3px solid #52677e;border-radius:8px;background:#75a85a;display:block;margin:auto;touch-action:manipulation}
button,select{background:#263c58;color:white;border:1px solid #607b9c;border-radius:7px;padding:10px;margin:4px;font-weight:bold}
button:active{background:#42668e}
button:disabled{opacity:.4}
#towers{max-width:900px;margin:10px auto}
#panel{background:#18263b;max-width:900px;margin:10px auto;padding:12px;border-radius:8px;min-height:80px}
#message{color:#ffe28a;min-height:22px;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">500</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>
<label>Game Mode: </label>
<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="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>
</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 above, then 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">Tap a tower button, then tap the map to place it. Tap a placed tower to upgrade or sell it.</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:"#e4e4e4",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}
};
let coins=500,lives=100,wave=0;
let towers=[],enemies=[],selected=null;
let placing=null,gameStarted=false,waveActive=false,paused=false,gameOver=false;
let spawnQueue=[],spawnTimer=0,lastTime=0;
let mode="normal";
function message(text){
document.getElementById("message").textContent=text;
}
function updateUI(){
document.getElementById("coins").textContent=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 cost: $"+(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 ? "Now tap an empty grass area to place your "+towerData[placing].name+"."
: "Choose a tower above, then tap an empty grass area to place it.";
document.getElementById("upgradeBtn").disabled=true;
document.getElementById("sellBtn").disabled=true;
}
}
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 grass area to place "+towerData[type].name+".");
}
function startGame(){
resetGame();
gameStarted=true;
mode=document.getElementById("mode").value;
message("Game started! Place your towers and start the first wave.");
updateUI();
}
function resetGame(){
coins=500;
lives=100;
wave=0;
towers=[];
enemies=[];
selected=null;
placing=null;
spawnQueue=[];
spawnTimer=0;
waveActive=false;
paused=false;
gameOver=false;
gameStarted=true;
document.getElementById("pauseBtn").textContent="Pause";
updateUI();
message("New game started! Place your towers.");
}
function startWave(){
if(!gameStarted || gameOver){
message("Start a new game first!");
return;
}
if(waveActive){
message("A wave is already in progress!");
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 stats=enemyStats(type);
enemies.push({
type:type,hp:stats.hp,maxHp:stats.hp,
speed:stats.speed,reward:stats.reward,
radius:stats.radius,color:stats.color,
segment:0,progress:0,x:path[0].x,y:path[0].y
});
}
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 tower={
type:placing,x:x,y:y,level:1,
damage:d.damage,range:d.range,rate:d.rate,
cooldown:0
};
towers.push(tower);
selected=tower;
placing=null;
updateUI();
message(d.name+" placed!");
}
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;
}
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--){
const t=towers[i];
if(Math.hypot(x-t.x,y-t.y)<23){
selected=t;
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.55);
selected.range+=12;
selected.rate=selected.rate>0?selected.rate*.88:0;
updateUI();
message(selected.type+" upgraded to level "+selected.level+"!");
}
function sellTower(){
if(!selected)return;
const d=towerData[selected.type];
coins+=Math.floor(d.cost*.6);
towers=towers.filter(t=>t!==selected);
selected=null;
updateUI();
message("Tower sold!");
}
function update(dt){
if(!gameStarted || paused || gameOver)return;
if(waveActive && spawnQueue.length>0){
spawnTimer-=dt;
if(spawnTimer<=0){
spawnEnemy(spawnQueue.shift());
spawnTimer=.75;
}
}
// Move enemies along the road.
for(let i=enemies.length-1;i>=0;i--){
const e=enemies[i];
let distance=e.speed*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(false);
}
}
}
// Towers attack enemies.
for(const t of towers){
if(t.cooldown>0)t.cooldown-=dt;
if(t.type==="commander")continue;
if(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);
if(dist<=t.range && e.segment+e.progress/1000>best){
target=e;
best=e.segment+e.progress/1000;
}
}
if(target){
let damage=t.damage;
if(t.type==="demo"){
for(const e of enemies){
if(Math.hypot(e.x-target.x,e.y-target.y)<55){
e.hp-=damage;
}
}
}else{
target.hp-=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)continue;
if(Math.hypot(t.x-c.x,t.y-c.y)<c.range){
t.cooldown=Math.max(0,t.cooldown-dt*.35);
}
}
}
// Remove defeated enemies and award coins.
for(let i=enemies.length-1;i>=0;i--){
if(enemies[i].hp<=0){
coins+=enemies[i].reward;
enemies.splice(i,1);
}
}
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(won){
gameOver=true;
waveActive=false;
message(won?"You completed the game!":"Game over! You ran out of lives.");
}
function drawBackground(){
ctx.fillStyle="#75a85a";
ctx.fillRect(0,0,900,520);
// Grass details.
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);
}
}
// Road.
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){
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();
ctx.fillStyle="#fff";
ctx.font="bold 12px Arial";
ctx.textAlign="center";
ctx.fillText(t.type==="scout"?"S":
t.type==="sniper"?"N":
t.type==="mini"?"M":
t.type==="demo"?"D":"C",t.x,t.y+4);
ctx.fillStyle="#fff";
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.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="rgba(255,255,255,.5)";
ctx.font="bold 16px Arial";
ctx.fillText("PLACEMENT MODE: "+towerData[placing].name,20,30);
}
if(paused){
ctx.fillStyle="rgba(0,0,0,.55)";
ctx.fillRect(0,0,900,520);
ctx.fillStyle="white";
ctx.font="bold 45px Arial";
ctx.textAlign="center";
ctx.fillText("PAUSED",450,260);
ctx.textAlign="left";
}
if(gameOver){
ctx.fillStyle="rgba(0,0,0,.65)";
ctx.fillRect(0,0,900,520);
ctx.fillStyle="white";
ctx.font="bold 42px Arial";
ctx.textAlign="center";
ctx.fillText("GAME OVER",450,245);
ctx.font="20px Arial";
ctx.fillText("Press Restart to play again",450,285);
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>
Featured
Advertisement
Category
Action