正文
增加 124 行删除 124 行
7 行未改动891011121314151611 行未改动28293031323334353615 行未改动52535455565758596011 行未改动72737475767778798023 行未改动1041051061071081091101111121131141151161171181191201211221231241251261271281296 行未改动1361371381391401411421431445 行未改动1501511521531541551561571581591601611621631641651661671681691701711721731741755 行未改动1811821831841851861871881895 行未改动1951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153 行未改动31932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836911 行未改动3813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264277 行未改动43543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849910 行未改动5105115125135145155165175183 行未改动5225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056063 行未改动6106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398404 行未改动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 ||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,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 &&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)};// 更新UIupdateUI();// 生成随机障碍物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));}}// 敌方坦克AIfunction 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();// 敌方AIenemyAI();// 更新子弹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);}}// 更新UIfunction 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 行未改动7 行未改动891011121314151611 行未改动28293031323334353615 行未改动52535455565758596011 行未改动72737475767778798023 行未改动1041051061071081091101111121131141151161171181191201211221231241251261271281296 行未改动1361371381391401411421431445 行未改动1501511521531541551561571581591601611621631641651661671681691701711721731741755 行未改动1811821831841851861871881895 行未改动1951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153 行未改动31932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836911 行未改动3813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264277 行未改动43543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849910 行未改动5105115125135145155165175183 行未改动5225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056063 行未改动6106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398404 行未改动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 ||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,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 &&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)};// 更新UIupdateUI();// 生成随机障碍物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));}}// 敌方坦克AIfunction 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();// 敌方AIenemyAI();// 更新子弹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);}}// 更新UIfunction 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 行未改动