华为云码道二创:记忆翻牌小游戏从最小版到完整版的演进实录

华为云码道二创:记忆翻牌小游戏从最小版到完整版的演进实录

上一篇的结尾我留了个钩子:「下次想加难度分级和最佳纪录,把它变成一个真正完整的小游戏」。这次来把话说兑现------用华为云码道把 4x4 的最小版翻牌游戏,升级成了三档难度、带计时计分、本地最佳纪录、翻牌音效和通关结算页的完整版,代码开源在 AtomGit,文末可在线试玩。

这篇不只讲结果,重点讲清楚一件事:为什么从 demo 到产品,难的不是功能,是状态。

一、从「能玩的 demo」到「产品」差在哪

先复盘一下 v0 的样子:单文件 HTML,4x4 十六张卡牌,Fisher--Yates 洗牌、CSS 3D 翻转、三重点击守门都有,但它只能算 demo------玩两局就到头了,关掉页面什么都没留下,安静得像在图书馆里翻牌。

demo 和产品之间隔着的,是「让人愿意再来一局」的那几个东西。我把它们列成需求,正好也是三句话能说完的事:

  1. 难度分级:4x4 / 4x6 / 6x6 三档,开局前选;
  2. 计分与纪录:步数和用时实时统计,每档难度分别存本地最佳纪录;
  3. 收尾体验:翻牌和配对成功的音效,通关后的结算页。

二、扩展过程:一句话的事,但要说到点子上

这次我依然先试着「一句话」启动:

text 复制代码
给游戏加难度分级、计时和最佳纪录

加出来的功能本身没错,但细节不齐。逐个展开说:

切难度不清场。 点了 4x6,棋盘上还摊着 4x4 的旧牌,新难度只是把网格改了------v0 重建棋盘的逻辑没有被「换难度」这个新入口复用到。

计时起点错位。 计时器从页面加载就开始走,而 v0 的步数明明是从第一次点击才开始的。两个计量的起点不一致,玩起来就说不清「我的成绩」是多少。

纪录只有一个格子。 三档难度共用一条最佳纪录,4x4 创下的纪录会被 6x6 的成绩覆盖------不同难度下的成绩本来就不该放在一张桌子上比。

音效没有开关。 加了声音,但没给控制权。

还是上篇那个结论------AI 不缺实现能力,缺的是你没说出口的那部分预期。于是补了一版把状态问题点名道姓的完整需求:

text 复制代码
在现有单文件记忆翻牌游戏基础上升级,要求:

难度分级:
1. 开局提供 4x4 / 4x6 / 6x6 三档难度选择
2. 切换难度或重新开始时,先完整重置棋盘与计时
3. 4x6 与 6x6 的卡牌尺寸自适应缩小,保持整盘居中

计分与纪录:
1. 步数按「翻开一对」计一次,用时从第一次翻牌开始计时
2. 用 localStorage 分难度档保存最佳纪录(最少步数与最短用时)
3. 结算页显示本局步数与用时,并提示是否刷新最佳纪录

音效与结算:
1. 翻牌、配对成功、通关三个事件各配提示音,用 Web Audio 生成,不引外部音频文件
2. 提供静音开关,状态也存 localStorage
3. 通关后展示结算页,提供「再来一局」与「换难度」两个入口

这轮下来,码道一次给出了完整可玩的 v1 版本。起手踩坑和完整需求这两段话术,都放在文末的开源仓库里,可以照着复现全过程。

三、v0 对比 v1:一张表看演进

能力 v0 首写版 v1 完整版
难度 仅 4x4 4x4 / 4x6 / 6x6 三档
计分 仅总步数 步数 + 用时,首翻起算
最佳纪录 无 分难度存 localStorage
音效 无 翻牌/配对/通关三事件音,可静音
通关 弹窗提示 结算页 + 再来一局/换难度
状态管理 4 个变量 +难度、计时器、纪录、静音四类状态
交付 单文件双击玩 开源 AtomGit + 在线试玩

四、状态管理:产品化的真正分水岭

v0 的四个变量(firstCard、isLocked、moves、matchedPairs)之所以够用,是因为一局游戏的生命周期是封闭的:开局、判定、通关,状态不会跨局存活。

v1 打破了这个封闭性。难度、纪录、静音开关都要求跨局、跨刷新存活,计时器还引入了「跑表中的实时状态」。扩充之后,状态可以按生命周期分成三层,各归各位:
#mermaid-svg-jnrFzPn4BxUh7wBS{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-jnrFzPn4BxUh7wBS .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-jnrFzPn4BxUh7wBS .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-jnrFzPn4BxUh7wBS .error-icon{fill:#552222;}#mermaid-svg-jnrFzPn4BxUh7wBS .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-jnrFzPn4BxUh7wBS .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-jnrFzPn4BxUh7wBS .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-jnrFzPn4BxUh7wBS .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-jnrFzPn4BxUh7wBS .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-jnrFzPn4BxUh7wBS .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-jnrFzPn4BxUh7wBS .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-jnrFzPn4BxUh7wBS .marker{fill:#333333;stroke:#333333;}#mermaid-svg-jnrFzPn4BxUh7wBS .marker.cross{stroke:#333333;}#mermaid-svg-jnrFzPn4BxUh7wBS svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-jnrFzPn4BxUh7wBS p{margin:0;}#mermaid-svg-jnrFzPn4BxUh7wBS .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-jnrFzPn4BxUh7wBS .cluster-label text{fill:#333;}#mermaid-svg-jnrFzPn4BxUh7wBS .cluster-label span{color:#333;}#mermaid-svg-jnrFzPn4BxUh7wBS .cluster-label span p{background-color:transparent;}#mermaid-svg-jnrFzPn4BxUh7wBS .label text,#mermaid-svg-jnrFzPn4BxUh7wBS span{fill:#333;color:#333;}#mermaid-svg-jnrFzPn4BxUh7wBS .node rect,#mermaid-svg-jnrFzPn4BxUh7wBS .node circle,#mermaid-svg-jnrFzPn4BxUh7wBS .node ellipse,#mermaid-svg-jnrFzPn4BxUh7wBS .node polygon,#mermaid-svg-jnrFzPn4BxUh7wBS .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-jnrFzPn4BxUh7wBS .rough-node .label text,#mermaid-svg-jnrFzPn4BxUh7wBS .node .label text,#mermaid-svg-jnrFzPn4BxUh7wBS .image-shape .label,#mermaid-svg-jnrFzPn4BxUh7wBS .icon-shape .label{text-anchor:middle;}#mermaid-svg-jnrFzPn4BxUh7wBS .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-jnrFzPn4BxUh7wBS .rough-node .label,#mermaid-svg-jnrFzPn4BxUh7wBS .node .label,#mermaid-svg-jnrFzPn4BxUh7wBS .image-shape .label,#mermaid-svg-jnrFzPn4BxUh7wBS .icon-shape .label{text-align:center;}#mermaid-svg-jnrFzPn4BxUh7wBS .node.clickable{cursor:pointer;}#mermaid-svg-jnrFzPn4BxUh7wBS .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-jnrFzPn4BxUh7wBS .arrowheadPath{fill:#333333;}#mermaid-svg-jnrFzPn4BxUh7wBS .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-jnrFzPn4BxUh7wBS .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-jnrFzPn4BxUh7wBS .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-jnrFzPn4BxUh7wBS .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-jnrFzPn4BxUh7wBS .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-jnrFzPn4BxUh7wBS .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-jnrFzPn4BxUh7wBS .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-jnrFzPn4BxUh7wBS .cluster text{fill:#333;}#mermaid-svg-jnrFzPn4BxUh7wBS .cluster span{color:#333;}#mermaid-svg-jnrFzPn4BxUh7wBS div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-jnrFzPn4BxUh7wBS .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-jnrFzPn4BxUh7wBS rect.text{fill:none;stroke-width:0;}#mermaid-svg-jnrFzPn4BxUh7wBS .icon-shape,#mermaid-svg-jnrFzPn4BxUh7wBS .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-jnrFzPn4BxUh7wBS .icon-shape p,#mermaid-svg-jnrFzPn4BxUh7wBS .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-jnrFzPn4BxUh7wBS .icon-shape .label rect,#mermaid-svg-jnrFzPn4BxUh7wBS .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-jnrFzPn4BxUh7wBS .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-jnrFzPn4BxUh7wBS .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-jnrFzPn4BxUh7wBS :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 三层状态
局内态:firstCard/isLocked/moves/计时
跨局态:当前难度、静音开关
持久态:分档最佳纪录(localStorage)
一局结束即清零
重开局保留,切难度可变
永不主动清除

把前两轮的坑翻译成状态语言,其实全是同一类问题:状态的归属层放错了。切难度不清场,是把「局内态」当成了「跨局态」;计时起点错位,是「跑表中的实时状态」没有挂在正确的事件上;纪录互踩,是「持久态」没按难度分键。想明白这一层,再读码道生成的重置函数就特别顺------重置之所以重,是因为它要把「局内态」全部归零、还要把「跨局态」里可能残留的局内影响一并清掉。

另一处值得说的时间设计:通关判定后延迟约 600 毫秒才弹结算页,给最后一张牌的翻转动画留出播放时间。v0 里就有类似的 1 秒翻回延迟------两个延迟值把「动画节奏」和「逻辑判定」解耦开了,这是纯功能思维之外的手感细节。

五、实测与开源

双击打开实测:三档难度切换后棋盘干净重置;计时从第一次翻牌起算;三档纪录互不覆盖;通关结算页两个入口都能用;静音状态刷新后保留。

代码与 README 已托管在 AtomGit(地址见文末),README 里写了从 v0 到 v1 的完整演进说明,License 用 MIT。为了降低体验成本,用 AtomGit Pages 托管了在线试玩入口------评审和读者点开就能玩,不用下载文件。


#mermaid-svg-eDyTyem2SFsTIBy5{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-eDyTyem2SFsTIBy5 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-eDyTyem2SFsTIBy5 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-eDyTyem2SFsTIBy5 .error-icon{fill:#552222;}#mermaid-svg-eDyTyem2SFsTIBy5 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-eDyTyem2SFsTIBy5 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-eDyTyem2SFsTIBy5 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-eDyTyem2SFsTIBy5 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-eDyTyem2SFsTIBy5 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-eDyTyem2SFsTIBy5 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-eDyTyem2SFsTIBy5 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-eDyTyem2SFsTIBy5 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-eDyTyem2SFsTIBy5 .marker.cross{stroke:#333333;}#mermaid-svg-eDyTyem2SFsTIBy5 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-eDyTyem2SFsTIBy5 p{margin:0;}#mermaid-svg-eDyTyem2SFsTIBy5 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-eDyTyem2SFsTIBy5 .cluster-label text{fill:#333;}#mermaid-svg-eDyTyem2SFsTIBy5 .cluster-label span{color:#333;}#mermaid-svg-eDyTyem2SFsTIBy5 .cluster-label span p{background-color:transparent;}#mermaid-svg-eDyTyem2SFsTIBy5 .label text,#mermaid-svg-eDyTyem2SFsTIBy5 span{fill:#333;color:#333;}#mermaid-svg-eDyTyem2SFsTIBy5 .node rect,#mermaid-svg-eDyTyem2SFsTIBy5 .node circle,#mermaid-svg-eDyTyem2SFsTIBy5 .node ellipse,#mermaid-svg-eDyTyem2SFsTIBy5 .node polygon,#mermaid-svg-eDyTyem2SFsTIBy5 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-eDyTyem2SFsTIBy5 .rough-node .label text,#mermaid-svg-eDyTyem2SFsTIBy5 .node .label text,#mermaid-svg-eDyTyem2SFsTIBy5 .image-shape .label,#mermaid-svg-eDyTyem2SFsTIBy5 .icon-shape .label{text-anchor:middle;}#mermaid-svg-eDyTyem2SFsTIBy5 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-eDyTyem2SFsTIBy5 .rough-node .label,#mermaid-svg-eDyTyem2SFsTIBy5 .node .label,#mermaid-svg-eDyTyem2SFsTIBy5 .image-shape .label,#mermaid-svg-eDyTyem2SFsTIBy5 .icon-shape .label{text-align:center;}#mermaid-svg-eDyTyem2SFsTIBy5 .node.clickable{cursor:pointer;}#mermaid-svg-eDyTyem2SFsTIBy5 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-eDyTyem2SFsTIBy5 .arrowheadPath{fill:#333333;}#mermaid-svg-eDyTyem2SFsTIBy5 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-eDyTyem2SFsTIBy5 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-eDyTyem2SFsTIBy5 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-eDyTyem2SFsTIBy5 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-eDyTyem2SFsTIBy5 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-eDyTyem2SFsTIBy5 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-eDyTyem2SFsTIBy5 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-eDyTyem2SFsTIBy5 .cluster text{fill:#333;}#mermaid-svg-eDyTyem2SFsTIBy5 .cluster span{color:#333;}#mermaid-svg-eDyTyem2SFsTIBy5 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-eDyTyem2SFsTIBy5 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-eDyTyem2SFsTIBy5 rect.text{fill:none;stroke-width:0;}#mermaid-svg-eDyTyem2SFsTIBy5 .icon-shape,#mermaid-svg-eDyTyem2SFsTIBy5 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-eDyTyem2SFsTIBy5 .icon-shape p,#mermaid-svg-eDyTyem2SFsTIBy5 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-eDyTyem2SFsTIBy5 .icon-shape .label rect,#mermaid-svg-eDyTyem2SFsTIBy5 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-eDyTyem2SFsTIBy5 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-eDyTyem2SFsTIBy5 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-eDyTyem2SFsTIBy5 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 一句话起手 + 三类要求修正
扩展需求两轮
v0 最小版
首写博文
v1 完整版
开源 AtomGit + Pages 试玩
二创博文(本篇)

六、写在最后

两轮下来,这个小游戏从两轮对话里的最小版,长成了一个有难度曲线、有纪录、有声音的完整作品。回头看,v0 到 v1 的最大差距不在功能数量,而在状态的层次感------demo 只活在当前一局里,产品要记得住历史、接得住切换、管得住节奏。

AI 写码这件事,入门只要一句话,做好靠的是把需求一寸一寸说清楚,把状态一层一层想明白。如果你也想复现这两轮演进,直接照着仓库 README 和文中的话术走一遍即可,全程约半小时;跑完你大概率会得到和我不一样的坑------那正是自己动手比读十篇文章更有价值的地方。欢迎在评论区交流你遇到的版本,我会把高频问题整理进仓库 README 的 FAQ。

首写博文链接 :https://blog.csdn.net/qq_35366330/article/details/171546618

AtomGit 仓库 :https://atomgit.com/dickeryang/memory-flip-game

相关推荐
初学者,亦行者1 天前
用码道CodeArts一键优化前端页面,简洁又高效
华为云码道·果办officeace
QuZhengRong4 天前
【Luck‑Report】报表项目运行与部署 - V2 版本
github·agent·报表·可视化·开源项目
JMzz4 天前
重构初体验,一场不敢改变输出的手术
华为云码道·果办officeace
GitCode官方12 天前
开源鸿蒙跨平台直播|AI 时代,用 cj-tauri 开发鸿蒙 PC 应用
人工智能·华为·harmonyos·atomgit
Robert_Terisa15 天前
从Markdown到Typst:大一新生vibe coding转译器实录
llm·开源项目·独立开发·vibe coding·大一新生
严肃小白兔23 天前
MATLAB 优秀开源项目与工具箱推荐:涵盖通信、图像处理、脑机接口等九大领域
图像处理·matlab·信息与通信·开源项目
QuZhengRong24 天前
【Luck‑Report】 AI 智能报表助手 + RAG 知识库 + 报表引擎 V2.0.9 更新
github·agent·报表·开源项目·数据可视化
GitCode官方24 天前
活动预告 | 模力无限 × AtomGit珠海首场 AI Meetup,来了
人工智能·atomgit
liuchangng1 个月前
React 19 + Vite 6 实战:从教材 PDF 到 866 词间隔重复单词 App
前端·react.js·pdf·vite·开源项目