Trae更新后使用分享

84 阅读2分钟

我正在参加Trae「超级体验官」创意实践征文,本文所使用的 Trae 免费下载链接:www.trae.com.cn/?utm_source…

以下是更新后Trae的新版本介绍

image.png

image.png

image.png

image.png 以下是我使用他的案例:他可以根据你页面选中的某些行进行分析帮你准确帮你定位,你想要解决的问题

image.png

以下今天我就用 Trea 尝试生成一个经典小游戏 —— 推箱子(Sokoban) ,并分享我的过程和体验。

项目需求说明

推箱子游戏玩法如下:

  • 游戏在一个固定的二维网格上进行(如 7x7)。
  • 玩家(P)可以向上、下、左、右移动。
  • 当玩家遇到箱子(B)时,可以将箱子推动一个单位。
  • 箱子必须被推到目标位置(T)才能完成游戏。
  • 玩家不能穿过墙壁(W)或推多个箱子。

核心代码实现

1. App.vue

vue
复制编辑
<template>
  <div class="container">
    <h1>推箱子小游戏</h1>
    <GameBoard />
  </div>
</template>

<script setup>
import GameBoard from './GameBoard.vue'
</script>

<style>
.container {
  text-align: center;
  font-family: sans-serif;
}
</style>

2. GameBoard.vue

vue
复制编辑
<template>
  <div class="board">
    <div
      v-for="(row, y) in grid"
      :key="y"
      class="row"
    >
      <div
        v-for="(cell, x) in row"
        :key="x"
        class="cell"
        :class="getClass(cell)"
      >
        <span v-if="player.x === x && player.y === y">🧍</span>
        <span v-else-if="isBox(x, y)">📦</span>
        <span v-else-if="cell === 'T'">🎯</span>
        <span v-else-if="cell === 'W'">🧱</span>
      </div>
    </div>
    <p v-if="gameWon" class="win">🎉 恭喜通关!</p>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'

const grid = ref([
  ['W','W','W','W','W','W','W'],
  ['W',' ',' ',' ',' ','T','W'],
  ['W',' ','W','W',' ',' ','W'],
  ['W',' ',' ','B',' ',' ','W'],
  ['W',' ',' ',' ','P',' ','W'],
  ['W','W','W','W','W','W','W'],
])

const player = ref({ x: 4, y: 4 })
const boxes = ref([{ x: 3, y: 3 }])
const targets = [{ x: 5, y: 1 }]
const gameWon = ref(false)

const isBox = (x, y) => boxes.value.some(b => b.x === x && b.y === y)

const move = (dx, dy) => {
  const nx = player.value.x + dx
  const ny = player.value.y + dy

  const box = boxes.value.find(b => b.x === nx && b.y === ny)
  const nextNext = boxes.value.find(b => b.x === nx + dx && b.y === ny + dy)

  if (grid.value[ny][nx] === 'W') return
  if (box) {
    if (grid.value[ny + dy][nx + dx] === 'W' || nextNext) return
    box.x += dx
    box.y += dy
  }

  player.value.x = nx
  player.value.y = ny

  checkWin()
}

const checkWin = () => {
  gameWon.value = boxes.value.every(box =>
    targets.some(t => t.x === box.x && t.y === box.y)
  )
}

onMounted(() => {
  window.addEventListener('keydown', e => {
    switch (e.key) {
      case 'ArrowUp': move(0, -1); break
      case 'ArrowDown': move(0, 1); break
      case 'ArrowLeft': move(-1, 0); break
      case 'ArrowRight': move(1, 0); break
    }
  })
})

const getClass = (cell) => {
  return {
    cell: true,
    wall: cell === 'W',
    target: cell === 'T'
  }
}
</script>

<style scoped>
.board {
  display: inline-block;
  margin-top: 20px;
}
.row {
  display: flex;
}
.cell {
  width: 40px;
  height: 40px;
  border: 1px solid #ccc;
  font-size: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.wall {
  background: #999;
}
.target {
  background: #ffd;
}
.win {
  color: green;
  font-weight: bold;
  margin-top: 10px;
}
</style>

使用心得与体验总结

在这次使用 Trea 构建 Vue 推箱子小游戏的过程中,我有以下几点深刻体会:

优点

  1. 生成速度极快:仅需一句自然语言描述,Trea 在几秒内就生成完整结构。
  2. 逻辑清晰:Trea 生成的代码可读性高,变量命名合理,组件结构分明。
  3. 支持 Vue 3:生成的项目默认使用 Vue 3 + Composition API,非常现代。
  4. 可交互性强:自动绑定键盘事件、状态响应更新都能正确生成。

总结

Trea 是一个非常适合用来构建 Vue 项目的 AI 工具。特别是对于小游戏、前端 Demo 或教学场景,几乎可以“秒建项目”。本次“推箱子小游戏”的实现,让我切实体验到 AI 帮助程序员提效的可能。 如果你也想尝试这款工具,强烈建议访问:www.trae.com.cn/