版本比较
bank
正文
增加 126 行删除 124 行
123452 行未改动89101111121314151611 行未改动2829303131323334353615 行未改动5253545555565758596011 行未改动7273747575767778798023 行未改动1041051061071071081091101111121131141151151161171181191201211221231241241251261271281296 行未改动1361371381391391401411421431445 行未改动1501511521531531541551561571571581591601611621631641651661661671681691701701711721731741755 行未改动1811821831841841851861871881895 行未改动1951961971981981992002012022032032042052062072082092102102112122132142142152162172182182192202212222232232242252262262272282292302312322322332342352362372382392392402412422432442452462462472482492502502512522532542542552562572582592602612612622632642652662672682692692702712722722732742752762772782792802802812822832842852862872872882892892902912922932932942952962972982993003003013023033033043053043053063073083093103103113123133143153 行未改动31932032132232232332432532632732832933033033133233333433533633733833933934034134134234334434534534634734834935035135235335435535535635735835936036036136236336436436536636736836911 行未改动3813823833843843853863873883893903913923933933943953963973983983994004004014024034034044054064074084094104114124124134144154164174174184194204214224224234244254264277 行未改动43543643743843843944044144144244344444444544644744844844945045145245245345445545545645745845946046146146246346446546646646746846947047147247347447547547647747847948048148148248348448548648748848848949049049149249349449449549649749849910 行未改动5105115125135135145155165175183 行未改动5225235245255255265275285295305305315325335345355365375385395395405415425425435445455465475485495495505515525535545555565565575585595605605615625635645655655665675685695695705715725735745755755765775785795795805815815825835845855865875875885895905915925925935945955965975975985996006016016026036046056063 行未改动610611612613613614615616617617618619620621621622623624625626627627628629630631632633634635635636637638639639640641642642643644645645646647648649650651652652653654655655656657658659660661662663663664665666667668669669670671672673673674675676677678679680681681682683684685686687688689689690691692693694694695696697698699699700701702703704704705706707708708709710711712713714714715716717718719719720721722723724724725726727728729730731731732733734735736737737738739740741741742743744744745746747748749750751752752753754755756757758759760761761762763764765766767768769770770771772773774775776777778779780780781782783784785786787788789790790791792793794794795796797798799800800801802803804805806806807808809810810811812813813814815816816817818819820821822822823824825826827827828829830831832832833834835835836837838839840841842843844```cpp<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8">2 行未改动 <!-- 引入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(); }); </script></body></html>```