上一篇文章介绍了一些简单的使用技巧,本文演示一下基本功能和一个很好用的Plan功能。
当我们claude登入后,直接和它对话即可,这就是claude的基本功能。和所有的AI工具一致
❯ 你好,什么是贪吃蛇游戏 ● 贪吃蛇(Snake)是一款经典的电子游戏: - 玩法:玩家控制一条蛇在屏幕上移动,吃掉食物后蛇身变长 - 目标:尽可能吃更多食物,获得更高分数 - 游戏结束条件:蛇撞到墙壁或撞到自己的身体 - 操作:通常用方向键(上下左右)控制移动方向 这是一款简单但上手容易、极具挑战性的益智游戏,最早出现在1970年代,诺基亚手机版本(1998年)使其广为人知。
我们是不是经常会遇到这样的情况:
和AI沟通的时候,要么自己表述有问题,没有清晰的把自己的需求说出来,要么AI理解有问题,没有理解你的表述
在这两种情况下,如果按照基本模式去工作,很容易让claude直接按照错误的意思干活去了,这导致claude干了不是你意思的活。导致你要么需要clear重新来,要么rewind重新问。
Plan模式就是解决这种困境的。
Plan mode让你和Claude提前对话,让它尝试理解你的意思,在理解完成之后,给出包含你意思的计划书。当你认为这个计划书没问题的时候,你可以让他按照这个计划书来干活。这样的好处就是,claude不会提前干活,且你能够很精确的确保claude干的活符合你的需求(因为你会检查计划书)。
下面开始演示 两种方式启动plan mode
/plan 直接启动shift + tab 快捷键选中到plan mode成功之后显示如下
⏸ plan mode on (shift+tab to cycle) ◐ medium · /effort ╭─── Claude Code v2.1.63 ───────────────────────────────────────────────────────────────────────────────────────────╮ │ │ Tips for getting started │ │ Welcome back! │ Run /init to create a CLAUDE.md file with instructions for Claude │ │ │ ───────────────────────────────────────────────────────────────── │ │ ▐▛███▜▌ │ Recent activity │ │ ▝▜█████▛▘ │ No recent activity │ │ ▘▘ ▝▝ │ │ │ │ │ │ Sonnet 4.6 · Claude Pro │ │ │ ~/ │ │ ╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
可以看到plan mode开启成功了,接下来我们对话,让它开发一个贪吃蛇小游戏,它不会直接开始干,而是理解你的需求,提供一份计划书,如下
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Plan to implement │ │ │ │ 网页贪吃蛇游戏开发计划 │ │ │ │ Context │ │ │ │ 用户需要一个网页版贪吃蛇游戏,通过键盘上下左右方向键控制蛇的移动。 │ │ │ │ 实现方案 │ │ │ │ 创建单个 HTML 文件 ~/snake.html,包含完整的 HTML + CSS + JavaScript。 │ │ │ │ 核心功能 │ │ │ │ - Canvas 绘制:使用 HTML5 Canvas 绘制游戏画面 │ │ - 蛇的移动:通过键盘方向键(↑↓←→)控制方向 │ │ - 食物生成:随机位置生成食物,蛇吃到后身体增长 │ │ - 碰撞检测:撞墙或撞自身则游戏结束 │ │ - 分数显示:实时显示当前得分 │ │ - 重新开始:游戏结束后可按空格键重新开始 │ │ │ │ 技术细节 │ │ │ │ - 画布大小:400x400 像素,网格单元 20x20 │ │ - 游戏循环:使用 setInterval,初始速度 150ms/帧 │ │ - 防止反向移动(如向右时不能直接向左) │ │ - 页面居中显示,深色背景,视觉清晰 │ │ │ │ 文件 │ │ │ │ - 新建:~/snake.html(单文件,无外部依赖) │ │ │ │ 验证 │ │ │ │ - 浏览器打开 snake.html,方向键控制蛇移动 │ │ - 吃到食物后蛇变长、分数增加 │ │ - 撞墙/撞自身游戏结束,空格键重新开始 │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
我们只需要检查这个计划书是否符合我们的需求,如果符合,就直接同意执行即可
· Incubating… ●IWrite(snake.html) ●────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ⎿ Wrotei205 lines to snake.html <!DOCTYPE html>──────────────────────────────────────────────────────────────────────────────────────────────────── ⏵⏵ <html lang="zh-CN">on (shift+tab to cycle) · esc to interrupt ◐ medium · /effor <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>贪吃蛇游戏</title> t <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { background: #1a1a2e; … +195 lines (ctrl+o to expand) ●I已创建 ~/snake.html。浏览器打开即可游玩: - 方向键 控制蛇移动(按下即开始) - 吃到红色食物蛇变长,得分 +10 - 撞墙或撞自身游戏结束 - 空格键 重新开始
等待一会儿,claude 按照计划完美完成任务了,我们检查一下效果

真的可以玩,功能无误。这个任务完美完成了。
plan mode是典型的先规划后执行的思维方式,我们在处理一个认为比较重要的事情的时候,先和他不断沟通,把沟通的决策记录下来,保存为设计文档。如果你觉得和你的需求有不一致的地方,你可以和它反复沟通修改计划书。当计划满意之后,直接执行即可
源码
<!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> * { margin: 0; padding: 0; box-sizing: border-box; } body { background: #1a1a2e; display: flex; justify-content: center; align-items: center; height: 100vh; font-family: 'Segoe UI', Arial, sans-serif; color: #eee; } #container { text-align: center; } h1 { margin-bottom: 10px; font-size: 28px; color: #0f0; } #score { font-size: 20px; margin-bottom: 10px; } canvas { border: 2px solid #0f0; background: #16213e; display: block; margin: 0 auto; } #message { margin-top: 12px; font-size: 16px; color: #aaa; height: 20px; } </style> </head> <body> <div id="container"> <h1>贪吃蛇</h1> <div id="score">得分:0</div> <canvas id="game" width="400" height="400"></canvas> <div id="message">按方向键开始游戏</div> </div> <script> (function () { const canvas = document.getElementById('game'); const ctx = canvas.getContext('2d'); const scoreEl = document.getElementById('score'); const messageEl = document.getElementById('message'); const GRID = 20; const COLS = canvas.width / GRID; const ROWS = canvas.height / GRID; const SPEED = 150; let snake, direction, nextDirection, food, score, gameOver, timer, started; function init() { snake = [ { x: 5, y: 10 }, { x: 4, y: 10 }, { x: 3, y: 10 } ]; direction = { x: 1, y: 0 }; nextDirection = { x: 1, y: 0 }; score = 0; gameOver = false; started = false; scoreEl.textContent = '得分:0'; messageEl.textContent = '按方向键开始游戏'; placeFood(); draw(); if (timer) clearInterval(timer); timer = null; } function placeFood() { while (true) { food = { x: Math.floor(Math.random() * COLS), y: Math.floor(Math.random() * ROWS) }; if (!snake.some(s => s.x === food.x && s.y === food.y)) break; } } function update() { direction = nextDirection; const head = { x: snake[0].x + direction.x, y: snake[0].y + direction.y }; if (head.x < 0 || head.x >= COLS || head.y < 0 || head.y >= ROWS) { endGame(); return; } if (snake.some(s => s.x === head.x && s.y === head.y)) { endGame(); return; } snake.unshift(head); if (head.x === food.x && head.y === food.y) { score += 10; scoreEl.textContent = '得分:' + score; placeFood(); } else { snake.pop(); } draw(); } function endGame() { gameOver = true; clearInterval(timer); timer = null; messageEl.textContent = '游戏结束!按空格键重新开始'; draw(); } function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); // Draw grid lines (subtle) ctx.strokeStyle = '#1a1a3e'; for (let i = 0; i <= COLS; i++) { ctx.beginPath(); ctx.moveTo(i * GRID, 0); ctx.lineTo(i * GRID, canvas.height); ctx.stroke(); } for (let i = 0; i <= ROWS; i++) { ctx.beginPath(); ctx.moveTo(0, i * GRID); ctx.lineTo(canvas.width, i * GRID); ctx.stroke(); } // Draw food ctx.fillStyle = '#e74c3c'; ctx.beginPath(); ctx.arc(food.x * GRID + GRID / 2, food.y * GRID + GRID / 2, GRID / 2 - 2, 0, Math.PI * 2); ctx.fill(); // Draw snake snake.forEach(function (seg, i) { ctx.fillStyle = i === 0 ? '#2ecc71' : '#27ae60'; ctx.fillRect(seg.x * GRID + 1, seg.y * GRID + 1, GRID - 2, GRID - 2); }); // Game over overlay if (gameOver) { ctx.fillStyle = 'rgba(0,0,0,0.55)'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = '#fff'; ctx.font = 'bold 30px sans-serif'; ctx.textAlign = 'center'; ctx.fillText('游戏结束', canvas.width / 2, canvas.height / 2 - 10); ctx.font = '18px sans-serif'; ctx.fillText('得分:' + score, canvas.width / 2, canvas.height / 2 + 20); } } document.addEventListener('keydown', function (e) { if (e.key === ' ' && gameOver) { e.preventDefault(); init(); return; } const key = e.key; if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(key)) { e.preventDefault(); } if (key === 'ArrowUp' && direction.y !== 1) { nextDirection = { x: 0, y: -1 }; } else if (key === 'ArrowDown' && direction.y !== -1) { nextDirection = { x: 0, y: 1 }; } else if (key === 'ArrowLeft' && direction.x !== 1) { nextDirection = { x: -1, y: 0 }; } else if (key === 'ArrowRight' && direction.x !== -1) { nextDirection = { x: 1, y: 0 }; } if (!started && !gameOver) { started = true; messageEl.textContent = ''; timer = setInterval(update, SPEED); } }); init(); })(); </script> </body> </html>