当前位置:首页 > VUE

vue实现棋子移动

2026-02-19 18:55:38VUE

实现棋子移动的基本思路

在Vue中实现棋子移动通常涉及以下几个核心概念:数据驱动、事件监听和动态样式绑定。通过维护棋子的位置状态,结合用户交互(如点击或拖拽),更新数据并触发视图重新渲染。

定义棋子数据模型

使用Vue的dataref(Composition API)定义棋子的初始位置和状态。例如:

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

// Composition API
const pieces = ref([
  { id: 1, x: 0, y: 0, type: 'pawn' },
  { id: 2, x: 1, y: 1, type: 'rook' }
]);
const selectedPiece = ref(null);

渲染棋子到棋盘

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

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

处理棋子选中逻辑

在方法中实现选中逻辑,高亮当前棋子并存储选中状态:

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

// Composition API
function selectPiece(id) {
  selectedPiece.value = id;
}

实现移动逻辑

监听棋盘点击事件,将选中的棋子移动到目标位置。需检查目标位置是否合法(如是否被其他棋子占据):

methods: {
  movePiece(targetX, targetY) {
    if (this.selectedPiece === null) return;

    const piece = this.pieces.find(p => p.id === this.selectedPiece);
    if (isValidMove(piece, targetX, targetY)) {
      piece.x = targetX;
      piece.y = targetY;
    }
    this.selectedPiece = null;
  }
}

拖拽移动的高级实现

若需支持拖拽,可使用原生拖拽API或第三方库(如vuedraggable):

<div
  v-for="piece in pieces"
  :key="piece.id"
  draggable="true"
  @dragstart="dragStart(piece.id)"
  @dragend="dragEnd"
  @dragover.prevent
  @drop="drop($event, piece)"
></div>
methods: {
  dragStart(id) {
    this.draggedPiece = id;
  },
  drop(event, targetPiece) {
    if (this.draggedPiece) {
      const piece = this.pieces.find(p => p.id === this.draggedPiece);
      piece.x = targetPiece.x;
      piece.y = targetPiece.y;
    }
  }
}

动画过渡效果

使用Vue的<transition>或CSS动画增强移动体验:

<transition name="slide">
  <div v-if="show" class="chess-piece"></div>
</transition>
.slide-enter-active {
  transition: all 0.5s ease;
}
.slide-leave-active {
  transition: all 0.8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}

完整示例代码

组合上述逻辑的简单实现:

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

<script>
export default {
  data() {
    return {
      pieces: [
        { id: 1, x: 2, y: 3 },
        { id: 2, x: 5, y: 1 }
      ],
      selectedPiece: null
    };
  },
  methods: {
    selectPiece(id) {
      this.selectedPiece = id;
    },
    deselect() {
      this.selectedPiece = null;
    }
  }
};
</script>

<style>
.board {
  position: relative;
  width: 400px;
  height: 400px;
  background-color: #f0d9b5;
}
.piece {
  position: absolute;
  width: 40px;
  height: 40px;
  background-color: #b58863;
  border-radius: 50%;
  cursor: move;
}
.piece.selected {
  box-shadow: 0 0 10px yellow;
}
</style>

vue实现棋子移动

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

相关文章

vue实现展开与收起

vue实现展开与收起

Vue 实现展开与收起功能 在 Vue 中实现展开与收起功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-show 或 v-if 控制显示 通过 Vue 的指令 v-show 或 v-i…

vue实现遮罩

vue实现遮罩

Vue 实现遮罩层的方法 在 Vue 中实现遮罩层可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-show 或 v-if 控制显示 通过 Vue 的指令动态控制遮罩层的显示与隐藏。…

vue实现拍照

vue实现拍照

Vue实现拍照功能 在Vue中实现拍照功能通常需要结合浏览器的MediaDevices API和Canvas元素。以下是具体实现方法: 安装依赖(可选) 如果需要更简单的封装,可以安装vue-w…

vue实现粘贴

vue实现粘贴

Vue 实现粘贴功能的方法 在 Vue 中实现粘贴功能通常涉及监听粘贴事件并处理剪贴板数据。以下是几种常见的实现方式: 监听原生粘贴事件 通过 @paste 指令或原生 addEventListe…

vue mvvm实现

vue mvvm实现

Vue MVVM 实现原理 Vue.js 是一个基于 MVVM(Model-View-ViewModel)模式的框架,其核心是通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 M…

vue实现画廊

vue实现画廊

Vue实现画廊的基本方法 使用Vue.js实现画廊功能可以通过多种方式完成,以下是一种常见的实现方案: 安装必要依赖 npm install vue-gallery --save 引入组件并注册…