Download task_003/solution/code/index.html from Lego-X/UXInteract: direct link, hf CLI and curl.
- Browser
- Download file 5.78 kB
-
https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_003/solution/code/index.html
- Command line
-
hf download hf://datasets/Lego-X/UXInteract/task_003/solution/code/index.html
-
curl -L -o index.html https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_003/solution/code/index.html
5.78 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>2D Tower Defense Framework</title> | |
| <style> | |
| body { font-family: system-ui, sans-serif; background: #0d1b2a; color: #e0e1dd; text-align: center; margin: 0; padding: 16px; } | |
| h1 { color: #00b4d8; } | |
| #hud { margin: 8px; font-size: 16px; } | |
| #game { background: #1b263b; border: 2px solid #00b4d8; display: block; margin: 12px auto; } | |
| .controls { margin: 10px; } | |
| button { padding: 8px 16px; font-size: 15px; border: none; border-radius: 6px; | |
| background: #00b4d8; color: #03045e; cursor: pointer; margin: 0 4px; } | |
| #tip { color: #90e0ef; margin-top: 8px; } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>2D Tower Defense Framework</h1> | |
| <div id="hud">Lives: <span id="lives">10</span> · Kills: <span id="kills">0</span> · Towers: <span id="towerCount">0</span></div> | |
| <canvas id="game" width="600" height="360"></canvas> | |
| <div class="controls"> | |
| <button id="spawn">Spawn Enemy Wave</button> | |
| <button id="reset">Reset</button> | |
| </div> | |
| <p id="tip">Click an empty area of the map to place a tower (cost: 1). Enemies move along the path from left to right.</p> | |
| <script> | |
| const canvas = document.getElementById('game'); | |
| const ctx = canvas.getContext('2d'); | |
| const livesEl = document.getElementById('lives'); | |
| const killsEl = document.getElementById('kills'); | |
| const towerCountEl = document.getElementById('towerCount'); | |
| // path: a polyline the enemies follow across the map | |
| const path = [ | |
| { x: 0, y: 80 }, | |
| { x: 200, y: 80 }, | |
| { x: 200, y: 280 }, | |
| { x: 420, y: 280 }, | |
| { x: 420, y: 120 }, | |
| { x: 600, y: 120 }, | |
| ]; | |
| let enemies = []; | |
| let towers = []; | |
| let lives = 10; | |
| let kills = 0; | |
| let simulationRemainder = 0; | |
| let deterministicMode = false; | |
| function spawnWave() { | |
| for (let i = 0; i < 5; i++) { | |
| enemies.push({ seg: 0, t: -i * 0.15, hp: 3, speed: 0.004 }); | |
| } | |
| } | |
| function pointOnPath(seg, t) { | |
| const a = path[seg], b = path[seg + 1]; | |
| return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t }; | |
| } | |
| // move all enemies forward along the path | |
| function moveEnemies() { | |
| for (const enemy of enemies) { | |
| if (enemy.t < 0) { enemy.t += enemy.speed * 16; continue; } | |
| enemy.t += enemy.speed * 16; | |
| if (enemy.t >= 1) { enemy.t = 0; enemy.seg++; } | |
| if (enemy.seg >= path.length - 1) { | |
| enemy.dead = true; | |
| lives = Math.max(0, lives - 1); | |
| } | |
| } | |
| enemies = enemies.filter(e => !e.dead); | |
| } | |
| function towersFire() { | |
| for (const tower of towers) { | |
| for (const enemy of enemies) { | |
| if (enemy.t < 0) continue; | |
| const p = pointOnPath(enemy.seg, enemy.t); | |
| const dist = Math.hypot(p.x - tower.x, p.y - tower.y); | |
| if (dist <= tower.range) { | |
| enemy.hp -= 0.05; | |
| if (enemy.hp <= 0 && !enemy.dead) { enemy.dead = true; kills++; } | |
| } | |
| } | |
| } | |
| enemies = enemies.filter(e => !e.dead); | |
| } | |
| function draw() { | |
| ctx.clearRect(0, 0, canvas.width, canvas.height); | |
| // draw path | |
| ctx.strokeStyle = '#415a77'; | |
| ctx.lineWidth = 26; | |
| 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(); | |
| // draw towers | |
| for (const tower of towers) { | |
| ctx.fillStyle = '#ffd60a'; | |
| ctx.fillRect(tower.x - 8, tower.y - 8, 16, 16); | |
| ctx.strokeStyle = 'rgba(255,214,10,0.25)'; | |
| ctx.lineWidth = 1; | |
| ctx.beginPath(); | |
| ctx.arc(tower.x, tower.y, tower.range, 0, Math.PI * 2); | |
| ctx.stroke(); | |
| } | |
| // draw enemies | |
| for (const enemy of enemies) { | |
| if (enemy.t < 0) continue; | |
| const p = pointOnPath(enemy.seg, enemy.t); | |
| ctx.fillStyle = '#ef476f'; | |
| ctx.beginPath(); | |
| ctx.arc(p.x, p.y, 8, 0, Math.PI * 2); | |
| ctx.fill(); | |
| } | |
| livesEl.textContent = lives; | |
| killsEl.textContent = kills; | |
| towerCountEl.textContent = towers.length; | |
| } | |
| function advanceSimulation(milliseconds) { | |
| if (!Number.isFinite(milliseconds) || milliseconds <= 0) return; | |
| simulationRemainder += milliseconds; | |
| const frames = Math.floor(simulationRemainder / 16); | |
| simulationRemainder -= frames * 16; | |
| for (let i = 0; i < frames; i++) { | |
| moveEnemies(); | |
| towersFire(); | |
| } | |
| } | |
| function loop() { | |
| if (!deterministicMode) advanceSimulation(16); | |
| draw(); | |
| requestAnimationFrame(loop); | |
| } | |
| canvas.addEventListener('click', (e) => { | |
| const rect = canvas.getBoundingClientRect(); | |
| const x = e.clientX - rect.left; | |
| const y = e.clientY - rect.top; | |
| towers.push({ x, y, range: 70 }); // place a basic tower | |
| }); | |
| document.getElementById('spawn').addEventListener('click', spawnWave); | |
| document.getElementById('reset').addEventListener('click', () => { | |
| enemies = []; towers = []; lives = 10; kills = 0; simulationRemainder = 0; deterministicMode = false; | |
| }); | |
| spawnWave(); | |
| requestAnimationFrame(loop); | |
| window.gameTest = { | |
| reset() { deterministicMode = true; enemies = []; towers = []; lives = 10; kills = 0; simulationRemainder = 0; draw(); }, | |
| spawnEnemy(options = {}) { deterministicMode = true; enemies.push({ seg: 0, t: 0, hp: 3, speed: options.speed ?? 0.004 }); draw(); return this.state(); }, | |
| placeTower(x, y, options = {}) { deterministicMode = true; towers.push({ x, y, range: options.range ?? 70 }); draw(); return this.state(); }, | |
| step(ms) { deterministicMode = true; advanceSimulation(ms); draw(); return this.state(); }, | |
| state() { return { path: path.map(p=>({...p})), enemies: enemies.map(e=>({...e, position:e.t>=0?pointOnPath(e.seg,e.t):null})), towers: towers.map(t=>({...t})), lives, kills }; } | |
| }; | |
| </script> | |
| </body> | |
| </html> | |