版本比较
bank
正文
增加 124 行删除 124 行
7 行未改动89101112121314151611 行未改动2829303132323334353615 行未改动5253545556565758596011 行未改动7273747576767778798023 行未改动1041051061071081081091101111121131141151161161171181191201211221231241251251261271281296 行未改动1361371381391401401411421431445 行未改动1501511521531541541551561571581581591601611621631641651661671671681691701711711721731741755 行未改动1811821831841851851861871881895 行未改动1951961971981991992002012022032042042052062072082092102112112122132142152152162172182192192202212222232242242252262272272282292302312322332332342352362372382392402402412422432442452462472472482492502512512522532542552552562572582592602612622622632642652662672682692702702712722732732742752762772782792802812812822832842852862872882882892902902912922932942942952962972982993003013013023033043053063043053063073083093103113113123133143153 行未改动31932032132232332332432532632732832933033133133233333433533633733833934034034134234234334434534634634734834935035135235335435535635635735835936036136136236336436536536636736836911 行未改动3813823833843853853863873883893903913923933943943953963973983993994004014014024034044044054064074084094104114124134134144154164174184184194204214224234234244254264277 行未改动43543643743843943944044144244244344444544544644744844944945045145245345345445545645645745845946046146246246346446546646746746846947047147247347447547647647747847948048148248248348448548648748848948949049149149249349449549549649749849910 行未改动5105115125135145145155165175183 行未改动5225235245255265265275285295305315315325335345355365375385395405405415425435435445455465475485495505505515525535545555565575575585595605615615625635645655665665675685695705705715725735745755765765775785795805805815825825835845855865875885885895905915925935935945955965975985985996006016026026036046056063 行未改动6106116126136146146156166176186186196206216226226236246256266276286286296306316326336346356366366376386396406406416426436436446456466466476486496506516526536536546556566566576586596606616626636646646656666676686696706706716726736746746756766776786796806816826826836846856866876886896906906916926936946956956966976986997007007017027037047057057067077087097097107117127137147157157167177187197207207217227237247257257267277287297307317327327337347357367377387387397407417427427437447457457467477487497507517527537537547557567577587597607617627627637647657667677687697707717717727737747757767777787797807817817827837847857867877887897907917917927937947957957967977987998008018018028038048058068078078088098108118118128138148148158168178178188198208218228238238248258268278288288298308318328338338348358368368378388398404 行未改动7 行未改动 <!-- 引入Tailwind CSS --> <script src="https://cdn.tailwindcss.com"></script> <!-- 引入Font Awesome --> <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
<!-- 配置Tailwind --> <script> tailwind.config = { theme: {11 行未改动 }, } } </script>
<!-- 自定义工具类 --> <style type="text/tailwindcss"> @layer utilities { .pixel-corners {15 行未改动 100% { transform: scale(2); opacity: 0; } } } </style>
<!-- 引入像素风格字体 --> <link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap" rel="stylesheet"></head>
11 行未改动 游戏说明 </button> </div> </div>
<!-- 游戏说明 --> <div id="instructions" class="absolute inset-0 z-40 hidden flex-col items-center justify-center bg-gray-900/95 backdrop-blur-sm p-6"> <h2 class="text-xl font-game text-primary mb-6 text-center">游戏说明</h2> <div class="bg-gray-800 p-6 rounded-lg max-w-md w-full mb-6">23 行未改动 <button id="back-btn" class="bg-primary hover:bg-primary/80 text-white py-2 px-4 rounded-lg font-game text-xs transition-all"> 返回 </button> </div>
<!-- 游戏暂停界面 --> <div id="pause-screen" class="absolute inset-0 z-30 hidden flex-col items-center justify-center bg-gray-900/80 backdrop-blur-sm"> <h2 class="text-2xl font-game text-primary mb-6">游戏暂停</h2> <button id="resume-btn" class="bg-primary hover:bg-primary/80 text-white py-3 px-6 rounded-lg font-game text-sm transition-all"> 继续游戏 </button> </div>
<!-- 游戏结束界面 --> <div id="game-over-screen" class="absolute inset-0 z-30 hidden flex-col items-center justify-center bg-gray-900/90 backdrop-blur-sm"> <h2 class="text-2xl font-game text-secondary mb-2">游戏结束</h2> <p class="text-xl mb-2">最终得分: <span id="final-score" class="text-primary">0</span></p> <button id="restart-btn" class="bg-primary hover:bg-primary/80 text-white py-3 px-6 rounded-lg font-game text-sm transition-all mt-6"> 重新开始 </button> </div>
<!-- 游戏信息面板 --> <div class="flex justify-between items-center mb-4 px-2"> <div class="flex items-center gap-2 bg-gray-800 py-2 px-4 rounded-lg"> <i class="fa fa-trophy text-yellow-500"></i>6 行未改动 <button id="pause-btn" class="bg-gray-800 hover:bg-gray-700 py-2 px-4 rounded-lg transition-all"> <i class="fa fa-pause"></i> </button> </div>
<!-- 游戏画布 --> <canvas id="gameCanvas" class="bg-ground pixel-corners game-shadow w-full"></canvas> </div>
5 行未改动 const TANK_SPEED = 2; const OBSTACLE_SIZE = 30; const ENEMY_SPAWN_RATE = 5000; // 敌方坦克生成间隔(ms) const EXPLOSION_DURATION = 500; // 爆炸动画持续时间(ms)
// 获取画布和上下文 const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d');
// 设置画布尺寸 function resizeCanvas() { // 保持16:9的游戏比例 const containerWidth = canvas.parentElement.clientWidth; const idealHeight = containerWidth * 9 / 16; canvas.width = Math.floor(containerWidth / OBSTACLE_SIZE) * OBSTACLE_SIZE; canvas.height = Math.floor(idealHeight / OBSTACLE_SIZE) * OBSTACLE_SIZE; }
// 初始化时调整一次画布大小 resizeCanvas(); window.addEventListener('resize', resizeCanvas);
// 游戏状态 let gameState = { running: false, paused: false,5 行未改动 obstacles: [], enemyTanks: [], playerTank: null };
// 玩家坦克类 class Tank { constructor(x, y, isPlayer = false) { this.x = x;5 行未改动 this.color = isPlayer ? '#165DFF' : '#FF4D4F'; this.lastShot = 0; this.shotCooldown = isPlayer ? 500 : 1000; // 射击冷却时间 }
// 绘制坦克 draw() { ctx.save(); ctx.translate(this.x + this.size / 2, this.y + this.size / 2);
// 根据方向旋转 switch(this.direction) { case 'right': ctx.rotate(Math.PI / 2); break; case 'down': ctx.rotate(Math.PI); break; case 'left': ctx.rotate(3 * Math.PI / 2); break; }
// 坦克身体 ctx.fillStyle = this.color; ctx.fillRect(-this.size/2, -this.size/2, this.size, this.size);
// 坦克炮管 ctx.fillStyle = '#000'; ctx.fillRect(-3, -this.size/2, 6, -this.size/2);
// 坦克履带 ctx.fillStyle = '#333'; ctx.fillRect(-this.size/2, -this.size/2, 5, this.size); ctx.fillRect(this.size/2 - 5, -this.size/2, 5, this.size);
ctx.restore(); }
// 移动坦克 move(direction) { this.direction = direction; let newX = this.x; let newY = this.y;
switch(direction) { case 'up': newY -= this.speed; break; case 'down': newY += this.speed; break; case 'left': newX -= this.speed; break; case 'right': newX += this.speed; break; }
// 边界检测和碰撞检测 if (this.isValidMove(newX, newY)) { this.x = newX; this.y = newY; } }
// 检查移动是否有效(不超出边界且不碰撞障碍物) isValidMove(newX, newY) { // 边界检测 if (newX < 0 || newX + this.size > canvas.width || if (newX < 0 || newX + this.size > canvas.width || newY < 0 || newY + this.size > canvas.height) { return false; }
// 障碍物碰撞检测 for (const obstacle of gameState.obstacles) { if (this.checkCollision(newX, newY, obstacle)) { return false; } }
// 坦克之间的碰撞检测 const tanks = this.isPlayer ? gameState.enemyTanks : [gameState.playerTank, ...gameState.enemyTanks.filter(t => t !== this)]; for (const tank of tanks) { if (tank && this.checkCollision(newX, newY, tank)) { return false; } }
return true; }
// 碰撞检测 checkCollision(x, y, obj) { return x < obj.x + obj.size && x + this.size > obj.x && y < obj.y + obj.size && y + this.size > obj.y; }
// 发射子弹 shoot() { const now = Date.now(); if (now - this.lastShot < this.shotCooldown) { return; }
this.lastShot = now;
// 计算子弹初始位置(炮口位置) let bulletX = this.x + this.size / 2 - BULLET_SIZE / 2; let bulletY = this.y + this.size / 2 - BULLET_SIZE / 2;
switch(this.direction) { case 'up': bulletY -= this.size / 2; break; case 'down': bulletY += this.size / 2; break; case 'left': bulletX -= this.size / 2; break; case 'right': bulletX += this.size / 2; break; }
// 创建子弹 gameState.bullets.push(new Bullet( bulletX, bulletY, this.direction, bulletX, bulletY, this.direction, this.isPlayer )); } }
// 子弹类 class Bullet { constructor(x, y, direction, isPlayerBullet) { this.x = x;3 行未改动 this.speed = BULLET_SPEED; this.isPlayerBullet = isPlayerBullet; this.color = isPlayerBullet ? '#FFF' : '#FFD700'; }
// 绘制子弹 draw() { ctx.fillStyle = this.color; ctx.beginPath(); ctx.arc(this.x + this.size/2, this.y + this.size/2, this.size/2, 0, Math.PI * 2); ctx.fill(); }
// 更新子弹位置 update() { switch(this.direction) { case 'up': this.y -= this.speed; break; case 'down': this.y += this.speed; break; case 'left': this.x -= this.speed; break; case 'right': this.x += this.speed; break; }
// 检查是否超出边界 return this.x > 0 && this.x < canvas.width && return this.x > 0 && this.x < canvas.width && this.y > 0 && this.y < canvas.height; } }
// 障碍物类 class Obstacle { constructor(x, y, type = 'brick') { this.x = x; this.y = y; this.size = OBSTACLE_SIZE; this.type = type; // 'brick' 可破坏, 'steel' 不可破坏 this.color = type === 'brick' ? '#8B4513' : '#808080'; }
// 绘制障碍物 draw() { ctx.fillStyle = this.color; ctx.fillRect(this.x, this.y, this.size, this.size);
// 绘制纹理 ctx.strokeStyle = 'rgba(0,0,0,0.3)'; ctx.lineWidth = 1;
if (this.type === 'brick') { // 砖块纹理 ctx.beginPath(); ctx.moveTo(this.x, this.y + this.size/2);11 行未改动 ctx.stroke(); } } }
// 爆炸效果类 class Explosion { constructor(x, y) { this.x = x; this.y = y; this.size = 40; this.startTime = Date.now(); }
// 绘制爆炸效果 draw() { const elapsed = Date.now() - this.startTime; const progress = elapsed / EXPLOSION_DURATION;
if (progress >= 1) return false;
ctx.save(); ctx.globalAlpha = 1 - progress;
// 绘制爆炸动画 const gradient = ctx.createRadialGradient( this.x + this.size/2, this.y + this.size/2, 0, this.x + this.size/2, this.y + this.size/2, this.size * progress ); gradient.addColorStop(0, '#FFD700'); gradient.addColorStop(0.5, '#FFA500'); gradient.addColorStop(1, '#FF4500');
ctx.fillStyle = gradient; ctx.beginPath(); ctx.arc(this.x + this.size/2, this.y + this.size/2, this.size * progress, 0, Math.PI * 2); ctx.fill();
ctx.restore(); return true; } }
// 初始化游戏 function initGame() { // 重置游戏状态 gameState = {7 行未改动 obstacles: [], enemyTanks: [], playerTank: new Tank(canvas.width / 2 - TANK_SIZE / 2, canvas.height - TANK_SIZE * 2, true) };
// 更新UI updateUI();
// 生成随机障碍物 generateObstacles();
// 开始游戏循环 gameLoop(); }
// 生成随机障碍物 function generateObstacles() { const obstacleCount = Math.floor((canvas.width / OBSTACLE_SIZE) * (canvas.height / OBSTACLE_SIZE) * 0.15);
for (let i = 0; i < obstacleCount; i++) { let x, y, validPosition;
// 找到有效的位置(不与玩家坦克重叠) do { validPosition = true; x = Math.floor(Math.random() * (canvas.width / OBSTACLE_SIZE)) * OBSTACLE_SIZE; y = Math.floor(Math.random() * (canvas.height / OBSTACLE_SIZE)) * OBSTACLE_SIZE;
// 确保不与玩家坦克重叠 if (gameState.playerTank && gameState.playerTank.checkCollision(x, y, gameState.playerTank)) { validPosition = false; }
// 确保不与已生成的障碍物重叠 for (const obstacle of gameState.obstacles) { if (x === obstacle.x && y === obstacle.y) { validPosition = false; break; } } } while (!validPosition);
// 随机生成砖块或钢铁障碍物(7:3比例) const type = Math.random() > 0.3 ? 'brick' : 'steel'; gameState.obstacles.push(new Obstacle(x, y, type)); } }
// 生成敌方坦克 function spawnEnemy() { const now = Date.now(); if (now - gameState.lastEnemySpawn < ENEMY_SPAWN_RATE) { return; }
gameState.lastEnemySpawn = now;
// 随机选择一个边缘位置生成敌方坦克 const side = Math.floor(Math.random() * 4); let x, y;
switch(side) { case 0: // 顶部 x = Math.floor(Math.random() * (canvas.width / TANK_SIZE)) * TANK_SIZE; y = 0;10 行未改动 x = 0; y = Math.floor(Math.random() * (canvas.height / TANK_SIZE)) * TANK_SIZE; break; }
// 确保不会与障碍物重叠 let canSpawn = true; for (const obstacle of gameState.obstacles) { if (x < obstacle.x + obstacle.size &&3 行未改动 canSpawn = false; break; } }
if (canSpawn) { gameState.enemyTanks.push(new Tank(x, y, false)); } }
// 敌方坦克AI function enemyAI() { for (const enemy of gameState.enemyTanks) { // 随机改变方向(1%的概率) if (Math.random() < 0.01) { const directions = ['up', 'down', 'left', 'right']; enemy.direction = directions[Math.floor(Math.random() * directions.length)]; }
// 移动 enemy.move(enemy.direction);
// 随机射击(0.1%的概率) if (Math.random() < 0.001) { enemy.shoot(); } } }
// 检测碰撞 function checkCollisions() { // 子弹与障碍物/坦克的碰撞检测 for (let i = gameState.bullets.length - 1; i >= 0; i--) { const bullet = gameState.bullets[i]; let hit = false;
// 检测与障碍物的碰撞 for (let j = gameState.obstacles.length - 1; j >= 0; j--) { const obstacle = gameState.obstacles[j];
if (bullet.x < obstacle.x + obstacle.size && bullet.x + bullet.size > obstacle.x && bullet.y < obstacle.y + obstacle.size && bullet.y + bullet.size > obstacle.y) {
// 子弹击中障碍物 gameState.bullets.splice(i, 1); hit = true;
// 如果是砖块障碍物,并且是玩家子弹,可以破坏 if (obstacle.type === 'brick' && bullet.isPlayerBullet) { gameState.obstacles.splice(j, 1); gameState.explosions.push(new Explosion(obstacle.x, obstacle.y)); }
break; } }
if (hit) continue;
// 检测与坦克的碰撞 if (bullet.isPlayerBullet) { // 玩家子弹击中敌方坦克 for (let j = gameState.enemyTanks.length - 1; j >= 0; j--) { const enemy = gameState.enemyTanks[j];
if (bullet.x < enemy.x + enemy.size && bullet.x + bullet.size > enemy.x && bullet.y < enemy.y + enemy.size && bullet.y + bullet.size > enemy.y) {
// 击中敌方坦克 gameState.bullets.splice(i, 1); gameState.enemyTanks.splice(j, 1); gameState.explosions.push(new Explosion(enemy.x, enemy.y));
// 增加分数 gameState.score += 100; updateUI();
hit = true; break; } }3 行未改动 bullet.x < gameState.playerTank.x + gameState.playerTank.size && bullet.x + bullet.size > gameState.playerTank.x && bullet.y < gameState.playerTank.y + gameState.playerTank.size && bullet.y + bullet.size > gameState.playerTank.y) {
// 击中玩家坦克 gameState.bullets.splice(i, 1); gameState.explosions.push(new Explosion(gameState.playerTank.x, gameState.playerTank.y));
// 减少生命值 gameState.lives--; updateUI();
// 检查游戏是否结束 if (gameState.lives <= 0) { endGame(); return; }
// 重置玩家位置 gameState.playerTank = new Tank(canvas.width / 2 - TANK_SIZE / 2, canvas.height - TANK_SIZE * 2, true); hit = true; } } } }
// 更新游戏状态 function update() { if (!gameState.running || gameState.paused) return;
// 生成敌方坦克 spawnEnemy();
// 敌方AI enemyAI();
// 更新子弹 for (let i = gameState.bullets.length - 1; i >= 0; i--) { if (!gameState.bullets[i].update()) { gameState.bullets.splice(i, 1); } }
// 检测碰撞 checkCollisions();
// 更新爆炸效果 for (let i = gameState.explosions.length - 1; i >= 0; i--) { if (!gameState.explosions[i].draw()) { gameState.explosions.splice(i, 1); } } }
// 绘制游戏 function draw() { // 清空画布 ctx.fillStyle = '#228B22'; ctx.fillRect(0, 0, canvas.width, canvas.height);
// 绘制网格背景 ctx.strokeStyle = 'rgba(0, 100, 0, 0.3)'; ctx.lineWidth = 1;
// 绘制水平线 for (let y = 0; y <= canvas.height; y += OBSTACLE_SIZE) { ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(canvas.width, y); ctx.stroke(); }
// 绘制垂直线 for (let x = 0; x <= canvas.width; x += OBSTACLE_SIZE) { ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, canvas.height); ctx.stroke(); }
// 绘制障碍物 for (const obstacle of gameState.obstacles) { obstacle.draw(); }
// 绘制子弹 for (const bullet of gameState.bullets) { bullet.draw(); }
// 绘制坦克 if (gameState.playerTank) { gameState.playerTank.draw(); }
for (const enemy of gameState.enemyTanks) { enemy.draw(); }
// 绘制爆炸效果 for (const explosion of gameState.explosions) { explosion.draw(); } }
// 游戏主循环 function gameLoop() { update(); draw();
if (gameState.running) { requestAnimationFrame(gameLoop); } }
// 更新UI function updateUI() { document.getElementById('score').textContent = gameState.score; document.getElementById('lives').textContent = gameState.lives; document.getElementById('final-score').textContent = gameState.score; }
// 结束游戏 function endGame() { gameState.running = false; document.getElementById('game-over-screen').classList.remove('hidden'); }
// 暂停/继续游戏 function togglePause() { if (!gameState.running) return;
gameState.paused = !gameState.paused; const pauseScreen = document.getElementById('pause-screen');
if (gameState.paused) { pauseScreen.classList.remove('hidden'); } else { pauseScreen.classList.add('hidden'); gameLoop(); // 继续游戏循环 } }
// 键盘控制 const keys = { up: false, down: false, left: false, right: false, space: false };
document.addEventListener('keydown', (e) => { if (!gameState.running || gameState.paused) { // 只有在游戏未运行或暂停时不处理移动和射击 if (e.key === 'p' || e.key === 'P') { togglePause(); } return; }
switch(e.key) { case 'ArrowUp': keys.up = true; break; case 'ArrowDown': keys.down = true; break; case 'ArrowLeft': keys.left = true; break; case 'ArrowRight': keys.right = true; break; case ' ': keys.space = true; break; case 'p': case 'P': togglePause(); break; } });
document.addEventListener('keyup', (e) => { switch(e.key) { case 'ArrowUp': keys.up = false; break; case 'ArrowDown': keys.down = false; break; case 'ArrowLeft': keys.left = false; break; case 'ArrowRight': keys.right = false; break; case ' ': keys.space = false; break; } });
// 处理持续按键 function handleContinuousInput() { if (!gameState.running || gameState.paused || !gameState.playerTank) return;
if (keys.up) { gameState.playerTank.move('up'); } else if (keys.down) { gameState.playerTank.move('down'); }
if (keys.left) { gameState.playerTank.move('left'); } else if (keys.right) { gameState.playerTank.move('right'); }
if (keys.space) { gameState.playerTank.shoot(); }
requestAnimationFrame(handleContinuousInput); }
// 开始处理输入 handleContinuousInput();
// 事件监听 document.getElementById('start-btn').addEventListener('click', () => { document.getElementById('start-screen').classList.add('hidden'); initGame(); });
document.getElementById('how-to-play-btn').addEventListener('click', () => { document.getElementById('start-screen').classList.add('hidden'); document.getElementById('instructions').classList.remove('hidden'); });
document.getElementById('back-btn').addEventListener('click', () => { document.getElementById('instructions').classList.add('hidden'); document.getElementById('start-screen').classList.remove('hidden'); });
document.getElementById('pause-btn').addEventListener('click', togglePause); document.getElementById('resume-btn').addEventListener('click', togglePause);
document.getElementById('restart-btn').addEventListener('click', () => { document.getElementById('game-over-screen').classList.add('hidden'); initGame(); });4 行未改动