aaa / index.html
zclcz's picture
我想设计一个纯H5的坦克大战小游戏 要求有无敌铁壁、无限生命、最高等级子弹的开关 - Initial Deployment
e9e8d93 verified
Raw History Blame Contribute Delete
44.5 kB
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>坦克大战</title> <script src="https://cdn.tailwindcss.com"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> <style> #gameCanvas { background-color: #333; display: block; margin: 0 auto; border: 4px solid #555; box-shadow: 0 0 20px rgba(0, 0, 0, 0.5); } .toggle-switch { position: relative; display: inline-block; width: 60px; height: 30px; } .toggle-switch input { opacity: 0; width: 0; height: 0; } .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; transition: .4s; border-radius: 34px; } .slider:before { position: absolute; content: ""; height: 22px; width: 22px; left: 4px; bottom: 4px; background-color: white; transition: .4s; border-radius: 50%; } input:checked + .slider { background-color: #4CAF50; } input:checked + .slider:before { transform: translateX(30px); } @keyframes explosion { 0% { transform: scale(0.1); opacity: 1; } 50% { opacity: 0.7; } 100% { transform: scale(1.5); opacity: 0; } } .explosion { position: absolute; width: 40px; height: 40px; background: radial-gradient(circle, #ffcc00, #ff6600); border-radius: 50%; animation: explosion 0.5s ease-out forwards; pointer-events: none; } .powerup { position: absolute; width: 20px; height: 20px; border-radius: 50%; animation: float 2s ease-in-out infinite; } @keyframes float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-10px); } } .bullet-powerup { background: radial-gradient(circle, #00ccff, #0066ff); } .health-powerup { background: radial-gradient(circle, #ff3366, #cc0033); } .shield-powerup { background: radial-gradient(circle, #66ff66, #00cc00); } </style> </head> <body class="bg-gray-900 text-white min-h-screen"> <div class="container mx-auto px-4 py-8"> <header class="text-center mb-8"> <h1 class="text-4xl font-bold text-yellow-400 mb-2">坦克大战</h1> <p class="text-gray-300">经典街机游戏 - HTML5重制版</p> </header> <div class="flex flex-col lg:flex-row gap-8"> <!-- 游戏控制面板 --> <div class="lg:w-1/4 bg-gray-800 p-6 rounded-lg shadow-lg"> <h2 class="text-2xl font-semibold mb-4 text-yellow-300 flex items-center"> <i class="fas fa-gamepad mr-2"></i> 游戏控制 </h2> <div class="mb-6"> <h3 class="text-lg font-medium mb-2 flex items-center"> <i class="fas fa-keyboard mr-2"></i> 操作方式 </h3> <ul class="space-y-2 text-gray-300"> <li><i class="fas fa-arrow-up mr-2"></i> 上箭头: 向上移动</li> <li><i class="fas fa-arrow-down mr-2"></i> 下箭头: 向下移动</li> <li><i class="fas fa-arrow-left mr-2"></i> 左箭头: 向左移动</li> <li><i class="fas fa-arrow-right mr-2"></i> 右箭头: 向右移动</li> <li><i class="fas fa-space-shuttle mr-2"></i> 空格键: 发射子弹</li> </ul> </div> <div class="mb-6"> <h3 class="text-lg font-medium mb-2 flex items-center"> <i class="fas fa-cogs mr-2"></i> 游戏设置 </h3> <div class="space-y-4"> <div class="flex items-center justify-between"> <span class="flex items-center"> <i class="fas fa-shield-alt mr-2 text-blue-400"></i> 无敌铁壁 </span> <label class="toggle-switch"> <input type="checkbox" id="invincibleToggle"> <span class="slider"></span> </label> </div> <div class="flex items-center justify-between"> <span class="flex items-center"> <i class="fas fa-heart mr-2 text-red-400"></i> 无限生命 </span> <label class="toggle-switch"> <input type="checkbox" id="infiniteLivesToggle"> <span class="slider"></span> </label> </div> <div class="flex items-center justify-between"> <span class="flex items-center"> <i class="fas fa-bolt mr-2 text-yellow-400"></i> 最高等级子弹 </span> <label class="toggle-switch"> <input type="checkbox" id="maxBulletToggle"> <span class="slider"></span> </label> </div> </div> </div> <div class="flex flex-col space-y-4"> <button id="startBtn" class="bg-green-600 hover:bg-green-700 text-white py-2 px-4 rounded-lg font-medium transition flex items-center justify-center"> <i class="fas fa-play mr-2"></i> 开始游戏 </button> <button id="pauseBtn" class="bg-yellow-600 hover:bg-yellow-700 text-white py-2 px-4 rounded-lg font-medium transition flex items-center justify-center"> <i class="fas fa-pause mr-2"></i> 暂停游戏 </button> <button id="resetBtn" class="bg-red-600 hover:bg-red-700 text-white py-2 px-4 rounded-lg font-medium transition flex items-center justify-center"> <i class="fas fa-redo mr-2"></i> 重新开始 </button> </div> </div> <!-- 游戏区域 --> <div class="lg:w-3/4"> <div class="bg-gray-800 p-4 rounded-lg shadow-lg"> <div class="flex justify-between items-center mb-4"> <div class="flex items-center space-x-4"> <div class="flex items-center"> <i class="fas fa-heart text-red-500 mr-2"></i> <span id="livesDisplay" class="font-bold">3</span> </div> <div class="flex items-center"> <i class="fas fa-bolt text-yellow-500 mr-2"></i> <span id="bulletLevelDisplay" class="font-bold">1</span> </div> <div class="flex items-center"> <i class="fas fa-shield-alt text-blue-500 mr-2"></i> <span id="shieldDisplay" class="font-bold">0</span> </div> </div> <div class="flex items-center"> <i class="fas fa-star text-yellow-300 mr-2"></i> <span id="scoreDisplay" class="font-bold text-xl">0</span> </div> </div> <div class="relative"> <canvas id="gameCanvas" width="800" height="500"></canvas> <div id="gameOverlay" class="absolute inset-0 flex items-center justify-center bg-black bg-opacity-70 hidden"> <div class="text-center p-8 bg-gray-900 rounded-lg max-w-md"> <h2 id="gameOverText" class="text-4xl font-bold mb-4 text-red-500">游戏结束</h2> <p id="finalScore" class="text-2xl mb-6">得分: <span class="text-yellow-400">0</span></p> <button id="restartBtn" class="bg-blue-600 hover:bg-blue-700 text-white py-2 px-6 rounded-lg font-medium transition"> <i class="fas fa-redo mr-2"></i> 再玩一次 </button> </div> </div> </div> </div> <!-- 游戏说明 --> <div class="mt-6 bg-gray-800 p-6 rounded-lg shadow-lg"> <h2 class="text-2xl font-semibold mb-4 text-yellow-300 flex items-center"> <i class="fas fa-info-circle mr-2"></i> 游戏说明 </h2> <div class="grid grid-cols-1 md:grid-cols-2 gap-4"> <div> <h3 class="text-lg font-medium mb-2 text-blue-300">游戏目标</h3> <ul class="list-disc pl-5 space-y-1 text-gray-300"> <li>消灭敌方坦克获得分数</li> <li>尽可能长时间生存</li> <li>收集道具增强能力</li> </ul> </div> <div> <h3 class="text-lg font-medium mb-2 text-green-300">道具说明</h3> <ul class="list-disc pl-5 space-y-1 text-gray-300"> <li><span class="text-blue-400">蓝色</span>: 子弹升级</li> <li><span class="text-red-400">红色</span>: 恢复生命</li> <li><span class="text-green-400">绿色</span>: 无敌护盾</li> </ul> </div> </div> </div> </div> </div> </div> <script> document.addEventListener('DOMContentLoaded', () => { // 游戏常量 const TANK_SIZE = 40; const BULLET_SIZE = 8; const ENEMY_TANK_SIZE = 36; const POWERUP_SIZE = 20; const GAME_WIDTH = 800; const GAME_HEIGHT = 500; // 游戏变量 let canvas, ctx; let gameRunning = false; let gamePaused = false; let score = 0; let lives = 3; let bulletLevel = 1; let shieldTime = 0; let invincible = false; let infiniteLives = false; let maxBullet = false; // 玩家坦克 let player = { x: GAME_WIDTH / 2 - TANK_SIZE / 2, y: GAME_HEIGHT - TANK_SIZE - 10, width: TANK_SIZE, height: TANK_SIZE, speed: 5, direction: 'up', color: '#3498db', lastShot: 0, shootDelay: 300 }; // 子弹数组 let bullets = []; // 敌方坦克数组 let enemies = []; // 道具数组 let powerups = []; // 爆炸效果数组 let explosions = []; // 敌方坦克生成计时器 let enemySpawnTimer = 0; const ENEMY_SPAWN_RATE = 1500; // 道具生成计时器 let powerupSpawnTimer = 0; const POWERUP_SPAWN_RATE = 10000; // 游戏主循环ID let gameLoopId; // 初始化游戏 function initGame() { canvas = document.getElementById('gameCanvas'); ctx = canvas.getContext('2d'); // 设置事件监听器 document.addEventListener('keydown', handleKeyDown); document.addEventListener('keyup', handleKeyUp); // 按钮事件 document.getElementById('startBtn').addEventListener('click', startGame); document.getElementById('pauseBtn').addEventListener('click', togglePause); document.getElementById('resetBtn').addEventListener('click', resetGame); document.getElementById('restartBtn').addEventListener('click', resetGame); // 开关事件 document.getElementById('invincibleToggle').addEventListener('change', (e) => { invincible = e.target.checked; if (invincible) { shieldTime = Infinity; updateShieldDisplay(); } else { shieldTime = 0; updateShieldDisplay(); } }); document.getElementById('infiniteLivesToggle').addEventListener('change', (e) => { infiniteLives = e.target.checked; }); document.getElementById('maxBulletToggle').addEventListener('change', (e) => { maxBullet = e.target.checked; if (maxBullet) { bulletLevel = 3; updateBulletLevelDisplay(); } }); // 初始化游戏状态显示 updateScoreDisplay(); updateLivesDisplay(); updateBulletLevelDisplay(); updateShieldDisplay(); } // 开始游戏 function startGame() { if (gameRunning) return; // 重置游戏状态 score = 0; lives = 3; bulletLevel = maxBullet ? 3 : 1; shieldTime = invincible ? Infinity : 0; bullets = []; enemies = []; powerups = []; explosions = []; // 更新显示 updateScoreDisplay(); updateLivesDisplay(); updateBulletLevelDisplay(); updateShieldDisplay(); // 重置玩家位置 player.x = GAME_WIDTH / 2 - TANK_SIZE / 2; player.y = GAME_HEIGHT - TANK_SIZE - 10; player.direction = 'up'; // 隐藏游戏结束覆盖层 document.getElementById('gameOverlay').classList.add('hidden'); gameRunning = true; gamePaused = false; // 开始游戏循环 gameLoopId = requestAnimationFrame(gameLoop); } // 暂停/继续游戏 function togglePause() { if (!gameRunning) return; gamePaused = !gamePaused; if (gamePaused) { cancelAnimationFrame(gameLoopId); document.getElementById('pauseBtn').innerHTML = '<i class="fas fa-play mr-2"></i> 继续游戏'; } else { gameLoopId = requestAnimationFrame(gameLoop); document.getElementById('pauseBtn').innerHTML = '<i class="fas fa-pause mr-2"></i> 暂停游戏'; } } // 重置游戏 function resetGame() { cancelAnimationFrame(gameLoopId); gameRunning = false; document.getElementById('gameOverlay').classList.add('hidden'); document.getElementById('pauseBtn').innerHTML = '<i class="fas fa-pause mr-2"></i> 暂停游戏'; startGame(); } // 游戏结束 function gameOver() { gameRunning = false; cancelAnimationFrame(gameLoopId); document.getElementById('gameOverText').textContent = '游戏结束'; document.getElementById('finalScore').innerHTML = `得分: <span class="text-yellow-400">${score}</span>`; document.getElementById('gameOverlay').classList.remove('hidden'); } // 游戏主循环 function gameLoop(timestamp) { if (gamePaused) return; // 清除画布 ctx.clearRect(0, 0, GAME_WIDTH, GAME_HEIGHT); // 更新游戏状态 updateGame(timestamp); // 绘制游戏元素 drawGame(); // 继续游戏循环 gameLoopId = requestAnimationFrame(gameLoop); } // 更新游戏状态 function updateGame(timestamp) { // 生成敌方坦克 if (timestamp - enemySpawnTimer > ENEMY_SPAWN_RATE) { spawnEnemy(); enemySpawnTimer = timestamp; } // 生成道具 if (timestamp - powerupSpawnTimer > POWERUP_SPAWN_RATE) { spawnPowerup(); powerupSpawnTimer = timestamp; } // 更新玩家位置 updatePlayerPosition(); // 更新子弹位置 updateBullets(); // 更新敌方坦克 updateEnemies(timestamp); // 更新道具 updatePowerups(); // 更新爆炸效果 updateExplosions(); // 更新护盾时间 if (shieldTime > 0 && !invincible) { shieldTime -= 16; // 大约每帧减少16ms if (shieldTime <= 0) { shieldTime = 0; updateShieldDisplay(); } } } // 绘制游戏元素 function drawGame() { // 绘制背景网格 drawGrid(); // 绘制玩家坦克 drawTank(player.x, player.y, player.width, player.height, player.direction, player.color, shieldTime > 0 || invincible); // 绘制子弹 bullets.forEach(bullet => { ctx.fillStyle = bullet.color; ctx.beginPath(); ctx.arc(bullet.x, bullet.y, bullet.size / 2, 0, Math.PI * 2); ctx.fill(); }); // 绘制敌方坦克 enemies.forEach(enemy => { drawTank(enemy.x, enemy.y, enemy.width, enemy.height, enemy.direction, enemy.color, false); }); // 绘制道具 powerups.forEach(powerup => { ctx.fillStyle = powerup.color; ctx.beginPath(); ctx.arc(powerup.x, powerup.y, powerup.size / 2, 0, Math.PI * 2); ctx.fill(); // 绘制道具图标 ctx.fillStyle = 'white'; ctx.font = '12px Arial'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; switch(powerup.type) { case 'bullet': ctx.fillText('↑', powerup.x, powerup.y); break; case 'health': ctx.fillText('♥', powerup.x, powerup.y); break; case 'shield': ctx.fillText('⛨', powerup.x, powerup.y); break; } }); // 绘制爆炸效果 explosions.forEach(explosion => { ctx.fillStyle = explosion.color; ctx.globalAlpha = explosion.opacity; ctx.beginPath(); ctx.arc(explosion.x, explosion.y, explosion.size / 2, 0, Math.PI * 2); ctx.fill(); ctx.globalAlpha = 1; }); } // 绘制背景网格 function drawGrid() { ctx.strokeStyle = '#444'; ctx.lineWidth = 1; // 水平线 for (let y = 0; y < GAME_HEIGHT; y += 40) { ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(GAME_WIDTH, y); ctx.stroke(); } // 垂直线 for (let x = 0; x < GAME_WIDTH; x += 40) { ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, GAME_HEIGHT); ctx.stroke(); } } // 绘制坦克 function drawTank(x, y, width, height, direction, color, hasShield) { // 坦克主体 ctx.fillStyle = color; ctx.fillRect(x, y, width, height); // 坦克炮管 ctx.fillStyle = '#555'; switch(direction) { case 'up': ctx.fillRect(x + width/2 - 4, y - 10, 8, 15); break; case 'down': ctx.fillRect(x + width/2 - 4, y + height - 5, 8, 15); break; case 'left': ctx.fillRect(x - 10, y + height/2 - 4, 15, 8); break; case 'right': ctx.fillRect(x + width - 5, y + height/2 - 4, 15, 8); break; } // 坦克履带标记 ctx.fillStyle = '#222'; ctx.fillRect(x + 5, y + 5, width - 10, 5); ctx.fillRect(x + 5, y + height - 10, width - 10, 5); // 如果有护盾,绘制护盾效果 if (hasShield) { ctx.strokeStyle = '#00ff00'; ctx.lineWidth = 3; ctx.beginPath(); ctx.arc(x + width/2, y + height/2, width/2 + 5, 0, Math.PI * 2); ctx.stroke(); } } // 生成敌方坦克 function spawnEnemy() { const x = Math.random() * (GAME_WIDTH - ENEMY_TANK_SIZE); const y = 10; const direction = Math.random() > 0.5 ? 'left' : 'right'; const speed = 1 + Math.random() * 2; enemies.push({ x: x, y: y, width: ENEMY_TANK_SIZE, height: ENEMY_TANK_SIZE, speed: speed, direction: direction, color: '#e74c3c', lastShot: 0, shootDelay: 1500 + Math.random() * 1500, moveTimer: 0, moveDuration: 1000 + Math.random() * 2000 }); } // 生成道具 function spawnPowerup() { const x = 20 + Math.random() * (GAME_WIDTH - 40); const y = 20 + Math.random() * (GAME_HEIGHT - 40); const types = ['bullet', 'health', 'shield']; const type = types[Math.floor(Math.random() * types.length)]; let color; switch(type) { case 'bullet': color = '#3498db'; break; case 'health': color = '#e74c3c'; break; case 'shield': color = '#2ecc71'; break; } powerups.push({ x: x, y: y, size: POWERUP_SIZE, type: type, color: color, duration: 10000 // 10秒后消失 }); // 创建视觉效果 const powerupElement = document.createElement('div'); powerupElement.className = `powerup ${type}-powerup`; powerupElement.style.left = `${x - POWERUP_SIZE/2}px`; powerupElement.style.top = `${y - POWERUP_SIZE/2}px`; document.getElementById('gameCanvas').parentNode.appendChild(powerupElement); // 10秒后移除视觉效果 setTimeout(() => { powerupElement.remove(); }, 10000); } // 更新玩家位置 function updatePlayerPosition() { // 边界检查 if (player.x < 0) player.x = 0; if (player.x > GAME_WIDTH - player.width) player.x = GAME_WIDTH - player.width; if (player.y < 0) player.y = 0; if (player.y > GAME_HEIGHT - player.height) player.y = GAME_HEIGHT - player.height; } // 更新子弹位置 function updateBullets() { for (let i = bullets.length - 1; i >= 0; i--) { const bullet = bullets[i]; // 移动子弹 switch(bullet.direction) { case 'up': bullet.y -= bullet.speed; break; case 'down': bullet.y += bullet.speed; break; case 'left': bullet.x -= bullet.speed; break; case 'right': bullet.x += bullet.speed; break; } // 检查子弹是否超出边界 if (bullet.x < 0 || bullet.x > GAME_WIDTH || bullet.y < 0 || bullet.y > GAME_HEIGHT) { bullets.splice(i, 1); continue; } // 检查子弹与敌方坦克碰撞 if (bullet.owner === 'player') { for (let j = enemies.length - 1; j >= 0; j--) { const enemy = enemies[j]; if (checkCollision(bullet, enemy)) { // 创建爆炸效果 createExplosion(enemy.x + enemy.width/2, enemy.y + enemy.height/2); // 移除子弹和敌方坦克 bullets.splice(i, 1); enemies.splice(j, 1); // 增加分数 score += 100; updateScoreDisplay(); break; } } } // 检查子弹与玩家坦克碰撞 else if (bullet.owner === 'enemy') { if (checkCollision(bullet, player) && shieldTime <= 0 && !invincible) { // 创建爆炸效果 createExplosion(player.x + player.width/2, player.y + player.height/2); // 移除子弹 bullets.splice(i, 1); // 减少生命 if (!infiniteLives) { lives--; updateLivesDisplay(); } // 检查游戏是否结束 if (lives <= 0) { gameOver(); return; } // 重置玩家位置 player.x = GAME_WIDTH / 2 - TANK_SIZE / 2; player.y = GAME_HEIGHT - TANK_SIZE - 10; player.direction = 'up'; // 短暂无敌时间 if (!invincible) { shieldTime = 2000; updateShieldDisplay(); } } } } } // 更新敌方坦克 function updateEnemies(timestamp) { for (let i = enemies.length - 1; i >= 0; i--) { const enemy = enemies[i]; // 移动敌方坦克 if (timestamp - enemy.moveTimer > enemy.moveDuration) { // 改变方向 const directions = ['left', 'right', 'down']; enemy.direction = directions[Math.floor(Math.random() * directions.length)]; enemy.moveTimer = timestamp; enemy.moveDuration = 1000 + Math.random() * 2000; } switch(enemy.direction) { case 'left': enemy.x -= enemy.speed; break; case 'right': enemy.x += enemy.speed; break; case 'down': enemy.y += enemy.speed; break; } // 边界检查 if (enemy.x < 0) { enemy.x = 0; enemy.direction = 'right'; } if (enemy.x > GAME_WIDTH - enemy.width) { enemy.x = GAME_WIDTH - enemy.width; enemy.direction = 'left'; } if (enemy.y > GAME_HEIGHT - enemy.height) { enemy.y = GAME_HEIGHT - enemy.height; enemy.direction = 'up'; } // 敌方坦克射击 if (timestamp - enemy.lastShot > enemy.shootDelay) { shoot(enemy, 'enemy'); enemy.lastShot = timestamp; enemy.shootDelay = 1500 + Math.random() * 1500; } // 检查敌方坦克与玩家坦克碰撞 if (checkCollision(enemy, player) && shieldTime <= 0 && !invincible) { // 创建爆炸效果 createExplosion(player.x + player.width/2, player.y + player.height/2); // 移除敌方坦克 enemies.splice(i, 1); // 减少生命 if (!infiniteLives) { lives--; updateLivesDisplay(); } // 检查游戏是否结束 if (lives <= 0) { gameOver(); return; } // 重置玩家位置 player.x = GAME_WIDTH / 2 - TANK_SIZE / 2; player.y = GAME_HEIGHT - TANK_SIZE - 10; player.direction = 'up'; // 短暂无敌时间 if (!invincible) { shieldTime = 2000; updateShieldDisplay(); } } } } // 更新道具 function updatePowerups() { for (let i = powerups.length - 1; i >= 0; i--) { const powerup = powerups[i]; // 检查玩家是否收集道具 if (checkCollision(player, powerup)) { // 应用道具效果 switch(powerup.type) { case 'bullet': if (!maxBullet) { bulletLevel = Math.min(bulletLevel + 1, 3); updateBulletLevelDisplay(); } break; case 'health': if (!infiniteLives) { lives = Math.min(lives + 1, 5); updateLivesDisplay(); } break; case 'shield': shieldTime = 5000; // 5秒护盾 if (invincible) shieldTime = Infinity; updateShieldDisplay(); break; } // 移除道具 powerups.splice(i, 1); } } } // 更新爆炸效果 function updateExplosions() { for (let i = explosions.length - 1; i >= 0; i--) { const explosion = explosions[i]; // 更新爆炸大小和透明度 explosion.size += 2; explosion.opacity -= 0.02; // 移除消失的爆炸 if (explosion.opacity <= 0) { explosions.splice(i, 1); } } } // 创建爆炸效果 function createExplosion(x, y) { explosions.push({ x: x, y: y, size: 10, color: `rgb(255, ${Math.floor(Math.random() * 156) + 100}, 0)`, opacity: 1 }); // 创建视觉效果 const explosionElement = document.createElement('div'); explosionElement.className = 'explosion'; explosionElement.style.left = `${x - 20}px`; explosionElement.style.top = `${y - 20}px`; document.getElementById('gameCanvas').parentNode.appendChild(explosionElement); // 动画结束后移除 setTimeout(() => { explosionElement.remove(); }, 500); } // 射击 function shoot(shooter, owner) { const now = Date.now(); // 检查射击冷却 if (now - shooter.lastShot < shooter.shootDelay && owner === 'player') { return; } let bulletX, bulletY; // 根据方向确定子弹初始位置 switch(shooter.direction) { case 'up': bulletX = shooter.x + shooter.width/2; bulletY = shooter.y - BULLET_SIZE/2; break; case 'down': bulletX = shooter.x + shooter.width/2; bulletY = shooter.y + shooter.height + BULLET_SIZE/2; break; case 'left': bulletX = shooter.x - BULLET_SIZE/2; bulletY = shooter.y + shooter.height/2; break; case 'right': bulletX = shooter.x + shooter.width + BULLET_SIZE/2; bulletY = shooter.y + shooter.height/2; break; } // 设置子弹属性 const bullet = { x: bulletX, y: bulletY, size: BULLET_SIZE, speed: owner === 'player' ? 8 : 5, direction: shooter.direction, color: owner === 'player' ? '#f1c40f' : '#e74c3c', owner: owner }; // 如果是玩家射击且子弹等级高,可以发射多个子弹 if (owner === 'player') { if (bulletLevel >= 2) { // 第二颗子弹 const bullet2 = {...bullet}; if (bulletLevel >= 2) { if (shooter.direction === 'up' || shooter.direction === 'down') { bullet2.x = shooter.x + shooter.width/4; bullet.x = shooter.x + 3*shooter.width/4; } else { bullet2.y = shooter.y + shooter.height/4; bullet.y = shooter.y + 3*shooter.height/4; } bullets.push(bullet2); } // 第三颗子弹 if (bulletLevel >= 3) { const bullet3 = {...bullet}; if (shooter.direction === 'up' || shooter.direction === 'down') { bullet3.x = shooter.x + shooter.width/2; bullet3.y = shooter.y - BULLET_SIZE; } else { bullet3.x = shooter.x - BULLET_SIZE; bullet3.y = shooter.y + shooter.height/2; } bullets.push(bullet3); } } } bullets.push(bullet); shooter.lastShot = now; } // 检查碰撞 function checkCollision(obj1, obj2) { return obj1.x < obj2.x + obj2.width && obj1.x + obj1.width > obj2.x && obj1.y < obj2.y + obj2.height && obj1.y + obj1.height > obj2.y; } // 键盘控制 let keys = {}; function handleKeyDown(e) { keys[e.key] = true; // 空格键射击 if (e.key === ' ' && gameRunning && !gamePaused) { shoot(player, 'player'); } } function handleKeyUp(e) { keys[e.key] = false; } // 更新玩家移动 function updatePlayerPosition() { if (keys['ArrowUp'] || keys['w']) { player.y -= player.speed; player.direction = 'up'; } if (keys['ArrowDown'] || keys['s']) { player.y += player.speed; player.direction = 'down'; } if (keys['ArrowLeft'] || keys['a']) { player.x -= player.speed; player.direction = 'left'; } if (keys['ArrowRight'] || keys['d']) { player.x += player.speed; player.direction = 'right'; } // 边界检查 player.x = Math.max(0, Math.min(GAME_WIDTH - player.width, player.x)); player.y = Math.max(0, Math.min(GAME_HEIGHT - player.height, player.y)); } // 更新分数显示 function updateScoreDisplay() { document.getElementById('scoreDisplay').textContent = score; } // 更新生命显示 function updateLivesDisplay() { document.getElementById('livesDisplay').textContent = lives; } // 更新子弹等级显示 function updateBulletLevelDisplay() { document.getElementById('bulletLevelDisplay').textContent = bulletLevel; } // 更新护盾显示 function updateShieldDisplay() { const shieldDisplay = document.getElementById('shieldDisplay'); if (shieldTime > 0 || invincible) { shieldDisplay.textContent = invincible ? '∞' : Math.ceil(shieldTime / 1000); shieldDisplay.classList.add('text-green-500'); } else { shieldDisplay.textContent = '0'; shieldDisplay.classList.remove('text-green-500'); } } // 初始化游戏 initGame(); }); </script> <p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=zclcz/aaa" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body> </html>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>坦克大战</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
#gameCanvas {
background-color: #333;
display: block;
margin: 0 auto;
border: 4px solid #555;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.toggle-switch {
position: relative;
display: inline-block;
width: 60px;
height: 30px;
}
.toggle-switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .4s;
border-radius: 34px;
}
.slider:before {
position: absolute;
content: "";
height: 22px;
width: 22px;
left: 4px;
bottom: 4px;
background-color: white;
transition: .4s;
border-radius: 50%;
}
input:checked + .slider {
background-color: #4CAF50;
}
input:checked + .slider:before {
transform: translateX(30px);
}
@keyframes explosion {
0% { transform: scale(0.1); opacity: 1; }
50% { opacity: 0.7; }
100% { transform: scale(1.5); opacity: 0; }
}
.explosion {
position: absolute;
width: 40px;
height: 40px;
background: radial-gradient(circle, #ffcc00, #ff6600);
border-radius: 50%;
animation: explosion 0.5s ease-out forwards;
pointer-events: none;
}
.powerup {
position: absolute;
width: 20px;
height: 20px;
border-radius: 50%;
animation: float 2s ease-in-out infinite;
}
@keyframes float {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-10px); }
}
.bullet-powerup {
background: radial-gradient(circle, #00ccff, #0066ff);
}
.health-powerup {
background: radial-gradient(circle, #ff3366, #cc0033);
}
.shield-powerup {