从 0 到 1:用Cursor+DeepSeek开发AI恋爱养成游戏

摘要:本文通过Cursor工具链与DeepSeek-V3大模型,实现一个包含6个AI角色的恋爱模拟游戏。全程采用TypeScript+AI生成代码模式,20分钟完成核心功能开发。 

源码可以关注作者私聊发送。

从 0 到 1:用Cursor+DeepSeek开发AI恋爱养成游戏_第1张图片

从 0 到 1:用Cursor+DeepSeek开发AI恋爱养成游戏_第2张图片

一、思路实现

1.1 核心指令

受到斯坦福开源项目《AI小镇》的启发,我一直想开发一个类似的小游戏,让AI上演甜蜜的恋爱剧情。之前看过综艺《心动的信号》给了我灵感,今天借助Cursor和Claude 4.0,仅用20分钟就完成了这个想法。下面是我给cursor的提示词:

我需要你帮我写一个恋爱小镇,就是让ai去谈恋爱,故事背景:六个ai,三男三女,让他们每天有交集,场景是一个5个人,其中两个是闺蜜住一间。场景素材你需要去下载,技术栈用效率最高的实现。ai对话用deepseek v3。

 1.2 技术栈

下面是cursor生成的README.md文件,当然你也可以使用下面这段话当提示词。

一个让AI角色们自主谈恋爱的模拟游戏!六个AI角色(三男三女)在一个共享的房屋中生活,通过DeepSeek v3 AI驱动他们的对话和互动。

✨ 特性

  •  AI驱动对话: 使用DeepSeek v3 API生成自然、有趣的对话

  •  六个独特角色: 三男三女,每个都有独特的性格和背景

  •  真实生活场景: 客厅、厨房、卧室、浴室、花园等生活空间

  •  动态关系系统: 角色间的好感度会根据互动实时变化

  •  实时互动: 通过Socket.io实现实时游戏状态同步

  •  现代UI: 使用React + Tailwind CSS + Framer Motion

角色介绍

男性角色

  • 陈浩 ‍ - 25岁程序员,温柔幽默体贴

  • 李俊 ‍♂️ - 27岁健身教练,外向运动乐观

  • 王涛  - 24岁艺术家,艺术安静敏感

女性角色

  • 小雨  - 23岁大学生,可爱活泼善良

  • 晓雪  - 24岁图书管理员,优雅知性温柔(小雨的闺蜜室友)

  • 美美  - 26岁时尚博主,独立时尚自信

️ 技术栈

前端

  • React 18 + TypeScript

  • Vite - 快速构建工具

  • Tailwind CSS - 现代CSS框架

  • Framer Motion - 动画库

  • Zustand - 状态管理

  • Socket.io Client - 实时通信

后端

  • Node.js + Express

  • TypeScript

  • Socket.io - 实时通信

  • DeepSeek v3 API - AI对话生成

 二、细节打磨

我需要对细节进行优化。下图是Cursor生成的第一版设计,目前页面整体协调性不足。建议将室内布局调整为2D平面图形式。此外,当前页面背景过于抢眼,会影响文字的可读性。

从 0 到 1:用Cursor+DeepSeek开发AI恋爱养成游戏_第3张图片

继续使用Cursor调整页面布局时,它会提供素材案例参考,但无法直接下载。需要手动下载素材后告知文件路径。目前我尚未研究Cursor与MCP结合实现图片素材下载和设计的功能,这部分内容后续会进行深入探索。

所有房间和场景做成规格布局,就比如户型图那种,需要房间素材需要到游戏素材网去下载 

如果你对内容有不满意的地方,可以告诉cursor具体哪里需要调整,或者提供相关素材,cursor会帮你修改到满意为止。

 从 0 到 1:用Cursor+DeepSeek开发AI恋爱养成游戏_第4张图片

 三、DeepSeek申请API keys

DeepSeek 开放平台

注册登录后充值10元即可长期使用,看一场浪漫的数字生命恋爱也很值得。我选择的是v3对话模型,这是默认设置。如果换成R1模型,整个对话过程都会像纯情少年初次恋爱般认真思考如何回应对方。

从 0 到 1:用Cursor+DeepSeek开发AI恋爱养成游戏_第5张图片

点击"API keys"申请一个密钥,然后将生成的key复制到配置文件中即可。从 0 到 1:用Cursor+DeepSeek开发AI恋爱养成游戏_第6张图片

 四、核心代码分析

4.1 场景人物设定

场景设定:

  1. 空间布局:包含7个功能区域,其中3间为卧室
  2. 人物关系:
    • 小雨与晓雪为闺蜜关系,两人共用一间卧室
    • 其余卧室均为单人使用
  3. 人物设定:
    • 其他关键属性
    • 性格特征
    • 年龄范围
private initializeGameState(): GameState {
    const locations: Location[] = [
      { id: 'living_room', name: '客厅', type: 'living_room', capacity: 6, currentCharacters: [] },
      { id: 'kitchen', name: '厨房', type: 'kitchen', capacity: 3, currentCharacters: [] },
      { id: 'bedroom1', name: '卧室1 (小雨&晓雪)', type: 'bedroom', capacity: 2, currentCharacters: [] },
      { id: 'bedroom2', name: '卧室2 (王涛)', type: 'bedroom', capacity: 2, currentCharacters: [] },
      { id: 'bedroom3', name: '卧室3 (美美)', type: 'bedroom', capacity: 2, currentCharacters: [] },
      { id: 'bathroom', name: '浴室', type: 'bathroom', capacity: 1, currentCharacters: [] },
      { id: 'garden', name: '花园', type: 'garden', capacity: 4, currentCharacters: [] }
    ];

    const characters: Character[] = [
      {
        id: 'male1',
        name: '陈浩',
        gender: 'male',
        age: 25,
        personality: ['温柔', '幽默', '体贴'],
        appearance: '高大帅气的程序员',
        avatar: '/src/static/images/man1.png',
        currentLocation: locations[0], // 客厅
        mood: 75,
        energy: 80,
        relationship: {},
        schedule: [],
        currentActivity: '看电视',
        thoughts: '今天感觉不错,希望能和大家聊聊天',
        backstory: '是一名软件工程师,喜欢编程和音乐,性格温和内敛。'
      },
      {
        id: 'male2',
        name: '李俊',
        gender: 'male',
        age: 27,
        personality: ['外向', '运动', '乐观'],
        appearance: '阳光运动型男生',
        avatar: '/src/static/images/man2.png',
        currentLocation: locations[6], // 花园
        mood: 85,
        energy: 95,
        relationship: {},
        schedule: [],
        currentActivity: '晨跑',
        thoughts: '新的一天开始了,充满活力!',
        backstory: '健身教练,热爱运动,总是精力充沛,很受大家欢迎。'
      },
      {
        id: 'male3',
        name: '王涛',
        gender: 'male',
        age: 24,
        personality: ['艺术', '安静', '敏感'],
        appearance: '文艺青年',
        avatar: '/src/static/images/man3.png',
        currentLocation: locations[3], // 卧室2
        mood: 60,
        energy: 70,
        relationship: {},
        schedule: [],
        currentActivity: '画画',
        thoughts: '想要创作一些美好的作品',
        backstory: '自由艺术家,喜欢绘画和诗歌,有些内向但很有才华。'
      },
      {
        id: 'female1',
        name: '小雨',
        gender: 'female',
        age: 23,
        personality: ['可爱', '活泼', '善良'],
        appearance: '甜美可爱的女孩',
        avatar: '/src/static/images/woman1.png',
        currentLocation: locations[2], // 卧室1
        mood: 80,
        energy: 85,
        relationship: {},
        schedule: [],
        currentActivity: '化妆',
        thoughts: '今天要美美的!',
        backstory: '大学生,学习设计专业,性格开朗活泼,很容易交朋友。'
      },
      {
        id: 'female2',
        name: '晓雪',
        gender: 'female',
        age: 24,
        personality: ['优雅', '知性', '温柔'],
        appearance: '优雅知性美女',
        avatar: '/src/static/images/woman2.png',
        currentLocation: locations[2], // 卧室1
        mood: 70,
        energy: 75,
        relationship: {},
        schedule: [],
        currentActivity: '读书',
        thoughts: '今天想读完这本小说',
        backstory: '图书管理员,喜欢阅读和写作,是小雨的闺蜜室友。'
      },
      {
        id: 'female3',
        name: '美美',
        gender: 'female',
        age: 26,
        personality: ['独立', '时尚', '自信'],
        appearance: '时尚独立女性',
        avatar: '/src/static/images/woman3.png',
        currentLocation: locations[4], // 卧室3
        mood: 78,
        energy: 82,
        relationship: {},
        schedule: [],
        currentActivity: '护肤',
        thoughts: '作为独立女性,要好好爱自己',
        backstory: '时尚博主,经济独立,追求自由生活,有自己的事业。'
      }
    ];

    // 初始化角色关系
    characters.forEach(char => {
      characters.forEach(other => {
        if (other.id !== char.id) {
          char.relationship[other.id] = Math.floor(Math.random() * 20) + 10; // 初始好感度 10-30
        }
      });
    });

    // 更新位置中的角色
    characters.forEach(char => {
      const location = locations.find(loc => loc.id === char.currentLocation.id);
      if (location) {
        location.currentCharacters.push(char.id);
      }
    });

    return {
      currentDay: 1,
      currentTime: '08:00',
      characters,
      locations,
      conversations: [],
      events: []
    };
  }

4.2 AI智能体互动模块

AI行为管理的核心功能模块,涵盖异常行为处理、交互响应以及自主决策机制。

private async processAIBehaviors(): Promise {
    console.log('\n 开始处理AI行为...');
    
    // 1. 处理角色移动
    await this.processCharacterMovement();
    
    // 2. 处理角色互动
    await this.processCharacterInteractions();
    
    // 3. 处理独自行为
    await this.processSoloActivities();
    
    // 4. 更新角色状态
    this.updateCharacterStates();
    
    console.log(`✅ AI行为处理完成,当前有 ${this.gameState.conversations.length} 个对话,${this.gameState.conversations.reduce((total, conv) => total + conv.messages.length, 0)} 条消息`);
    
    this.broadcastGameState();
  }

4.3 AI对话决策管理

通过对话指令的构建,无论是橘色互动还是对话交流,都能自动关联相关信息,在buildPrompt方法中,AI会获得角色的完整记忆信息。


  private buildPrompt(
    character: Character, 
    context: string, 
    otherCharacters: Character[], 
    conversationHistory: string
  ): string {
    const relationshipInfo = otherCharacters.map(other => {
      const relationship = character.relationship[other.id] || 0;
      return `${other.name}(${other.avatar}): 好感度${relationship}`;
    }).join(', ');

    return `
角色信息:
- 姓名:${character.name}
- 性别:${character.gender === 'male' ? '男' : '女'}
- 年龄:${character.age}岁
- 性格:${character.personality.join('、')}
- 外貌:${character.appearance}
- 背景:${character.backstory}
- 当前心情:${character.mood}/100
- 当前能量:${character.energy}/100
- 当前想法:${character.thoughts}
- 当前活动:${character.currentActivity}
- 当前位置:${character.currentLocation.name}

人际关系:${relationshipInfo}

当前情境:${context}

${conversationHistory ? `对话历史:\n${conversationHistory}` : ''}

请以${character.name}的身份,根据以上信息生成合适的回应。要体现角色的性格特点,考虑当前的心情和人际关系。
`;
  }

  private parseAIResponse(aiResponse: string): AIResponse {
    try {
      const parsed = JSON.parse(aiResponse);
      return {
        content: parsed.content || '...',
        emotion: parsed.emotion || 'neutral',
        action: parsed.action,
        thoughtProcess: parsed.thoughtProcess
      };
    } catch (error) {
      console.log(`⚠️  JSON解析失败,尝试文本解析`);
      // 如果解析失败,尝试提取内容
      const content = aiResponse.substring(0, 200).replace(/[\r\n]+/g, ' ').trim();
      return {
        content: content || '嗯...',
        emotion: 'neutral'
      };
    }
  }

五、后记

AI的发展为创意探索开辟了新天地,我深信这项技术将助力人类迈向更美好的明天。近期我将专注于游戏设计领域的深造,不断完善这款恋爱游戏的细节:提升美术品质,优化角色动作表现,让交互体验更加真实自然。后续我会持续更新游戏内容,并考虑开放部分素材资源。欢迎关注我的创作进展!

你可能感兴趣的:(副业项目,人工智能,AI智能体,Python,DeepSeek,Cursor,Claude)