做一个html代码,并且是一个格斗游戏,并且是手机运行不是电脑,
- User
做一个html代码,并且是一个格斗游戏,并且是手机运行不是电脑,
Assistant
我来为你创建一个适合手机运行的HTML格斗游戏。这个游戏将包含触屏控制、简单的格斗机制和移动端优化。
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <title>手机格斗游戏</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); overflow: hidden; font-family: Arial, sans-serif; touch-action: none; } #gameCanvas { display: block; margin: 0 auto; background: linear-gradient(to bottom, #87CEEB 0%, #98FB98 100%); border: 2px solid #333; } #controls { position: fixed; bottom: 20px; left: 0; right: 0; display: flex; justify-content: space-between; padding: 0 20px; } .control-group { display: flex; gap: 10px; } .control-btn { width: 60px; height: 60px; border-radius: 50%; border: 3px solid #fff; background: rgba(255, 255, 255, 0.3); color: white; font-size: 14px; font-weight: bold; display: flex; align-items: center; justify-content: center; user-select: none; touch-action: manipulation; } .control-btn:active { background: rgba(255, 255, 255, 0.6); transform: scale(0.95); } #gameInfo { position: fixed; top: 10px; left: 0; right: 0; display: flex; justify-content: space-between; padding: 0 20px; color: white; font-size: 16px; font-weight: bold; } .health-bar { width: 120px; height: 20px; background: #333; border: 2px solid #fff; border-radius: 10px; overflow: hidden; } .health-fill { height: 100%; background: linear-gradient(90deg, #ff4444, #ffaa44); transition: width 0.3s ease; } </style> </head> <body> <div id="gameInfo"> <div> <div>玩家1</div> <div class="health-bar"> <div class="health-fill" id="player1Health" style="width: 100%;"></div> </div> </div> <div> <div>玩家2</div> <div class="health-bar"> <div class="health-fill" id="player2Health" style="width: 100%;"></div> </div> </div> </div> <canvas id="gameCanvas"></canvas> <div id="controls"> <div class="control-group"> <div class="control-btn" id="leftBtn">←</div> <div class="control-btn" id="rightBtn">→</div> </div> <div class="control-group"> <div class="control-btn" id="jumpBtn">跳</div> <div class="control-btn" id="punchBtn">拳</div> <div class="control-btn" id="kickBtn">踢</div> </div> </div> <script> const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); // 设置画布大小适应手机屏幕 function resizeCanvas() { canvas.width = window.innerWidth - 4; canvas.height = window.innerHeight - 200; } resizeCanvas(); window.addEventListener('resize', resizeCanvas); // 游戏状态 const game = { gravity: 0.8, groundY: canvas.height - 50 }; // 玩家类 class Player { constructor(x, y, color, isAI = false) { this.x = x; this.y = y; this.width = 40; this.height = 60; this.color = color; this.velocityX = 0; this.velocityY = 0; this.health = 100; this.maxHealth = 100; this.isJumping = false; this.isAttacking = false; this.attackCooldown = 0; this.isAI = isAI; this.aiTimer = 0; } update() { // 重力 if (this.y < game.groundY - this.height) { this.velocityY += game.gravity; this.isJumping = true; } else { this.y = game.groundY - this.height; this.velocityY = 0; this.isJumping = false; } // 移动 this.x += this.velocityX; this.y += this.velocityY; // 边界检测 if (this.x < 0) this.x = 0; if (this.x > canvas.width - this.width) this.x = canvas.width - this.width; // 摩擦力 this.velocityX *= 0.8; // 攻击冷却 if (this.attackCooldown > 0) { this.attackCooldown--; } // AI逻辑 if (this.isAI) { this.aiUpdate(); } } aiUpdate() { this.aiTimer++; const player1 = players[0]; const distance = Math.abs(this.x - player1.x); if (this.aiTimer % 60 === 0) { // 每秒做一次决策 if (distance > 80) { // 靠近玩家 if (this.x < player1.x) { this.moveRight(); } else { this.moveLeft(); } } else if (distance < 60) { // 攻击或跳跃 if (Math.random() < 0.7) { this.attack(); } else { this.jump(); } } } } moveLeft() { this.velocityX = -5; } moveRight() { this.velocityX = 5; } jump() { if (!this.isJumping) { this.velocityY = -15; this.isJumping = true; } } attack() { if (this.attackCooldown === 0) { this.isAttacking = true; this.attackCooldown = 30; // 检测攻击命中 const other = this === players[0] ? players[1] : players[0]; const distance = Math.abs(this.x - other.x); if (distance < 70) { other.takeDamage(10); // 击退效果 if (this.x < other.x) { other.velocityX = 8; } else { other.velocityX = -8; } } setTimeout(() => { this.isAttacking = false; }, 200); } } takeDamage(damage) { this.health -= damage; if (this.health < 0) this.health = 0; } draw() { // 绘制玩家身体 ctx.fillStyle = this.color; ctx.fillRect(this.x, this.y, this.width, this.height); // 绘制头部 ctx.beginPath(); ctx.arc(this.x + this.width/2, this.y + 15, 12, 0, Math.PI * 2); ctx.fill(); // 绘制攻击效果 if (this.isAttacking) { ctx.fillStyle = 'rgba(255, 255, 0, 0.7)'; ctx.fillRect(this.x - 10, this.y + 20, this.width + 20, 20); } // 绘制血量条 const barWidth = this.width; const barHeight = 6; const barY = this.y - 15; ctx.fillStyle = '#333'; ctx.fillRect(this.x, barY, barWidth, barHeight); ctx.fillStyle = this.health > 30 ? '#4CAF50' : '#F44336'; ctx.fillRect(this.x, barY, (this.health / this.maxHealth) * barWidth, barHeight); } } // 创建玩家 const players = [ new Player(100, game.groundY - 60, '#4CAF50'), new Player(canvas.width - 140, game.groundY - 60, '#F44336', true) ]; // 控制状态 const controls = { left: false, right: false, jump: false, punch: false, kick: false }; // 触摸控制 function setupControls() { const buttons = { leftBtn: () => controls.left = true, rightBtn: () => controls.right = true, jumpBtn: () => players[0].jump(), punchBtn: () => players[0].attack(), kickBtn: () => players[0].attack() }; Object.keys(buttons).forEach(id => { const btn = document.getElementById(id); btn.addEventListener('touchstart', (e) => { e.preventDefault(); buttons[id](); }); btn.addEventListener('touchend', (e) => { e.preventDefault(); if (id === 'leftBtn') controls.left = false; if (id === 'rightBtn') controls.right = false; }); // 鼠标支持(用于测试) btn.addEventListener('mousedown', buttons[id]); btn.addEventListener('mouseup', () => { if (id === 'leftBtn') controls.left = false; if (id === 'rightBtn') controls.right = false; }); }); } // 更新血量显示 function updateHealthBars() { document.getElementById('player1Health').style.width = players[0].health + '%'; document.getElementById('player2Health').style.width = players[1].health + '%'; } // 游戏循环 function gameLoop() { // 清空画布 ctx.clearRect(0, 0, canvas.width, canvas.height); // 绘制地面 ctx.fillStyle = '#8B4513'; ctx.fillRect(0, game.groundY, canvas.width, canvas.height - game.groundY); // 处理玩家1控制 if (controls.left) players[0].moveLeft(); if (controls.right) players[0].moveRight(); // 更新玩家 players.forEach(player => { player.update(); player.draw(); }); // 更新血量条 updateHealthBars(); // 检查游戏结束 if (players[0].health <= 0 || players[1].health <= 0) { const winner = players[0].health > 0 ? '玩家1' : '玩家2'; ctx.fillStyle = 'rgba(0, 0, 0, 0.8)'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = 'white'; ctx.font = '32px Arial'; ctx.textAlign = 'center'; ctx.fillText(winner + ' 获胜!', canvas.width/2, canvas.height/2); ctx.fillText('刷新页面重新开始', canvas.width/2, canvas.height/2 + 50); return; } requestAnimationFrame(gameLoop); } // 初始化游戏 setupControls(); gameLoop(); // 防止页面滚动 document.addEventListener('touchmove', (e) => { e.preventDefault(); }, { passive: false }); </script> </body> </html>这个HTML格斗游戏包含以下特性:
游戏功能:
- 双人格斗(玩家 vs AI)
- 移动、跳跃、攻击系统
- 血量系统和伤害计算
- 重力和物理效果
手机优化:
- 响应式设计,适配不同屏幕尺寸
- 触屏控制按钮
- 防止页面滚动
- 触摸反馈效果
控制方式:
- 左右箭头:移动
- 跳:跳跃
- 拳/踢:攻击
AI对手:
- 自动寻路和攻击
- 简单的战斗策略
将这段代码保存为
.html文件,用手机浏览器打开即可游玩。游戏会自动适配手机屏幕,通过触摸屏幕下方的控制按钮来操作角色进行格斗。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.