横版格斗游戏合集,-【html canvas】从零实现一款 HTML5 横版格斗游戏 - draw() 根据当前状态绘制舞台、角色、特效和 UI

📅 2026-11-30 👤 拼多多助力营销技巧 📂 手赚APP 👁️ 552 次阅读

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */

#content_views .toc,

/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */

#content_views.markdown_views > p:empty:has(+ .toc),

#content_views.markdown_views > .toc + p:empty,

/* 富文本旧版目录标记 */

#content_views.htmledit_views #main-toc,

#content_views.htmledit_views #hr-toc,

#content_views.htmledit_views p[id*="-toc"] {

display: none !important;

}

/* 目录去掉后,紧跟的首个标题不再多出一块上边距 */

#content_views.markdown_views > .toc + h1,

#content_views.markdown_views > .toc + h2,

#content_views.markdown_views > .toc + h3,

#content_views.markdown_views > .toc + h4,

#content_views.markdown_views > .toc + p:empty + h1,

#content_views.markdown_views > .toc + p:empty + h2,

#content_views.markdown_views > .toc + p:empty + h3,

#content_views.markdown_views > .toc + p:empty + h4 {

margin-top: 0 !important;

}

从零实现一款 HTML5 横版格斗游戏

《霓虹斗场:零点决战》技术分享

原生 Canvas、数据驱动角色、格斗状态机、程序化动画与离线发布

1. 项目概览

《霓虹斗场》是一款无需服务器、无需第三方游戏引擎的 2D 横版街机格斗游戏。玩家可以从 7 名原创角色中选择一人,与电脑控制的对手进行三局两胜制战斗。

当前版本包含:

角色选择、倒计时、战斗、暂停、回合结算和比赛结算等完整流程。地面拳脚、扫腿、防御、冲刺、跳跃拳脚和能量弹必杀。生命、能量、连击、胜场、回合计时与格挡削血。程序化角色骨骼动画、粒子特效、命中停帧和屏幕震动。具备距离判断、攻击、防御、跳跃和必杀决策的电脑 AI。由 Web Audio API 实时合成的界面、攻击和必杀音效。直接打开 HTML 即可运行的离线发布形式。

这份文档不只介绍“代码写了什么”,更关注几个可以复用的工程问题:

小型游戏如何在不引入引擎的情况下保持结构清晰?格斗游戏的攻击窗口、碰撞和硬直如何表达?没有传统精灵图时,如何通过程序化绘制产生多种动作?如何用极少的系统构成可感知的打击感?如何让 HTML 游戏既便于开发,又便于直接分发?

2. 为什么选择原生 Canvas

项目采用 HTML + CSS + JavaScript + Canvas 2D,而没有引入 Phaser、PixiJS 或 Unity WebGL。

这个选择适合当前项目规模,主要有四个原因:

启动成本低:项目没有构建步骤,浏览器直接加载 index.html。分发简单:代码和资产放在同一目录,压缩后即可发送。控制精确:角色姿势、碰撞盒、命中停帧和震屏都可以直接控制。依赖稳定:不存在框架升级、包管理器或 CDN 不可用的问题。

代价也很明确:资源管理、场景管理、动画、碰撞、音频和调试设施都需要自己实现。因此,这种方案适用于单舞台、小阵容和中等复杂度玩法;当项目发展到大量关卡、联网、复杂骨骼系统或工具链协作时,应考虑专业引擎。

3. 项目结构

街机横版游戏/

├─ index.html # 游戏入口和 Canvas 容器

├─ styles.css # 响应式缩放、加载界面和页面外观

├─ game.js # 游戏全部运行逻辑

├─ assets/

│ └─ neon-harbor-stage.png # 原创舞台背景

├─ tests/

│ └─ smoke-test.js # 无浏览器依赖的烟雾测试

├─ docs/ # 设计和技术文档

└─ 启动游戏.bat # Windows 快捷启动脚本

目前运行时代码集中在 game.js 中,但已经自然分成若干职责单元:

Synth:合成音效。Fighter:角色状态、输入响应、物理、攻击与绘制。Projectile:能量弹生命周期和碰撞。Particle:火花、尾迹和扬尘。Game:全局状态机、回合流程、AI、HUD 和主循环。

4. 总体架构

#mermaid-svg-IJWh453LX28LDyRt{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-IJWh453LX28LDyRt .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-IJWh453LX28LDyRt .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-IJWh453LX28LDyRt .error-icon{fill:#552222;}#mermaid-svg-IJWh453LX28LDyRt .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-IJWh453LX28LDyRt .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-IJWh453LX28LDyRt .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-IJWh453LX28LDyRt .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-IJWh453LX28LDyRt .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-IJWh453LX28LDyRt .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-IJWh453LX28LDyRt .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-IJWh453LX28LDyRt .marker{fill:#333333;stroke:#333333;}#mermaid-svg-IJWh453LX28LDyRt .marker.cross{stroke:#333333;}#mermaid-svg-IJWh453LX28LDyRt svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-IJWh453LX28LDyRt p{margin:0;}#mermaid-svg-IJWh453LX28LDyRt .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-IJWh453LX28LDyRt .cluster-label text{fill:#333;}#mermaid-svg-IJWh453LX28LDyRt .cluster-label span{color:#333;}#mermaid-svg-IJWh453LX28LDyRt .cluster-label span p{background-color:transparent;}#mermaid-svg-IJWh453LX28LDyRt .label text,#mermaid-svg-IJWh453LX28LDyRt span{fill:#333;color:#333;}#mermaid-svg-IJWh453LX28LDyRt .node rect,#mermaid-svg-IJWh453LX28LDyRt .node circle,#mermaid-svg-IJWh453LX28LDyRt .node ellipse,#mermaid-svg-IJWh453LX28LDyRt .node polygon,#mermaid-svg-IJWh453LX28LDyRt .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-IJWh453LX28LDyRt .rough-node .label text,#mermaid-svg-IJWh453LX28LDyRt .node .label text,#mermaid-svg-IJWh453LX28LDyRt .image-shape .label,#mermaid-svg-IJWh453LX28LDyRt .icon-shape .label{text-anchor:middle;}#mermaid-svg-IJWh453LX28LDyRt .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-IJWh453LX28LDyRt .rough-node .label,#mermaid-svg-IJWh453LX28LDyRt .node .label,#mermaid-svg-IJWh453LX28LDyRt .image-shape .label,#mermaid-svg-IJWh453LX28LDyRt .icon-shape .label{text-align:center;}#mermaid-svg-IJWh453LX28LDyRt .node.clickable{cursor:pointer;}#mermaid-svg-IJWh453LX28LDyRt .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-IJWh453LX28LDyRt .arrowheadPath{fill:#333333;}#mermaid-svg-IJWh453LX28LDyRt .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-IJWh453LX28LDyRt .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-IJWh453LX28LDyRt .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-IJWh453LX28LDyRt .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-IJWh453LX28LDyRt .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-IJWh453LX28LDyRt .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-IJWh453LX28LDyRt .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-IJWh453LX28LDyRt .cluster text{fill:#333;}#mermaid-svg-IJWh453LX28LDyRt .cluster span{color:#333;}#mermaid-svg-IJWh453LX28LDyRt 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-IJWh453LX28LDyRt .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-IJWh453LX28LDyRt rect.text{fill:none;stroke-width:0;}#mermaid-svg-IJWh453LX28LDyRt .icon-shape,#mermaid-svg-IJWh453LX28LDyRt .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-IJWh453LX28LDyRt .icon-shape p,#mermaid-svg-IJWh453LX28LDyRt .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-IJWh453LX28LDyRt .icon-shape .label rect,#mermaid-svg-IJWh453LX28LDyRt .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-IJWh453LX28LDyRt .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-IJWh453LX28LDyRt .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-IJWh453LX28LDyRt :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}键盘 / 指针输入Game 全局状态机Fighter 玩家AI 决策器Fighter 电脑攻击盒与身体盒检测伤害 / 格挡 / 硬直 / KO粒子 / 停帧 / 震屏 / 音效Projectile 投射物Particle 特效Canvas 绘制浏览器画面

设计上的重要约束是:全局流程只由 Game 决定,角色局部行为由 Fighter 决定。

例如,“角色是否正在受击”属于 Fighter;“双方是否已经分出回合胜负”属于 Game。保持这个边界可以避免角色对象自行切换场景,也避免回合系统直接修改动画细节。

5. 游戏主循环

浏览器通过 requestAnimationFrame 驱动循环:

let last = performance.now();

function frame(now) {

const dt = Math.min(0.033, (now - last) / 1000);

last = now;

game.update(dt);

game.draw();

requestAnimationFrame(frame);

}

这里没有使用固定时间步,而是使用经过限制的可变 dt。将最大单帧时间限制为 0.033 秒,可以避免浏览器窗口短暂卡顿后,角色一次更新移动过远,直接穿过对手或地面。

循环严格分成两个阶段:

update(dt) 更新输入、AI、物理、碰撞、计时和状态。draw() 根据当前状态绘制舞台、角色、特效和 UI。

逻辑与绘制分离后,自动化测试可以只调用 update(),不必依赖真实浏览器画面。

5.1 命中停帧

if (this.freeze > 0) {

this.freeze -= dt;

pressed.clear();

return;

}

轻攻击停帧约 0.035 秒。重攻击停帧约 0.065 秒。必杀命中停帧约 0.075 秒。

冻结期间不更新角色、AI、投射物和粒子,使双方在命中瞬间共同停住。这个实现非常简单,但对打击感的提升很明显。

6. 全局界面状态机

项目没有把菜单、战斗和结算写成互相调用的函数,而是把它们视为互斥状态。

#mermaid-svg-DI7SeLW28EBeYrr6{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-DI7SeLW28EBeYrr6 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-DI7SeLW28EBeYrr6 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-DI7SeLW28EBeYrr6 .error-icon{fill:#552222;}#mermaid-svg-DI7SeLW28EBeYrr6 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-DI7SeLW28EBeYrr6 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-DI7SeLW28EBeYrr6 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-DI7SeLW28EBeYrr6 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-DI7SeLW28EBeYrr6 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-DI7SeLW28EBeYrr6 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-DI7SeLW28EBeYrr6 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-DI7SeLW28EBeYrr6 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-DI7SeLW28EBeYrr6 .marker.cross{stroke:#333333;}#mermaid-svg-DI7SeLW28EBeYrr6 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-DI7SeLW28EBeYrr6 p{margin:0;}#mermaid-svg-DI7SeLW28EBeYrr6 defs #statediagram-barbEnd{fill:#333333;stroke:#333333;}#mermaid-svg-DI7SeLW28EBeYrr6 g.stateGroup text{fill:#9370DB;stroke:none;font-size:10px;}#mermaid-svg-DI7SeLW28EBeYrr6 g.stateGroup text{fill:#333;stroke:none;font-size:10px;}#mermaid-svg-DI7SeLW28EBeYrr6 g.stateGroup .state-title{font-weight:bolder;fill:#131300;}#mermaid-svg-DI7SeLW28EBeYrr6 g.stateGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-DI7SeLW28EBeYrr6 g.stateGroup line{stroke:#333333;stroke-width:1;}#mermaid-svg-DI7SeLW28EBeYrr6 .transition{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-DI7SeLW28EBeYrr6 .stateGroup .composit{fill:white;border-bottom:1px;}#mermaid-svg-DI7SeLW28EBeYrr6 .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px;}#mermaid-svg-DI7SeLW28EBeYrr6 .state-note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-DI7SeLW28EBeYrr6 .state-note text{fill:black;stroke:none;font-size:10px;}#mermaid-svg-DI7SeLW28EBeYrr6 .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-DI7SeLW28EBeYrr6 .edgeLabel .label rect{fill:#ECECFF;opacity:0.5;}#mermaid-svg-DI7SeLW28EBeYrr6 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-DI7SeLW28EBeYrr6 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-DI7SeLW28EBeYrr6 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-DI7SeLW28EBeYrr6 .edgeLabel .label text{fill:#333;}#mermaid-svg-DI7SeLW28EBeYrr6 .label div .edgeLabel{color:#333;}#mermaid-svg-DI7SeLW28EBeYrr6 .stateLabel text{fill:#131300;font-size:10px;font-weight:bold;}#mermaid-svg-DI7SeLW28EBeYrr6 .node circle.state-start{fill:#333333;stroke:#333333;}#mermaid-svg-DI7SeLW28EBeYrr6 .node .fork-join{fill:#333333;stroke:#333333;}#mermaid-svg-DI7SeLW28EBeYrr6 .node circle.state-end{fill:#9370DB;stroke:white;stroke-width:1.5;}#mermaid-svg-DI7SeLW28EBeYrr6 .end-state-inner{fill:white;stroke-width:1.5;}#mermaid-svg-DI7SeLW28EBeYrr6 .node rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-DI7SeLW28EBeYrr6 .node polygon{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-DI7SeLW28EBeYrr6 #statediagram-barbEnd{fill:#333333;}#mermaid-svg-DI7SeLW28EBeYrr6 .statediagram-cluster rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-DI7SeLW28EBeYrr6 .cluster-label,#mermaid-svg-DI7SeLW28EBeYrr6 .nodeLabel{color:#131300;}#mermaid-svg-DI7SeLW28EBeYrr6 .statediagram-cluster rect.outer{rx:5px;ry:5px;}#mermaid-svg-DI7SeLW28EBeYrr6 .statediagram-state .divider{stroke:#9370DB;}#mermaid-svg-DI7SeLW28EBeYrr6 .statediagram-state .title-state{rx:5px;ry:5px;}#mermaid-svg-DI7SeLW28EBeYrr6 .statediagram-cluster.statediagram-cluster .inner{fill:white;}#mermaid-svg-DI7SeLW28EBeYrr6 .statediagram-cluster.statediagram-cluster-alt .inner{fill:#f0f0f0;}#mermaid-svg-DI7SeLW28EBeYrr6 .statediagram-cluster .inner{rx:0;ry:0;}#mermaid-svg-DI7SeLW28EBeYrr6 .statediagram-state rect.basic{rx:5px;ry:5px;}#mermaid-svg-DI7SeLW28EBeYrr6 .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#f0f0f0;}#mermaid-svg-DI7SeLW28EBeYrr6 .note-edge{stroke-dasharray:5;}#mermaid-svg-DI7SeLW28EBeYrr6 .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#mermaid-svg-DI7SeLW28EBeYrr6 .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#mermaid-svg-DI7SeLW28EBeYrr6 .statediagram-note text{fill:black;}#mermaid-svg-DI7SeLW28EBeYrr6 .statediagram-note .nodeLabel{color:black;}#mermaid-svg-DI7SeLW28EBeYrr6 .statediagram .edgeLabel{color:red;}#mermaid-svg-DI7SeLW28EBeYrr6 #dependencyStart,#mermaid-svg-DI7SeLW28EBeYrr6 #dependencyEnd{fill:#333333;stroke:#333333;stroke-width:1;}#mermaid-svg-DI7SeLW28EBeYrr6 .statediagramTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-DI7SeLW28EBeYrr6 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}资源加载完成ENTER / 点击确认角色倒计时结束P / ESC / 窗口失焦P / ESCKO / 时间到双方均未达到两胜一方达到两胜再次挑战menuselectcountdownplayingpauseroundovergameover

状态作用是否更新战斗menu主菜单和开始入口否select七角色选择否countdown回合重置与开战倒计时仅角色待机playing完整战斗逻辑是pause冻结游戏否roundover胜利、KO 落地和回合流转仅结算动作gameover最终比分和重新挑战否加载画面由 DOM 层负责,在背景资源完成或超时后淡出,因此没有占用运行时 Game.state。

6.1 为什么 KO 动画必须在回合结算继续更新

一个常见错误是:角色生命归零后立刻切换到结算,而结算状态不再更新角色。结果是败者冻结在半空中。

本项目在 roundover 中继续更新双方角色:

} else if (this.state === 'roundover') {

this.roundDelay -= dt;

this.p1.update(dt, {}, this.p2);

this.p2.update(dt, {}, this.p1);

}

胜者通过 victory 锁定胜利动作;败者通过 ko 分支继续处理重力、旋转、落地和扬尘。全局状态已经结束战斗,但局部表现仍然可以完整播放。

7. 数据驱动的角色系统

七名角色共享同一套 Fighter 逻辑,差异集中在角色数据中:

const ROSTER = [

{

name: '岚 · ARASHI',

title: '疾风拳士',

primary: '#167cff',

accent: '#56e8ff',

hairStyle: 0,

speed: 1.04,

jump: 1.02,

power: 1

},

// ...其余角色

];

这种结构有几个好处:

角色选择界面可以直接遍历数据生成卡片。创建角色时只需要合并阵营和出生位置。平衡调整不需要修改角色状态机。新增角色不会复制整套移动、攻击和碰撞代码。

创建角色的逻辑保持很薄:

createFighter(index, side) {

const x = side === 'left' ? 360 : 920;

return new Fighter({

...ROSTER[index],

rosterIndex: index,

side,

startX: x,

x

});

}

当前角色差异主要是速度、跳跃、力量、配色、发型和服装标记。后续可以把攻击表、投射物样式、特殊规则和 AI 权重也移动到角色数据中,形成真正的独立招式组。

8. Fighter 局部状态机

Fighter 同时处理输入、状态优先级、物理和局部动画。状态优先级非常重要:KO 必须覆盖全部输入,受击必须打断攻击,攻击期间不能被普通移动覆盖。

简化后的优先级如下:

KO

└─ 胜利锁定

└─ 受击硬直

└─ 冲刺

└─ 攻击

└─ 空中控制

└─ 地面输入

对应代码结构为连续的 if / else if:

if (this.ko) {

// KO 击飞、重力与落地

} else if (this.victory) {

// 锁定胜利姿势

} else if (this.hitstun > 0) {

// 受击硬直

} else if (this.dashTime > 0) {

// 冲刺

} else if (this.attack) {

// 攻击时间轴

} else {

// 读取普通输入

}

这种做法比“每个按键直接设置动作”稳定,因为一个角色不可能同时处于受击、冲刺和攻击状态。

9. 攻击时间轴

每个攻击都由数据描述:

const ATTACKS = {

jab: {

startup: 0.07,

active: 0.09,

recovery: 0.16,

damage: 6,

stun: 0.18,

reach: 82,

height: 48,

y: -120,

push: 250,

meter: 7

}

};

一个招式被拆为三个阶段:

启动 startup → 有效 active → 收招 recovery

不能命中 可以命中 不能命中

总时长为 startup + active + recovery。attackClock 从 0 开始累计,只有处于有效窗口时才生成攻击盒。

if (

this.attackClock < attack.startup ||

this.attackClock > attack.startup + attack.active

) {

return null;

}

9.1 防止一次攻击重复命中

如果每一帧都做重叠检测,一个持续 0.1 秒的攻击可能在 60 FPS 下造成 6 次伤害。项目使用 didHit 标记:

attackBox() {

if (!this.attack || this.didHit) return null;

// 返回当前攻击盒

}

攻击命中后立即设置 didHit = true,直到下一次攻击重新初始化。这种方法适合单段攻击。多段必杀可以进一步改为“已经命中过的目标 ID 集合”或“每一段独立命中编号”。

10. 碰撞系统

项目使用简单的 AABB,也就是轴对齐矩形碰撞。

每个角色拥有身体盒:

bodyBox() {

const crouch = this.crouching ? 42 : 0;

return {

x: this.x - 31,

y: this.y - 172 + crouch,

w: 62,

h: 172 - crouch

};

}

攻击处于有效窗口时生成攻击盒。碰撞判断只需要四个不分离条件:

const overlap =

hit.x < body.x + body.w &&

hit.x + hit.w > body.x &&

hit.y < body.y + body.h &&

hit.y + hit.h > body.y;

这种碰撞不追求精确贴合绘制轮廓,而追求稳定、低成本、易调试和容易平衡。格斗判定通常本来就不应完全跟随美术轮廓;过度精确会导致招式难以命中,玩家反而觉得判定不可信。

10.1 角色之间的分离

两个角色不能站在完全相同的位置。当中心距离小于阈值时,双方各自向外移动一半:

const min = 68;

const distance = p2.x - p1.x;

if (Math.abs(distance) < min) {

const push = (min - Math.abs(distance)) / 2;

const direction = Math.sign(distance) || 1;

p1.x -= push * direction;

p2.x += push * direction;

}

对称分离可以避免只推动一方导致的墙角抖动。

11. 地面、跳跃与空中攻击

角色使用最小化的纵向物理模型:

this.vy += gravity * dt;

this.y += this.vy * dt;

if (this.y >= FLOOR) {

this.y = FLOOR;

this.vy = 0;

this.grounded = true;

}

起跳本质上是赋予负向垂直速度:

this.vy = -760 * this.jump;

this.grounded = false;

空中状态保留水平方向输入,但速度降低为地面的 70%,防止玩家在空中瞬间改变落点。

空中攻击和地面攻击使用同一个攻击系统,只增加 aerial: true:

airKick: {

startup: 0.11,

active: 0.16,

recovery: 0.20,

damage: 10,

aerial: true

}

startAttack() 会检查攻击类型与 grounded 是否匹配。这让空中拳脚可以复用伤害、硬直、碰撞和连击系统,而不需要第二套空战逻辑。

12. 防御、伤害与资源

防御成立需要角色在地面、玩家按住防御键,而且角色面向攻击者。

格挡不会完全免伤,而是承受约 16% 的削血:

const chip = Math.max(1, Math.floor(damage * 0.16));

this.hp = Math.max(0, this.hp - chip);

这样可以避免玩家长期防守拖延时间,同时保留“防住重攻击仍然很划算”的策略。

能量通过攻击命中积累,范围限制在 0-100:

attacker.meter = clamp(

attacker.meter + attack.meter,

0,

100

);

能量弹需要至少 30 点能量,启动时立即扣除。先扣资源可以防止攻击过程中被打断后免费释放。

13. KO 倒地系统

当伤害使生命降至 0 时,不再进入普通受击状态,而是调用独立的 knockout():

knockout(attacker, push) {

this.ko = true;

this.attack = null;

this.blocking = false;

this.grounded = false;

this.state = 'koAir';

this.vx = Math.sign(this.x - attacker.x)

* Math.max(push * 1.35, 540);

this.vy = -650;

}

KO 序列分为四个阶段:

生命归零,强闪白并产生大量冲击粒子。角色向远离攻击者的方向击飞,并持续旋转。接触地面时触发强震屏和扬尘。切换到 koGround,保持躺倒直到回合结束。

KO 使用独立布尔状态,而不只是一个短暂动画名称。原因是 KO 会永久改变角色在本回合中的行为:禁止输入、禁止攻击、禁止恢复站立。

14. 程序化角色绘制

项目没有使用传统逐帧精灵图。角色由头部、躯干、手臂和腿部等图形实时绘制。

姿势数据只记录关键点:

const poses = {

idle: {

head: [0, -162],

body: [-2, -118],

hand1: [-38, -107],

hand2: [34, -128],

foot1: [-27, 0],

foot2: [29, 0]

}

};

绘制时根据端点推导肩部、髋部、肘部和膝部,再用粗线段和圆角连接形成肢体。同一套几何可以通过主色、次色、强调色、发型轮廓和胸前标记产生角色差异。

14.1 镜像

角色动作只设计一个方向,通过 Canvas 变换完成左右镜像:

ctx.translate(this.x, this.y);

ctx.scale(this.facing, 1);

facing 为 1 或 -1。绘制和攻击盒都参考同一个方向值,避免出现“人物已经转身,但攻击仍然打向旧方向”的问题。

14.2 程序化动画的边界

程序化角色适合原型和风格化作品,但也有明显限制:

复杂衣物和面部表演难以表现。关键姿势之间缺少专业动画师控制的运动弧线。角色数量增加后,公共骨架会造成动作同质化。

后续可以保留当前状态机与碰撞系统,只把 draw() 替换为精灵表、Spine 或自定义骨骼渲染器。

15. 粒子、震屏与投射物

Particle 只包含位置、速度、重力、寿命、尺寸和颜色:

class Particle {

update(dt) {

this.x += this.vx * dt;

this.y += this.vy * dt;

this.vy += this.gravity * dt;

this.life -= dt;

}

}

命中火花、能量尾迹和 KO 扬尘都复用这个类,只调整颜色、速度、寿命和重力。

屏幕震动在整帧绘制前给 Canvas 一个随机偏移:

const shakeX = rand(-shake, shake);

const shakeY = rand(-shake * 0.45, shake * 0.45);

ctx.translate(shakeX, shakeY);

震动值使用指数衰减,因此在不同帧率下具有更一致的结束速度。

能量弹是独立的 Projectile 对象,拥有所有者、位置、速度、半径、寿命和回收标记。每帧更新后统一过滤:

projectiles.forEach(p => p.update(dt));

projectiles = projectiles.filter(p => !p.dead);

“标记后统一回收”比在遍历数组时直接删除元素安全,可以避免跳过后续对象。

16. 电脑 AI

AI 没有使用路径搜索或机器学习,而是定时做一次基于距离和威胁的规则决策。

#mermaid-svg-of5dih1MRJW1HEDc{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-of5dih1MRJW1HEDc .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-of5dih1MRJW1HEDc .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-of5dih1MRJW1HEDc .error-icon{fill:#552222;}#mermaid-svg-of5dih1MRJW1HEDc .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-of5dih1MRJW1HEDc .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-of5dih1MRJW1HEDc .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-of5dih1MRJW1HEDc .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-of5dih1MRJW1HEDc .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-of5dih1MRJW1HEDc .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-of5dih1MRJW1HEDc .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-of5dih1MRJW1HEDc .marker{fill:#333333;stroke:#333333;}#mermaid-svg-of5dih1MRJW1HEDc .marker.cross{stroke:#333333;}#mermaid-svg-of5dih1MRJW1HEDc svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-of5dih1MRJW1HEDc p{margin:0;}#mermaid-svg-of5dih1MRJW1HEDc .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-of5dih1MRJW1HEDc .cluster-label text{fill:#333;}#mermaid-svg-of5dih1MRJW1HEDc .cluster-label span{color:#333;}#mermaid-svg-of5dih1MRJW1HEDc .cluster-label span p{background-color:transparent;}#mermaid-svg-of5dih1MRJW1HEDc .label text,#mermaid-svg-of5dih1MRJW1HEDc span{fill:#333;color:#333;}#mermaid-svg-of5dih1MRJW1HEDc .node rect,#mermaid-svg-of5dih1MRJW1HEDc .node circle,#mermaid-svg-of5dih1MRJW1HEDc .node ellipse,#mermaid-svg-of5dih1MRJW1HEDc .node polygon,#mermaid-svg-of5dih1MRJW1HEDc .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-of5dih1MRJW1HEDc .rough-node .label text,#mermaid-svg-of5dih1MRJW1HEDc .node .label text,#mermaid-svg-of5dih1MRJW1HEDc .image-shape .label,#mermaid-svg-of5dih1MRJW1HEDc .icon-shape .label{text-anchor:middle;}#mermaid-svg-of5dih1MRJW1HEDc .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-of5dih1MRJW1HEDc .rough-node .label,#mermaid-svg-of5dih1MRJW1HEDc .node .label,#mermaid-svg-of5dih1MRJW1HEDc .image-shape .label,#mermaid-svg-of5dih1MRJW1HEDc .icon-shape .label{text-align:center;}#mermaid-svg-of5dih1MRJW1HEDc .node.clickable{cursor:pointer;}#mermaid-svg-of5dih1MRJW1HEDc .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-of5dih1MRJW1HEDc .arrowheadPath{fill:#333333;}#mermaid-svg-of5dih1MRJW1HEDc .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-of5dih1MRJW1HEDc .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-of5dih1MRJW1HEDc .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-of5dih1MRJW1HEDc .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-of5dih1MRJW1HEDc .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-of5dih1MRJW1HEDc .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-of5dih1MRJW1HEDc .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-of5dih1MRJW1HEDc .cluster text{fill:#333;}#mermaid-svg-of5dih1MRJW1HEDc .cluster span{color:#333;}#mermaid-svg-of5dih1MRJW1HEDc 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-of5dih1MRJW1HEDc .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-of5dih1MRJW1HEDc rect.text{fill:none;stroke-width:0;}#mermaid-svg-of5dih1MRJW1HEDc .icon-shape,#mermaid-svg-of5dih1MRJW1HEDc .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-of5dih1MRJW1HEDc .icon-shape p,#mermaid-svg-of5dih1MRJW1HEDc .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-of5dih1MRJW1HEDc .icon-shape .label rect,#mermaid-svg-of5dih1MRJW1HEDc .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-of5dih1MRJW1HEDc .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-of5dih1MRJW1HEDc .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-of5dih1MRJW1HEDc :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}是否是否是否AI 决策时钟归零玩家正在攻击且距离近?概率防御距离大于 245?接近 / 冲刺 / 概率必杀距离小于 82?概率后退拳 / 脚 / 防御 / 跳跃

AI 每隔 0.08-0.18 秒更新一次决策,而不是每帧重算。这既降低计算量,也避免 AI 每一帧改变主意,产生抖动和超人反应。

规则 AI 的重点不是“最强”,而是“看起来在思考”。随机区间、决策间隔和错误概率比纯粹追求最优行动更重要。

后续可以给每个角色增加不同权重,例如玄武偏向防守和重击,月璃偏向跳跃与空中脚。

17. Web Audio 合成音效

项目通过 OscillatorNode、GainNode、滤波器和随机噪声实时生成音效。

tone(freq, duration, type, volume, slide) {

oscillator.frequency.setValueAtTime(freq, now);

oscillator.frequency.exponentialRampToValueAtTime(

freq * slide,

now + duration

);

}

不同事件组合不同声源:

轻击:短噪声加中频方波快速下滑。重击:更长噪声加低频冲击。格挡:短促高频方波上滑。必杀:锯齿波从低频上升到高频。

浏览器禁止页面在没有用户交互时自动播放声音,因此 AudioContext 延迟到玩家首次点击或确认时创建和恢复。

18. 输入设计

项目同时维护两个集合:

keys:当前持续按住的键。pressed:本帧刚刚按下的键。

持续动作使用 keys:

left: keys.has('a')

单次动作使用 pressed:

jump: pressed.has('w')

每帧结束后清空 pressed。这样按住 W 不会在角色刚落地时再次自动起跳,按住攻击键也不会无限自动连发。

19. 响应式显示

Canvas 内部逻辑分辨率固定为 1280 × 720,所有游戏坐标都基于这个空间计算。CSS 只负责等比缩放:

.game-shell {

width: min(100%, 1280px);

aspect-ratio: 16 / 9;

}

canvas {

width: 100%;

height: 100%;

}

固定逻辑分辨率的好处是:碰撞盒、出生点、HUD 和粒子无需针对每个窗口重新计算。指针输入则根据 Canvas 实际显示尺寸换算回逻辑坐标。

20. 自动化测试

Canvas 游戏很难用普通 DOM 测试。项目的烟雾测试使用 Node.js vm 模块,并提供最小化的浏览器和 Canvas 替身:

模拟 document、window 和 Image。用空函数代理 Canvas 2D API。模拟 Web Audio 节点。捕获 requestAnimationFrame。通过 ?debug 暴露游戏实例。

测试覆盖:

游戏实例能够启动。七角色数据和角色选择可用。玩家选择正确应用,电脑不会选择同一角色。倒计时能够进入战斗。攻击碰撞能够扣血。跳跃拳和跳跃脚可以启动。生命归零触发击飞。回合结算期间能够完成落地并进入 koGround。

运行方式:

node --check .\game.js

node .\tests\smoke-test.js

当前期望输出:

PASS: 7 fighters, selection, air attacks, hit detection and KO fall confirmed 拼多多帮助力入口

这种测试不能代替真实浏览器中的视觉验收,但可以快速发现状态流转、数值异常、碰撞失效和 KO 冻结等逻辑回归。

21. 性能策略

当前规模下 Canvas 2D 足够流畅,但仍然采用了一些低成本优化:

背景图只加载一次,每帧直接绘制。AI 降频决策,而不是每帧生成新策略。粒子和投射物使用生命周期过滤统一回收。碰撞只使用矩形和圆形近似。dt 上限防止卡顿后物理爆炸。只在必要时创建粒子;暂停时停止所有动态更新。

如果粒子数量继续增加,可以引入对象池,避免频繁创建和垃圾回收。若角色美术改为大型序列帧,则需要使用精灵图集减少图片切换。

22. 离线发布与 EXE 封装

HTML 发布版不需要本地服务器。将以下内容保持在同一目录即可:

index.html

styles.css

game.js

assets/

Windows 用户可以双击 启动游戏.bat,其他平台直接使用浏览器打开 index.html。

如果需要封装为 EXE,有两条常见路线:

Electron

优点:生态成熟、兼容性好、调试方便。缺点:安装包较大,携带完整 Chromium 和 Node.js。接入方式:创建窗口并加载项目中的 index.html。

Tauri

优点:安装包更小,使用系统 WebView。缺点:需要 Rust 工具链,不同系统 WebView 可能存在差异。接入方式:把当前目录作为前端静态资源,无需改写战斗逻辑。

无论使用哪种方式,都应保持“Web 游戏本身可以独立运行”,封装层只负责窗口、图标和安装。

23. 当前架构的不足

23.1 game.js 体积偏大

可以拆分为:

src/

├─ core/Game.js

├─ entities/Fighter.js

├─ entities/Projectile.js

├─ effects/Particle.js

├─ audio/Synth.js

├─ data/attacks.js

├─ data/roster.js

├─ systems/Input.js

├─ systems/AI.js

└─ render/UI.js

如果仍要保持“无需构建”,可以使用浏览器原生 ES Modules;需要压缩和缓存控制时再引入 Vite。

23.2 招式仍然由全角色共享

目前角色区别主要是基础属性和视觉。下一步可以让 ROSTER 引用独立招式表:

{

id: 'tsukiri',

stats: { speed: 1.08, jump: 1.13, power: 0.97 },

moveset: TSUKIRI_MOVES,

aiProfile: 'aerial'

}

23.3 状态使用字符串

字符串直观,但容易拼写错误。可以集中定义常量:

const GAME_STATE = Object.freeze({

MENU: 'menu',

SELECT: 'select',

COUNTDOWN: 'countdown',

PLAYING: 'playing',

PAUSE: 'pause',

ROUND_OVER: 'roundover',

GAME_OVER: 'gameover'

});

23.4 缺少调试可视化

开发格斗判定时,建议增加调试模式:

身体盒用绿色矩形。攻击盒用红色矩形。显示当前动作、攻击阶段和硬直剩余时间。显示 FPS、粒子数和投射物数。

这会显著提高招式平衡和碰撞问题排查效率。

24. 可复用的经验

经验一:先做状态闭环,再堆内容

一个只有两名角色但能“开始、战斗、KO、结算、重开”的版本,比十名角色却没有完整流程的版本更接近真正的游戏。

经验二:打击感是多个小系统的乘积

单独增加粒子或音效不会自动获得打击感。停帧、击退、震屏、闪白、声音和动画姿势需要在同一时刻发生。

经验三:攻击必须数据化

启动、有效、收招、伤害、硬直、范围和资源收益应该是数据,而不是散落在输入判断中的魔法数字。

经验四:结算状态也可能需要物理更新

“战斗结束”不等于“画面停止”。KO、胜利、镜头和比分动画通常发生在结算阶段。

经验五:美术表现与战斗判定应解耦

角色以后可以从程序化骨骼替换为精灵图,但状态机、攻击时间轴和碰撞盒仍然应该继续工作。

经验六:浏览器游戏首先应该是一个普通文件夹

把核心运行能力和打包器解耦,既方便调试,也能避免未来更换 Electron、Tauri 或 Web 部署方式时重写游戏。

25. 总结

《霓虹斗场》的核心不是使用了多复杂的算法,而是把少量系统组合成了完整闭环:

状态机

+ 数据驱动角色

+ 攻击时间轴

+ 简单可靠的碰撞

+ 程序化动画

+ 粒子 / 停帧 / 震屏 / 音效

+ AI 决策

+ 离线分发

= 一款可以直接游玩的横版格斗游戏

对于中小型 Web 游戏原型,原生 Canvas 仍然是很有价值的选择。它迫使开发者理解游戏循环、时间、状态、碰撞和渲染之间的关系;这些知识在迁移到任何游戏引擎后依然适用。

想下载并体验游戏

绿泡泡搜索 “码来的小朋友” 直接获取资源和查看文章

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧