当前位置:首页 > 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);
      }
    }
  }
}

检查胜利条件

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

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实现菜单调用

Vue 实现动态菜单的常见方法 基于路由配置生成菜单 Vue Router 的路由配置可以天然转化为菜单结构,适合权限与路由绑定的系统: // router/index.js const rout…

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { -…

vue实现本地数据存储

vue实现本地数据存储

使用 localStorage 或 sessionStorage Vue 中可以直接调用浏览器提供的 localStorage 或 sessionStorage API 进行数据存储。localSto…

vue实现全局遮罩层

vue实现全局遮罩层

实现全局遮罩层的方法 在Vue中实现全局遮罩层可以通过多种方式完成,以下是几种常见的实现方法: 使用Vue组件创建遮罩层 创建一个遮罩层组件,通过全局注册或动态挂载的方式实现全局调用。以下是一个简单…

vue 实现在线预览

vue 实现在线预览

Vue 实现在线预览的常见方法 使用 iframe 嵌入 通过 iframe 标签可以嵌入多种类型的文件,如 PDF、图片、网页等。需要确保文件地址可访问。 <template> &…

vue手写签名如何实现

vue手写签名如何实现

实现手写签名的基本思路 在Vue中实现手写签名功能,通常需要借助HTML5的Canvas API来捕获用户的绘制操作。核心流程包括初始化画布、监听触摸/鼠标事件、记录轨迹以及生成签名图片。 安装依…