<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>贪吃蛇游戏</title> <style> body { margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; background: #f0f0f0; } #game-container { position: relative; } #canvas { border: 2px solid #333; background: #fff; } #controls { position: absolute; bottom: 20px; width: 100%; disp


你提供的代码存在不完整的问题,`ctx.clearRect(0, 0, canvas.width, canvas.height)` 之后缺少相应的绘制操作,并且缺少游戏循环和事件监听的完整逻辑。以下是补全后的代码: ```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> <style> body { margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; background: #f0f0f0; } #game-container { position: relative; } #canvas { border: 2px solid #333; background: #fff; } #controls { position: absolute; bottom: 20px; width: 100%; display: flex; justify-content: center; gap: 20px; } .btn { width: 60px; height: 60px; border-radius: 50%; background: #4CAF50; color: white; display: flex; justify-content: center; align-items: center; font-size: 20px; cursor: pointer; } #score { position: absolute; top: 20px; left: 50%; transform: translateX(-50%); font-size: 24px; font-weight: bold; color: #333; } </style> </head> <body> <div id="game-container"> <canvas id="canvas" width="300" height="400"></canvas> <div id="score">分数: 0</div> <div id="controls"> <div class="btn" id="up">↑</div> <div class="btn" id="left">←</div> <div class="btn" id="down">↓</div> <div class="btn" id="right">→</div> </div> </div> <script> // 获取DOM元素 const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const scoreEl = document.getElementById('score'); const upBtn = document.getElementById('up'); const downBtn = document.getElementById('down'); const leftBtn = document.getElementById('left'); const rightBtn = document.getElementById('right'); // 游戏参数 const gridSize = 20; // 网格大小 let snake = [{ x: 100, y: 100 }, { x: 80, y: 100 }, { x: 60, y: 100 }]; // 蛇身初始位置 let food = { x: 0, y: 0 }; // 食物位置 let direction = 'right'; // 初始方向 let score = 0; // 分数 let gameOver = false; // 游戏结束标志 let intervalId; // 定时器ID // 初始化食物位置 function initFood() { food.x = Math.floor(Math.random() * (canvas.width / gridSize - 2)) * gridSize + gridSize; food.y = Math.floor(Math.random() * (canvas.height / gridSize - 2)) * gridSize + gridSize; } // 绘制蛇 function drawSnake() { snake.forEach(segment => { ctx.fillStyle = '#4CAF50'; ctx.fillRect(segment.x, segment.y, gridSize, gridSize); ctx.strokeStyle = '#333'; ctx.strokeRect(segment.x, segment.y, gridSize, gridSize); }); } // 绘制食物 function drawFood() { ctx.fillStyle = '#F44336'; ctx.fillRect(food.x, food.y, gridSize, gridSize); ctx.strokeStyle = '#333'; ctx.strokeRect(food.x, food.y, gridSize, gridSize); } // 绘制分数 function drawScore() { scoreEl.textContent = `分数: ${score}`; } // 移动蛇 function moveSnake() { if (gameOver) return; // 获取蛇头位置 const head = { x: snake[0].x, y: snake[0].y }; // 根据方向移动蛇头 switch (direction) { case 'up': head.y -= gridSize; break; case 'down': head.y += gridSize; break; case 'left': head.x -= gridSize; break; case 'right': head.x += gridSize; break; } // 添加新蛇头 snake.unshift(head); // 检查是否吃到食物 if (head.x === food.x && head.y === food.y) { score += 10; initFood(); } else { // 没吃到食物则移除蛇尾 snake.pop(); } // 检查游戏是否结束 checkGameOver(); // 清屏并重新绘制 ctx.clearRect(0, 0, canvas.width, canvas.height); drawSnake(); drawFood(); drawScore(); } // 检查游戏是否结束 function checkGameOver() { const head = snake[0]; // 检查是否撞到墙壁 if (head.x < 0 || head.x >= canvas.width || head.y < 0 || head.y >= canvas.height) { gameOver = true; } // 检查是否撞到自己 for (let i = 1; i < snake.length; i++) { if (head.x === snake[i].x && head.y === snake[i].y) { gameOver = true; } } if (gameOver) { clearInterval(intervalId); alert('游戏结束,你的分数是: ' + score); } } // 处理方向改变 function changeDirection(newDirection) { if ( (direction === 'up' && newDirection === 'down') || (direction === 'down' && newDirection === 'up') || (direction === 'left' && newDirection === 'right') || (direction === 'right' && newDirection === 'left') ) { return; } direction = newDirection; } // 键盘事件监听 document.addEventListener('keydown', function (e) { switch (e.key) { case 'ArrowUp': changeDirection('up'); break; case 'ArrowDown': changeDirection('down'); break; case 'ArrowLeft': changeDirection('left'); break; case 'ArrowRight': changeDirection('right'); break; } }); // 按钮事件监听 upBtn.addEventListener('click', function () { changeDirection('up'); }); downBtn.addEventListener('click', function () { changeDirection('down'); }); leftBtn.addEventListener('click', function () { changeDirection('left'); }); rightBtn.addEventListener('click', function () { changeDirection('right'); }); // 初始化游戏 function initGame() { initFood(); intervalId = setInterval(moveSnake, 200); } initGame(); </script> </body> </html> ``` 额外需要注意的逻辑点 1. 方向改变的限制:在改变蛇的方向时,要避免蛇直接反转方向,比如蛇向上移动时不能直接向下移动,这会导致蛇撞到自己。 2. 边界检查:在每次移动蛇后,要检查蛇头是否超出了画布的边界,如果超出则游戏结束。 3. 食物生成位置:食物的生成位置要保证不会出现在蛇的身体上,当前代码只是简单随机生成,可能会出现这种情况,后续可以进一步优化。 [2025-07-10 15:51:52 | AI写代码神器 | 2000点数解答]
- <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>贪吃蛇游戏</title> <style> body { margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; background: #f0f0f0; } #game-container { position: relative; } #canvas { border: 2px solid #333; background: #fff; } #controls { position: absolute; bottom: 20px; width: 100%; disp(2000点数解答 | 2025-07-10 15:51:52)65
- <!doctype html> <html lang="zh"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title></title> </head> <style> body{display:flex;justify-content:center; margin-top:200px;background-color:#171715;} #container{ display: flex; justify-content: center; align-items: flex-start; height: 500px; } #canvas3d{ width: 500px; height: 500px; } </style> <body> <div id='container'> <canvas i(35点数解答 | 2024-11-09 10:44:18)168
- <!DOCTYPE html> <html> <head> <style> .art-frame { width: 300px; height: 400px; background: #fff; margin: 20px auto; padding: 15px; box-shadow: 0 0 15px rgba(0,0,0,0.2); position: relative; border-radius: 2px; } .frame-inner { height: 100%; border: 2px solid #f0f0f0; display: flex; align-items: center; justify-content: center; background: #fff; } .artwork { width: 90%; height: 90%; object-fit: cover; border: 1px solid #eee(92点数解答 | 2025-03-16 12:40:12)135
- 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> <style> body { font-family: Arial, sans-serif; background-color: #f4f4f4; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } .container { background-color: #fff; padding: 20px; border-radius: 5px; box-shadow(189点数解答 | 2025-03-05 21:41:57)166
- ```html <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>可以成为千一的恋人吗?</title> <style> body { display: flex; justify-content: center; align-items: center; height:100vh; margin: 0; background-color: #ACDEFF; font-family: 'Arial', sans-serif; text-align: center; transition: background-color 0.5s; } .container { background-color: #F9FFFF; padding: 30px; border-radius: 15px; box-shadow: 0 4px8px rgba(0,0,0, 0.1(1271点数解答 | 2025-07-26 08:49:50)78
- <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>手机贪吃蛇</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } canvas { border: 1px solid black; } </style> </head> <body> <canvas id="gameCanvas" width="400" height="400"><(182点数解答 | 2025-01-21 11:57:37)152
- <!DOCTYPE html> <html> <head> <style> .three-line-table { border-collapse: collapse; width: 100%; margin: 20px 0; font-family: Arial, sans-serif; } .three-line-table th, .three-line-table td { padding: 12px; text-align: left; border-bottom: 1px solid #ddd; } .three-line-table th { border-top: 2px solid #333; border-bottom: 2px solid #333; background-color: #f5f5f5; } .three-line-table tr:last-child td { border-bottom: 2px solid #333; } a { color: #0066cc; text-decoratio(75点数解答 | 2025-03-13 23:16:59)152
- <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <title>一袋米要扛几楼</title> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="description" content="" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style> body{margin:0;background:#123;}.base{min-height:100%;display:flex;justify-content:center;align-items:center;background:#123;}.eyeleft{overflow:hidden;width:300px;height:300px;marg(530点数解答 | 2025-07-14 23:05:45)67
- <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>爱心跳动</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; } .heart-container { position: relative; transform: rotate(45deg); (1313点数解答 | 2025-06-11 21:06:30)84
- <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>贪吃蛇游戏</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; font-family: Arial, sans-serif; } #game-container { text-align: center; (891点数解答 | 2025-05-03 22:07:44)99
- <!DOCTYPE html> <html> <head> <style> body { font-family: Arial, sans-serif; max-width: 800px; margin: auto; border: 2px solid #e0e0e0; padding: 20px } .header { text-align: center; border-bottom: 2px solid #c00; padding-bottom: 10px } .section { margin: 15px 0 } table { width: 100%; border-collapse: collapse } th, td { border: 1px solid #ddd; padding: 8px; text-align: left } .diagnosis { background: #fff8e1; padding: 15px; border-left: 4px solid #ffc107 } .signatur(223点数解答 | 2025-06-10 13:32:12)76
- <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>在线钢琴</title> <style> body { display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; background-color: #f0f0f0; } .piano-container { width: 1000px; height: 600px; background(780点数解答 | 2025-04-19 13:57:24)95