一天做出:鸿蒙 + AI 游戏 Demo


子玥酱 (掘金 / 知乎 / CSDN / 简书 同名)

大家好,我是 子玥酱,一名长期深耕在一线的前端程序媛 👩‍💻。曾就职于多家知名互联网大厂,目前在某国企负责前端软件研发相关工作,主要聚焦于业务型系统的工程化建设与长期维护。

我持续输出和沉淀前端领域的实战经验,日常关注并分享的技术方向包括 前端工程化、小程序、React / RN、Flutter、跨端方案,

在复杂业务落地、组件抽象、性能优化以及多端协作方面积累了大量真实项目经验。

技术方向: 前端 / 跨端 / 小程序 / 移动端工程化 内容平台: 掘金、知乎、CSDN、简书 创作特点: 实战导向、源码拆解、少空谈多落地 **文章状态:**长期稳定更新,大量原创输出

我的内容主要围绕 前端技术实战、真实业务踩坑总结、框架与方案选型思考、行业趋势解读 展开。文章不会停留在"API 怎么用",而是更关注为什么这么设计、在什么场景下容易踩坑、真实项目中如何取舍,希望能帮你在实际工作中少走弯路。

子玥酱 · 前端成长记录官 ✨

👋 如果你正在做前端,或准备长期走前端这条路

📚 关注我,第一时间获取前端行业趋势与实践总结

🎁 可领取 11 类前端进阶学习资源 (工程化 / 框架 / 跨端 / 面试 / 架构)

💡 一起把技术学"明白",也用"到位"

持续写作,持续进阶。

愿我们都能在代码和生活里,走得更稳一点 🌱

文章目录

引言

很多人看到"鸿蒙 + AI 游戏",第一反应是:

"这是不是很复杂?要不要几周甚至几个月?"

其实不需要,如果你用对方法,在 HarmonyOS 上:

1 天就可以做出一个"可运行 + 有 AI + 可扩展"的小游戏 Demo

目标:1 天做出 AI 游戏 Demo

内容:架构 + 代码 + 步骤 + 扩展

一、Demo 目标

我们不要做复杂游戏,只做一个:

AI 对话 + 轻交互游戏

功能设计
复制代码
玩家进入场景
      ↓
和 NPC 对话(AI)
      ↓
NPC 根据对话给任务
      ↓
玩家完成任务 → 得分

核心亮点:

  • 有 UI
  • 有状态
  • 有 AI
  • 可扩展

二、整体架构

推荐结构

复制代码
entry
 ├─ pages
 │   └─ GamePage.ets
 │
 ├─ components
 │   └─ ChatBox.ets
 │
 ├─ services
 │   ├─ GameService.ets
 │   └─ AIService.ets
 │
 ├─ store
 │   └─ GameStore.ets
 │
 ├─ models
 │   └─ GameModel.ets
 │
 └─ agent
     └─ NPCAgent.ets

核心链路:

复制代码
UI → Service → Store → Agent → AI

三、核心模块实现

1、数据模型

ts 复制代码
// models/GameModel.ets
export interface GameState {
  score: number
  messages: string[]
  currentTask: string
}

2、状态管理

ts 复制代码
// store/GameStore.ets
export class GameStore {

  state: GameState = {
    score: 0,
    messages: [],
    currentTask: ''
  }

  update(partial: Partial<GameState>) {
    this.state = { ...this.state, ...partial }
  }

}

export const gameStore = new GameStore()

3、AI 服务

ts 复制代码
// services/AIService.ets
export class AIService {

  async chat(message: string): Promise<string> {

    // 模拟 AI(可替换真实接口)
    if (message.includes("任务")) {
      return "请去收集 3 个金币"
    }

    return "你好冒险者!"
  }

}

export const aiService = new AIService()

后续可以接真实大模型 API。

4、Agent

ts 复制代码
// agent/NPCAgent.ets
import { aiService } from '../services/AIService'
import { gameStore } from '../store/GameStore'

export class NPCAgent {

  async talk(input: string) {

    const reply = await aiService.chat(input)

    gameStore.update({
      messages: [...gameStore.state.messages, `NPC: ${reply}`],
      currentTask: reply.includes("收集") ? "collect" : ''
    })

  }

}

export const npcAgent = new NPCAgent()

这里就是"AI → 游戏逻辑"的桥梁。

5、游戏逻辑

ts 复制代码
// services/GameService.ets
import { gameStore } from '../store/GameStore'

export class GameService {

  sendMessage(text: string) {
    gameStore.update({
      messages: [...gameStore.state.messages, `玩家: ${text}`]
    })
  }

  completeTask() {
    if (gameStore.state.currentTask === "collect") {
      gameStore.update({
        score: gameStore.state.score + 10,
        currentTask: ''
      })
    }
  }

}

export const gameService = new GameService()

6、UI 组件

ts 复制代码
// components/ChatBox.ets
@Component
export struct ChatBox {

  @Prop messages: string[]

  build() {
    Column() {
      ForEach(this.messages, msg => {
        Text(msg)
      })
    }
  }

}

7、主页面

ts 复制代码
// pages/GamePage.ets
import { gameStore } from '../store/GameStore'
import { gameService } from '../services/GameService'
import { npcAgent } from '../agent/NPCAgent'
import { ChatBox } from '../components/ChatBox'

@Entry
@Component
struct GamePage {

  @State state = gameStore.state
  @State input: string = ""

  async send() {
    gameService.sendMessage(this.input)
    await npcAgent.talk(this.input)

    this.state = gameStore.state
    this.input = ""
  }

  build() {
    Column() {

      Text(`Score: ${this.state.score}`)

      ChatBox({ messages: this.state.messages })

      TextInput({ text: this.input })
        .onChange(v => this.input = v)

      Button("发送")
        .onClick(() => this.send())

      Button("完成任务")
        .onClick(() => {
          gameService.completeTask()
          this.state = gameStore.state
        })

    }
  }

}

四、一天开发时间拆解

上午(3小时)

  • 搭项目结构
  • 写 Store + Service
  • 搭 UI

下午(3小时)

  • 写 AIService(可 mock)
  • 写 Agent
  • 接入对话

晚上(2小时)

  • 调整 UI
  • 加简单玩法
  • 测试

8 小时即可完成。

五、升级方向

1、接入真实 AI

ts 复制代码
await fetch("LLM API")

2、多端联动

ts 复制代码
distributedSync.send(gameStore.state)

3、AI 剧情生成

ts 复制代码
ai.generateStory(context)

4、NPC 多角色

ts 复制代码
agentMap[npcId].talk()

5、任务系统升级

ts 复制代码
taskEngine.run()

六、这个 Demo 的意义

你做的不是一个简单小游戏,而是:

一个"AI 游戏最小模型"

复制代码
UI(ArkUI)
+ State(Store)
+ Logic(Service)
+ AI(Agent)

这个结构可以扩展成:

  • RPG 游戏
  • AI 剧情游戏
  • 多端游戏

总结

核心价值不在复杂度,而在:

它验证了一种全新的游戏架构

可以用一句话总结:

复制代码
传统游戏 = 逻辑驱动
AI 游戏 = 状态 + Agent 驱动

在 HarmonyOS 上,这种模式可以天然支持:

  • 多端
  • AI
  • 分布式

最后:不要一开始就做"大游戏",先做"可跑的 AI Demo"。

因为:Demo 才是你进入这个赛道的"入场券"。

相关推荐
冬奇Lab14 小时前
Agent 系列(23):Web Agent——让 Agent 真正浏览网页
人工智能·llm·agent
冬奇Lab14 小时前
每日一个开源项目(第135篇):codebase-memory-mcp - 给 AI Agent 一张代码库的知识图谱
人工智能·开源·llm
IT_陈寒17 小时前
JavaScript的闭包把我坑惨了,说好的内存会自动回收呢?
前端·人工智能·后端
金銀銅鐵19 小时前
用 Pygame 实现 15 puzzle
python·数学·游戏
jooloo21 小时前
Codex 间歇性 400 之谜:一条对话里,它为什么有时候用 chat/completions,有时候切到 responses?
人工智能
用户51914958484521 小时前
OpenSSL PKCS#12 PBMAC1 堆栈缓冲区溢出漏洞 (CVE-2025-11187) 分析与验证
人工智能·aigc
用户5191495848451 天前
HP Sound Research SECOMNService 权限提升漏洞利用工具
人工智能·aigc
用户018349301691 天前
给 AI 智能体能力包一层 BFF,前端只调一个接口
人工智能