当前位置:首页 > VUE

vue实现滑动拼图

2026-01-17 11:20:51VUE

Vue 实现滑动拼图的方法

滑动拼图是一种常见的交互式游戏,可以通过 Vue 的动态数据绑定和事件处理轻松实现。以下是实现滑动拼图的步骤和代码示例。

数据结构设计

使用一个二维数组来表示拼图的当前状态,其中每个元素代表拼图的一个格子。空白的格子可以用 null 或特定的标识符表示。

data() {
  return {
    puzzle: [
      [1, 2, 3],
      [4, 5, 6],
      [7, 8, null]
    ],
    rows: 3,
    cols: 3
  };
}

渲染拼图界面

使用 v-for 指令动态渲染拼图的格子,并为每个格子绑定点击事件。

<template>
  <div class="puzzle-container">
    <div 
      v-for="(row, rowIndex) in puzzle" 
      :key="rowIndex" 
      class="puzzle-row"
    >
      <div
        v-for="(cell, colIndex) in row"
        :key="colIndex"
        class="puzzle-cell"
        @click="handleClick(rowIndex, colIndex)"
      >
        {{ cell }}
      </div>
    </div>
  </div>
</template>

处理点击事件

当用户点击某个格子时,检查该格子是否可以移动(即是否与空白格子相邻)。如果可以移动,则交换两者的位置。

methods: {
  handleClick(row, col) {
    const blankPos = this.findBlankPosition();
    if (this.isAdjacent(row, col, blankPos.row, blankPos.col)) {
      this.swapCells(row, col, blankPos.row, blankPos.col);
    }
  },
  findBlankPosition() {
    for (let i = 0; i < this.rows; i++) {
      for (let j = 0; j < this.cols; j++) {
        if (this.puzzle[i][j] === null) {
          return { row: i, col: j };
        }
      }
    }
    return { row: -1, col: -1 };
  },
  isAdjacent(row1, col1, row2, col2) {
    return (
      (Math.abs(row1 - row2) === 1 && col1 === col2) ||
      (Math.abs(col1 - col2) === 1 && row1 === row2)
    );
  },
  swapCells(row1, col1, row2, col2) {
    const temp = this.puzzle[row1][col1];
    this.$set(this.puzzle[row1], col1, this.puzzle[row2][col2]);
    this.$set(this.puzzle[row2], col2, temp);
  }
}

样式设计

为拼图容器和格子添加样式,使其看起来更美观。

.puzzle-container {
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.puzzle-row {
  display: flex;
  gap: 5px;
}
.puzzle-cell {
  width: 60px;
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f0f0f0;
  border: 1px solid #ccc;
  cursor: pointer;
}
.puzzle-cell:hover {
  background-color: #e0e0e0;
}

初始化拼图

created 钩子中初始化拼图,可以随机打乱顺序以增加游戏难度。

created() {
  this.shufflePuzzle();
},
methods: {
  shufflePuzzle() {
    // 简单的打乱算法
    for (let i = 0; i < 100; i++) {
      const blankPos = this.findBlankPosition();
      const directions = [
        { row: blankPos.row - 1, col: blankPos.col },
        { row: blankPos.row + 1, col: blankPos.col },
        { row: blankPos.row, col: blankPos.col - 1 },
        { row: blankPos.row, col: blankPos.col + 1 }
      ];
      const validMoves = directions.filter(
        dir => dir.row >= 0 && dir.row < this.rows && dir.col >= 0 && dir.col < this.cols
      );
      const move = validMoves[Math.floor(Math.random() * validMoves.length)];
      if (move) {
        this.swapCells(blankPos.row, blankPos.col, move.row, move.col);
      }
    }
  }
}

检查胜利条件

每次移动后检查拼图是否已经完成(即所有格子按顺序排列)。

vue实现滑动拼图

methods: {
  checkWin() {
    let expected = 1;
    for (let i = 0; i < this.rows; i++) {
      for (let j = 0; j < this.cols; j++) {
        if (i === this.rows - 1 && j === this.cols - 1) {
          if (this.puzzle[i][j] !== null) return false;
        } else {
          if (this.puzzle[i][j] !== expected) return false;
          expected++;
        }
      }
    }
    return true;
  },
  handleClick(row, col) {
    const blankPos = this.findBlankPosition();
    if (this.isAdjacent(row, col, blankPos.row, blankPos.col)) {
      this.swapCells(row, col, blankPos.row, blankPos.col);
      if (this.checkWin()) {
        alert('恭喜你完成了拼图!');
      }
    }
  }
}

通过以上步骤,可以实现一个基本的滑动拼图游戏。可以根据需要进一步扩展功能,例如增加难度级别、计时器或步数统计。

标签: 拼图vue
分享给朋友:

相关文章

vue 实现豆瓣

vue 实现豆瓣

以下是基于 Vue 实现豆瓣电影类功能的实现方案,涵盖核心模块和技术要点: 数据获取与 API 调用 使用豆瓣开放 API(需注意调用频率限制)或第三方代理接口 推荐 axios 进行异步请求,配合…

vue首页实现

vue首页实现

实现Vue首页的基本步骤 创建一个Vue首页通常涉及项目初始化、页面结构设计、路由配置和组件开发。以下是具体实现方法: 初始化Vue项目 使用Vue CLI或Vite快速搭建项目结构: npm i…

vue实现抽屉

vue实现抽屉

Vue 实现抽屉组件 抽屉组件是一种常见的 UI 模式,通常用于从屏幕边缘滑出内容。以下是几种实现抽屉的方法: 使用 Vue 原生实现 创建一个基本的抽屉组件,利用 Vue 的过渡和条件渲染功能。…

vue实现数据删除

vue实现数据删除

vue实现数据删除的方法 使用v-for和v-on指令 在Vue中可以通过v-for指令渲染列表数据,结合v-on指令监听删除事件。定义一个方法处理删除逻辑,通常使用splice方法从数组中移除指定项…

vue 实现 input focus

vue 实现 input focus

实现 Input Focus 的方法 在 Vue 中实现 input 元素的聚焦可以通过以下几种方式完成。 使用 ref 和 $refs 通过 ref 属性标记 input 元素,然后在 Vue 实…

vue界面实现滚动

vue界面实现滚动

实现滚动的基本方法 在Vue中实现滚动效果可以通过多种方式完成,包括使用原生CSS、JavaScript或第三方库。以下是一些常见的实现方法: 使用CSS实现滚动 通过CSS的overflow属性可…