当前位置:首页 > VUE

vue实现棋子移动

2026-01-19 02:32:29VUE

实现棋子移动的基本思路

在Vue中实现棋子移动通常需要结合数据驱动和DOM操作。通过维护棋子的位置状态,利用Vue的响应式特性更新视图。以下是关键实现步骤:

数据驱动棋子位置

定义棋子的初始位置数据,通常使用xy坐标或网格索引:

vue实现棋子移动

data() {
  return {
    pieces: [
      { id: 1, x: 0, y: 0, type: 'pawn' },
      { id: 2, x: 1, y: 1, type: 'rook' }
    ]
  }
}

渲染棋子到视图

使用v-for渲染棋子,并通过内联样式或CSS类绑定位置:

<div class="chess-board">
  <div 
    v-for="piece in pieces" 
    :key="piece.id"
    class="chess-piece"
    :style="{ left: `${piece.x * 50}px`, top: `${piece.y * 50}px` }"
    @click="selectPiece(piece)"
  >
    {{ piece.type }}
  </div>
</div>

处理移动逻辑

实现移动方法,更新选中棋子的坐标:

vue实现棋子移动

methods: {
  selectPiece(piece) {
    this.selectedPiece = piece;
  },

  movePiece(toX, toY) {
    if (this.selectedPiece) {
      this.selectedPiece.x = toX;
      this.selectedPiece.y = toY;
    }
  }
}

添加拖拽功能(可选)

结合HTML5拖放API或第三方库实现拖拽:

<div
  draggable="true"
  @dragstart="handleDragStart(piece)"
  @dragend="handleDragEnd"
></div>
handleDragStart(piece) {
  this.selectedPiece = piece;
  event.dataTransfer.setData('text/plain', piece.id);
},
handleDragEnd(event) {
  const dropX = Math.floor(event.clientX / 50);
  const dropY = Math.floor(event.clientY / 50);
  this.movePiece(dropX, dropY);
}

动画过渡效果

使用Vue的<transition>或CSS过渡实现平滑移动:

.chess-piece {
  transition: all 0.3s ease;
}

完整组件示例

<template>
  <div class="chess-board" @click.self="deselect">
    <div
      v-for="piece in pieces"
      :key="piece.id"
      class="chess-piece"
      :style="pieceStyle(piece)"
      @click="selectPiece(piece)"
      draggable="true"
      @dragstart="handleDragStart($event, piece)"
      @dragend="handleDragEnd($event)"
    >
      {{ piece.type }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pieces: [
        { id: 1, x: 0, y: 0, type: '♙' },
        { id: 2, x: 3, y: 2, type: '♖' }
      ],
      selectedPiece: null
    }
  },
  methods: {
    pieceStyle(piece) {
      return {
        left: `${piece.x * 50}px`,
        top: `${piece.y * 50}px`,
        backgroundColor: this.selectedPiece === piece ? '#ffeb3b' : ''
      }
    },
    selectPiece(piece) {
      this.selectedPiece = piece;
    },
    deselect() {
      this.selectedPiece = null;
    },
    handleDragStart(event, piece) {
      this.selectPiece(piece);
      event.dataTransfer.setData('text/plain', piece.id);
    },
    handleDragEnd(event) {
      const rect = event.currentTarget.parentElement.getBoundingClientRect();
      const x = event.clientX - rect.left;
      const y = event.clientY - rect.top;
      const gridX = Math.floor(x / 50);
      const gridY = Math.floor(y / 50);

      if (this.selectedPiece) {
        this.selectedPiece.x = gridX;
        this.selectedPiece.y = gridY;
      }
    }
  }
}
</script>

<style>
.chess-board {
  position: relative;
  width: 400px;
  height: 400px;
  background-color: #f0d9b5;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
}

.chess-piece {
  position: absolute;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
  cursor: move;
  user-select: none;
  transition: all 0.3s ease;
  z-index: 10;
}
</style>

标签: 棋子vue
分享给朋友:

相关文章

vue实现共享屏幕

vue实现共享屏幕

共享屏幕的基本实现 在Vue中实现共享屏幕功能通常需要结合浏览器的getDisplayMedia API。以下是一个基础实现示例: <template> <div>…

vue实现多级组件

vue实现多级组件

实现多级组件的基本结构 在Vue中实现多级组件通常涉及父子组件嵌套,通过props向下传递数据,通过$emit向上传递事件。多级组件的核心是递归或动态组件渲染。 <!-- 父组件 Parent…

vue实现滚动中断

vue实现滚动中断

Vue 实现滚动中断的方法 在 Vue 中实现滚动中断通常涉及监听滚动事件,并在特定条件下阻止默认行为或停止滚动。以下是几种常见方法: 监听滚动事件并阻止默认行为 通过 @scroll 或 @wh…

vue 实现长列表

vue 实现长列表

Vue 实现长列表的优化方案 虚拟滚动技术 虚拟滚动是处理长列表的高效方式,仅渲染可视区域内的元素。通过计算滚动位置动态加载和卸载DOM节点,大幅减少内存占用和渲染压力。 <template…

vue实现添加用户

vue实现添加用户

Vue 实现添加用户功能 数据绑定与表单设计 在 Vue 中实现添加用户功能,首先需要设计一个表单,用于收集用户输入的数据。通过 v-model 实现双向数据绑定,确保表单数据与 Vue 实例中的数据…

课程表vue实现

课程表vue实现

实现课程表的Vue组件 创建课程表需要设计数据结构、布局和交互逻辑。以下是一个基于Vue 3的实现方案: 数据结构设计 const timetableData = ref([ { ti…