gemini-2.5-pro· Google LMArena Human Preference 140K
做一个html代码,并且是一个格斗游戏,并且是手机运行不是电脑,
- User
做一个html代码,并且是一个格斗游戏,并且是手机运行不是电脑,
- Assistant
好的,没问题!这是一个专门为手机设计的、简单但功能完整的HTML格斗游戏。
你只需要将下面的所有代码复制到一个文本文件中,然后将文件另存为
game.html。之后,将这个game.html文件传到你的手机上,用手机里的任何浏览器(如Chrome, Safari等)打开它,就可以玩了。游戏特点
- 纯HTML/CSS/JS:无需任何服务器或复杂的设置,是一个独立的单文件游戏。
- 专为手机优化:使用触摸屏按钮进行控制,而不是键盘。
- 响应式布局:能适应不同尺寸的手机屏幕。
- 基本格斗元素:移动、攻击、生命条、击中效果和游戏结束/重玩。
- 简单的AI对手:电脑玩家会自动移动和攻击。
完整代码 (复制以下所有内容)
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <title>手机格斗游戏</title> <style> /* 基础样式和布局 */ body { margin: 0; padding: 0; background-color: #333; color: white; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; display: flex; justify-content: center; align-items: center; width: 100vw; height: 100vh; overflow: hidden; /* 防止滚动 */ } #game-wrapper { width: 100%; height: 100%; display: flex; flex-direction: column; justify-content: space-between; align-items: center; } /* 游戏屏幕 */ #game-screen { width: 100%; flex-grow: 1; /* 占据剩余空间 */ background-color: #87CEEB; /* 天蓝色背景 */ position: relative; overflow: hidden; border-bottom: 5px solid #556B2F; /* 深橄榄绿地面 */ } /* 玩家和对手 */ .character { position: absolute; width: 50px; height: 100px; bottom: 0; transition: left 0.1s linear, transform 0.2s; } #player1 { background-color: #0000FF; /* 蓝色 */ left: 20%; } #player2 { background-color: #FF0000; /* 红色 */ left: 70%; transform: scaleX(-1); /* 让对手面向玩家 */ } /* 击中时的闪烁效果 */ .hit { animation: flash 0.3s; } @keyframes flash { 0%, 100% { opacity: 1; } 50% { opacity: 0.3; } } /* 攻击动画 */ .punch { animation: punch-anim 0.3s; } @keyframes punch-anim { 0% { transform: scale(1); } 50% { transform: scale(1.2, 1.2) rotate(5deg); } 100% { transform: scale(1); } } #player2.punch { animation: punch-anim-p2 0.3s; } @keyframes punch-anim-p2 { 0% { transform: scaleX(-1) scale(1); } 50% { transform: scaleX(-1) scale(1.2, 1.2) rotate(-5deg); } 100% { transform: scaleX(-1) scale(1); } } /* 生命条 */ #health-bars { position: absolute; top: 10px; width: 100%; display: flex; justify-content: space-around; padding: 0 10px; box-sizing: border-box; } .health-bar-container { width: 40%; height: 25px; background-color: rgba(0, 0, 0, 0.3); border: 2px solid white; border-radius: 5px; } .health-bar { height: 100%; width: 100%; background-color: #4CAF50; /* 绿色 */ transition: width 0.3s; } /* 游戏结束界面 */ #game-over { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.7); display: flex; flex-direction: column; justify-content: center; align-items: center; z-index: 10; } #game-over h2 { font-size: 2.5rem; margin-bottom: 20px; text-shadow: 2px 2px 4px #000; } #restart-button { padding: 15px 30px; font-size: 1.5rem; border: none; border-radius: 10px; background-color: #4CAF50; color: white; cursor: pointer; } .hidden { display: none !important; } /* 控制按钮 */ #controls { width: 100%; height: 150px; background-color: #222; display: flex; justify-content: space-between; align-items: center; padding: 10px 20px; box-sizing: border-box; user-select: none; /* 防止长按选择文字 */ -webkit-user-select: none; } .control-group { display: flex; gap: 20px; } .control-button { width: 60px; height: 60px; border-radius: 50%; border: 2px solid #ccc; background-color: #555; color: white; font-size: 1.5rem; display: flex; justify-content: center; align-items: center; font-weight: bold; } .action-button { width: 80px; height: 80px; background-color: #B22222; /* 火砖色*/ } </style> </head> <body> <div id="game-wrapper"> <div id="game-screen"> <!-- 生命条 --> <div id="health-bars"> <div class="health-bar-container"> <div id="player1-health" class="health-bar"></div> </div> <div class="health-bar-container"> <div id="player2-health" class="health-bar"></div> </div> </div> <!-- 角色 --> <div id="player1" class="character"></div> <div id="player2" class="character"></div> <!-- 游戏结束画面 --> <div id="game-over" class="hidden"> <h2 id="winner-message"></h2> <button id="restart-button">重新开始</button> </div> </div> <!-- 控制器 --> <div id="controls"> <div class="control-group"> <div id="left-btn" class="control-button">←</div> <div id="right-btn" class="control-button">→</div> </div> <div class="control-group"> <div id="punch-btn" class="control-button action-button">打</div> </div> </div> </div> <script> document.addEventListener('DOMContentLoaded', () => { // 获取所有需要的元素 const player1 = document.getElementById('player1'); const player2 = document.getElementById('player2'); const player1HealthBar = document.getElementById('player1-health'); const player2HealthBar = document.getElementById('player2-health'); const gameOverScreen = document.getElementById('game-over'); const winnerMessage = document.getElementById('winner-message'); const restartButton = document.getElementById('restart-button'); const gameScreen = document.getElementById('game-screen'); // 游戏状态变量 let player1State = { health: 100, x: 0, // 使用 getBoundingClientRect 来实时获取位置 isAttacking: false }; let player2State = { health: 100, x: 0, isAttacking: false }; let isGameOver = false; let aiInterval; // 初始化游戏 function init() { // 获取初始位置 const p1Rect = player1.getBoundingClientRect(); const p2Rect = player2.getBoundingClientRect(); const screenRect = gameScreen.getBoundingClientRect(); player1.style.left = `${screenRect.width * 0.2}px`; player2.style.left = `${screenRect.width * 0.7}px`; } // 重置游戏 function resetGame() { player1State.health = 100; player2State.health = 100; updateHealthBars(); const screenRect = gameScreen.getBoundingClientRect(); player1.style.left = `${screenRect.width * 0.2}px`; player2.style.left = `${screenRect.width * 0.7}px`; isGameOver = false; gameOverScreen.classList.add('hidden'); startAI(); } // 更新生命条 function updateHealthBars() { player1HealthBar.style.width = player1State.health + '%'; player2HealthBar.style.width = player2State.health + '%'; } // 移动玩家 function movePlayer(player, direction) { if (isGameOver) return; const step = 15; const currentLeft = parseFloat(player.style.left) || 0; const screenWidth = gameScreen.offsetWidth; const playerWidth = player.offsetWidth; let newLeft = currentLeft + (direction === 'right' ? step : -step); // 边界检测 if (newLeft < 0) newLeft = 0; if (newLeft > screenWidth - playerWidth) newLeft = screenWidth - playerWidth; player.style.left = newLeft + 'px'; } // 玩家攻击 function playerAttack() { if (isGameOver || player1State.isAttacking) return; player1State.isAttacking = true; player1.classList.add('punch'); // 检测是否击中 const p1Rect = player1.getBoundingClientRect(); const p2Rect = player2.getBoundingClientRect(); // 简单的碰撞检测,看两个矩形是否重叠 if (p1Rect.right > p2Rect.left && p1Rect.left < p2Rect.right) { dealDamage(player2State, 10, player2); } setTimeout(() => { player1.classList.remove('punch'); player1State.isAttacking = false; }, 300); // 攻击动画持续时间 } // 对手攻击 function opponentAttack() { if (isGameOver || player2State.isAttacking) return; player2State.isAttacking = true; player2.classList.add('punch'); // 检测是否击中 const p1Rect = player1.getBoundingClientRect(); const p2Rect = player2.getBoundingClientRect(); if (p1Rect.right > p2Rect.left && p1Rect.left < p2Rect.right) { dealDamage(player1State, 8, player1); // AI攻击力稍低 } setTimeout(() => { player2.classList.remove('punch'); player2State.isAttacking = false; }, 300); } // 处理伤害和游戏结束 function dealDamage(targetState, damage, targetElement) { targetState.health -= damage; if (targetState.health < 0) targetState.health = 0; targetElement.classList.add('hit'); setTimeout(() => targetElement.classList.remove('hit'), 300); updateHealthBars(); checkGameOver(); } // 检查游戏是否结束 function checkGameOver() { if (player1State.health <= 0 || player2State.health <= 0) { isGameOver = true; stopAI(); if (player1State.health <= 0) { winnerMessage.textContent = '你输了!'; } else { winnerMessage.textContent = '你赢了!'; } gameOverScreen.classList.remove('hidden'); } } // 对手的简单AI function aiLogic() { if (isGameOver) return; const p1Rect = player1.getBoundingClientRect(); const p2Rect = player2.getBoundingClientRect(); const distance = Math.abs(p1Rect.x - p2Rect.x); // 随机决定行动 const action = Math.random(); if (distance < 80 && action > 0.4) { // 距离近时,有60%几率攻击 opponentAttack(); } else if (p2Rect.x > p1Rect.x) { // 如果在玩家右边,就向左移动 movePlayer(player2, 'left'); } else { // 否则向右移动 movePlayer(player2, 'right'); } } function startAI() { aiInterval = setInterval(aiLogic, 500); // AI每0.5秒行动一次 } function stopAI() { clearInterval(aiInterval); } // --- 事件监听 --- // 使用 touchstart 和 touchend 来实现连续移动 let moveInterval; function handleMoveStart(direction) { if (moveInterval) clearInterval(moveInterval); moveInterval = setInterval(() => movePlayer(player1, direction), 50); } function handleMoveEnd() { clearInterval(moveInterval); } // 绑定移动按钮事件 const leftBtn = document.getElementById('left-btn'); const rightBtn = document.getElementById('right-btn'); leftBtn.addEventListener('touchstart', (e) => { e.preventDefault(); handleMoveStart('left'); }); leftBtn.addEventListener('touchend', handleMoveEnd); rightBtn.addEventListener('touchstart', (e) => { e.preventDefault(); handleMoveStart('right'); }); rightBtn.addEventListener('touchend', handleMoveEnd); // 桌面浏览器调试用 leftBtn.addEventListener('mousedown', () => handleMoveStart('left')); leftBtn.addEventListener('mouseup', handleMoveEnd); leftBtn.addEventListener('mouseleave', handleMoveEnd); rightBtn.addEventListener('mousedown', () => handleMoveStart('right')); rightBtn.addEventListener('mouseup', handleMoveEnd); rightBtn.addEventListener('mouseleave', handleMoveEnd); // 绑定攻击按钮事件 const punchBtn = document.getElementById('punch-btn'); punchBtn.addEventListener('touchstart', (e) => { e.preventDefault(); playerAttack(); }); punchBtn.addEventListener('mousedown', playerAttack); // 桌面浏览器调试用 // 绑定重玩按钮 restartButton.addEventListener('click', resetGame); // 游戏启动 init(); updateHealthBars(); startAI(); }); </script> </body> </html>
如何在手机上运行
-
保存文件:
- 在电脑上,打开一个文本编辑器(如记事本、VS Code等)。
- 将上面所有的代码复制并粘贴进去。
- 保存文件,命名为
game.html。请确保文件后缀是.html而不是.txt。
-
传输到手机:
- 方法一 (数据线):用USB数据线连接手机和电脑,将
game.html文件复制到手机的存储中(任意文件夹都可以)。 - 方法二 (即时通讯工具):用电脑版微信或QQ,将文件发送给手机版的自己。
- 方法三 (云存储):将文件上传到百度网盘、iCloud Drive等云服务,然后在手机上下载。
- 方法一 (数据线):用USB数据线连接手机和电脑,将
-
打开并玩耍:
- 在手机上找到
game.html文件(通常在 "文件管理" 或 "下载" 文件夹中)。 - 点击这个文件,系统会提示你用浏览器打开。
- 选择任意一个浏览器,游戏就会加载并可以玩了!
[image: 手机游戏截图示例] open (这是一个模拟的截图,实际界面会和代码一致)
- 在手机上找到
后续可以改进的地方
- 添加更多动作:比如 "踢"、"跳跃"、"防御"。
- 美化角色:现在角色只是一个色块,你可以用
<img>标签替换<div class="character">,并使用角色图片。 - 添加音效:通过JavaScript的
Audio对象,可以添加击打、背景音乐等音效。 - 更复杂的AI:让对手的行动模式更丰富,而不是简单的随机移动和攻击。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.