基于vue編寫一個(gè)月餅連連看游戲
"月圓花好夜,花好月圓人更圓。"
前言
中秋節(jié)快要到啦,我們一起用Vue創(chuàng)建一個(gè)簡單的連連看游戲。
連連看大家一定都玩過吧,通過消除相同的圖案來清理棋盤。上面就是我們今天要做的效果圖,看上去也挺像那么回事,不過實(shí)現(xiàn)起來也是非常簡單。
我將一步步引導(dǎo)大家完成整個(gè)游戲的制作過程,讓我們開始吧,一起為中秋節(jié)增添一些互動(dòng)和娛樂!
設(shè)計(jì)思路
在構(gòu)建連連看游戲之前,首先需要考慮游戲的設(shè)計(jì)思路。
- 游戲界面:我們將創(chuàng)建一個(gè)網(wǎng)格狀的游戲界面,每個(gè)格子上都有一個(gè)圖標(biāo)或者數(shù)字。
- 游戲邏輯:玩家可以點(diǎn)擊兩個(gè)相同的格子來連接它們,但是連接的路徑上下左右不能有障礙物
- 游戲結(jié)束:當(dāng)所有的格子都被連接后,游戲結(jié)束。
初始棋盤
<template> <div class="game-board"> <!-- 棋盤網(wǎng)格 --> <div v-for="(row,rowIndex) in grid" :key="rowIndex" class="row"> <div v-for="(cell,colIndex) in row" class="cell" :key="colIndex" > {{grid[rowIndex][colIndex]}} </div> </div> </div> </template> <script setup lang="ts"> import { onMounted, ref } from 'vue'; const grid = ref<number[][]>([]) /** * 隨機(jī)生成grid */ const randomizeGrid = () => { const rows = 10 const cols = 10 for(let i = 0 ; i < rows ; i ++) { const row = [] for(let j = 0 ; j < cols ; j ++) { row.push(Math.floor(Math.random() * 3 + 1)) } grid.value.push(row) } } onMounted(() => { randomizeGrid() }) </script> <style scoped lang="less"> .game-board { background-color: palegoldenrod; background-size: contain; .row { display: flex; .cell { width: 60px; height: 60px; margin: 5px; display: flex; align-items: center; justify-content: center; cursor: pointer; transition: all .3s; } } } </style>
這樣就生成了一個(gè)隨機(jī)棋盤,后續(xù)我們將數(shù)組換成對(duì)應(yīng)的圖片就可以達(dá)到效果。
但是上面的寫法是我前面寫的,后續(xù)發(fā)現(xiàn)一個(gè)問題,我們不能簡單的隨機(jī)1、2、3
,我們要確保每種值都是成對(duì)存在的。為了實(shí)現(xiàn)這一點(diǎn),可以采用以下步驟:
- 初始化一個(gè)存儲(chǔ)可用格子坐標(biāo)的數(shù)組
availableCells
。 - 遍歷棋盤格子,并將每個(gè)格子的坐標(biāo)添加到
availableCells
。 - 隨機(jī)選擇一個(gè)可用格子,為其分配一個(gè)隨機(jī)圖案值,然后從
availableCells
中移除該格子。 - 再次隨機(jī)選擇一個(gè)可用格子,并為其分配相同的圖案值。
- 重復(fù)步驟 3 和 4,直到生成了足夠的圖案對(duì)。
- 將這些圖案值填充到游戲棋盤中。
/** * 隨機(jī)生成grid,確保每一個(gè)值都是成對(duì)存在的 */ const randomizeGrid = () => { const rows = 10; const cols = 10; const numPairs = (rows * cols) / 2; // 總共需要的值對(duì)數(shù) const igrid:number[][] = Array(rows).fill(0).map(() => Array(cols).fill(0)) const availableCells = []; // 存儲(chǔ)可用格子的坐標(biāo) // 初始化可用格子的坐標(biāo)數(shù)組 for (let i = 0; i < rows; i++) { for (let j = 0; j < cols; j++) { availableCells.push([i, j]); } } // 隨機(jī)生成成對(duì)的圖案,并填充到grid中 for (let pair = 1; pair <= numPairs; pair++) { // 隨機(jī)選擇一個(gè)可用格子 const cellIndex = Math.floor(Math.random() * availableCells.length); const [row, col] = availableCells[cellIndex]; // 隨機(jī)生成一個(gè)圖案 const value = Math.floor(Math.random() * 4+ 1); igrid[row][col] = value; availableCells.splice(cellIndex, 1); // 隨機(jī)選擇一個(gè)可用格子,將相同圖案填充到第二個(gè)位置 const secondCellIndex = Math.floor(Math.random() * availableCells.length); const [secondRow, secondCol] = availableCells[secondCellIndex]; igrid[secondRow][secondCol] = value; // 從可用格子數(shù)組中移除已經(jīng)填充的格子 availableCells.splice(secondCellIndex, 1); } grid.value = igrid };
上面注釋已經(jīng)很清楚啦,如果大家有更好的方法歡迎在評(píng)論區(qū)交流 !
點(diǎn)擊動(dòng)作
上一個(gè)步驟中,我們已經(jīng)獲取到了每個(gè)格子的值。現(xiàn)在我們寫一下選中邏輯,由于每次點(diǎn)擊都需要和上一次結(jié)果進(jìn)行對(duì)比,就需要將上一個(gè)點(diǎn)擊位置存起來
現(xiàn)在我們要實(shí)現(xiàn)一下功能
const lastSelectedCell = ref<number[]>([]) const selectCell = (rowIndex: number, colIndex: number) => { // 上次選中了 if(lastSelectedCell.value?.length) { if(canConnect(lastSelectedCell.value,[rowIndex,colIndex])) { console.log('可以連接~~'); grid.value[lastSelectedCell.value[0]][lastSelectedCell.value[1]] = 0 grid.value[rowIndex][colIndex] = 0 } lastSelectedCell.value = [] } else { lastSelectedCell.value = [rowIndex,colIndex] } } /** * 格子是否被選中 */ const isCellSelected = (rowIndex: number, colIndex: number): boolean => { return lastSelectedCell.value[0] === rowIndex && lastSelectedCell.value[1] === colIndex; };
isCellSelected
是我們選中要配對(duì)的那個(gè)方格,為了是對(duì)它添加動(dòng)畫方便。
點(diǎn)擊時(shí)有兩種情況:
- 正在選第一個(gè),我們需要記錄次方格,為了下一次連接配對(duì)
- 選了第一個(gè)了,此時(shí)點(diǎn)擊選中第二個(gè),判斷是否配對(duì)成功 (配對(duì)成功的邏輯下一步介紹)
連接判定
連接判斷使用bfs來做
這里需要注意,判斷邏輯需要寫在for循環(huán)里面,因?yàn)樾枰鋵?duì)的那個(gè)方格值一定不為0,也就是說目標(biāo)方格只要有值就到達(dá)不了
/** * 使用 BFS 檢查路徑是否通 */ const directions = [ [-1, 0], // 上 [1, 0], // 下 [0, -1], // 左 [0, 1], // 右 ]; const isPathConnected = (startCell: number[], endCell: number[]): boolean => { const visited = new Set<string>(); const queue: number[][] = [startCell]; while (queue.length) { const [row, col] = queue.shift()!; // 檢查四個(gè)方向的相鄰格子 for (const [dx, dy] of directions) { const newRow = row + dx; const newCol = col + dy; const key = `${newRow}-${newCol}`; if(endCell[0] === newRow && endCell[1] === newCol) return true if ( newRow >= 0 && newRow < grid.value.length && newCol >= 0 && newCol < grid.value[0].length && !visited.has(key) && grid.value[newRow][newCol] === 0 ) { queue.push([newRow, newCol]); visited.add(key); } } } return false; // 沒找到路徑 };
此時(shí)我們就可以點(diǎn)擊配對(duì)了,最簡單的版本也就完成了,貼一下完整代碼:
<template> <div class="game-board"> <!-- 棋盤網(wǎng)格 --> <div v-for="(row,rowIndex) in grid" :key="rowIndex" class="row"> <div v-for="(cell,colIndex) in row" class="cell" :class="{isSelected : isCellSelected(rowIndex,colIndex)}" :key="colIndex" @click="selectCell(rowIndex,colIndex)" > {{grid[rowIndex][colIndex]}} </div> </div> </div> </template> <script setup lang="ts"> import { onMounted, ref } from 'vue'; const grid = ref<number[][]>([]) const lastSelectedCell = ref<number[]>([]) const selectCell = (rowIndex: number, colIndex: number) => { // 上次選中了 if(lastSelectedCell.value?.length) { if(canConnect(lastSelectedCell.value,[rowIndex,colIndex])) { console.log('可以連接~~'); grid.value[lastSelectedCell.value[0]][lastSelectedCell.value[1]] = 0 grid.value[rowIndex][colIndex] = 0 } lastSelectedCell.value = [] } else { lastSelectedCell.value = [rowIndex,colIndex] } } /** * 格子是否被選中 */ const isCellSelected = (rowIndex: number, colIndex: number): boolean => { return lastSelectedCell.value[0] === rowIndex && lastSelectedCell.value[1] === colIndex; }; /** * 檢查兩個(gè)格子是否可以連接 */ const canConnect = (cell1: number[],cell2: number[]): boolean => { // 點(diǎn)擊同一個(gè)格子 if(cell1[0] === cell2[0] && cell1[1] === cell2[1]) { return false } // 是否值相同 if(grid.value[cell1[0]][cell1[1]] !== grid.value[cell2[0]][cell2[1]]) { return false } // 路徑是否通 return isPathConnected(cell1,cell2) } /** * 使用 BFS 檢查路徑是否通 */ const directions = [ [-1, 0], // 上 [1, 0], // 下 [0, -1], // 左 [0, 1], // 右 ]; const isPathConnected = (startCell: number[], endCell: number[]): boolean => { const visited = new Set<string>(); const queue: number[][] = [startCell]; while (queue.length) { const [row, col] = queue.shift()!; // 檢查四個(gè)方向的相鄰格子 for (const [dx, dy] of directions) { const newRow = row + dx; const newCol = col + dy; const key = `${newRow}-${newCol}`; if(endCell[0] === newRow && endCell[1] === newCol) return true if ( newRow >= 0 && newRow < grid.value.length && newCol >= 0 && newCol < grid.value[0].length && !visited.has(key) && grid.value[newRow][newCol] === 0 ) { queue.push([newRow, newCol]); visited.add(key); } } } return false; // 沒找到路徑 }; /** * 隨機(jī)生成grid */ const randomizeGrid = () => { const rows = 10 const cols = 10 for(let i = 0 ; i < rows ; i ++) { const row = [] for(let j = 0 ; j < cols ; j ++) { row.push(Math.floor(Math.random() * 3 + 1)) } grid.value.push(row) } } onMounted(() => { randomizeGrid() }) </script> <style scoped lang="less"> .game-board { // width: 80vw; // height: 80vh; background-color: palegoldenrod; background-size: contain; .row { display: flex; .cell { width: 60px; height: 60px; margin: 5px; display: flex; align-items: center; justify-content: center; cursor: pointer; transition: all .3s; background-color: aquamarine; } } } </style>
現(xiàn)在效果:
引入月餅圖片
現(xiàn)在簡單功能算是完成了,但是太簡單了。我們得做一個(gè)看起來高大上的練練看。
此時(shí),直接請(qǐng)班里妹子畫幾個(gè)好看的月餅圖片:
真的太好看了,項(xiàng)目直接升個(gè)檔次。
拿ps切出來:
然后就可以根據(jù)當(dāng)前格子渲染出對(duì)應(yīng)的小月餅啦!
動(dòng)態(tài)加載圖片需要用到vite
自己的方法
加了點(diǎn)擊的動(dòng)畫,選中的圖片放大并且添加了光圈。大家自己看一看具體實(shí)現(xiàn)吧,也是很簡單。
小結(jié)
最后貼一下項(xiàng)目地址
項(xiàng)目地址:https://github.com/Bbbtt04/mooncake-match
主要有兩個(gè)難點(diǎn):
- bfs判斷連接
- 隨機(jī)生成成對(duì)的值
- 最難的一點(diǎn):找一個(gè)會(huì)手繪的妹子
大家也可以試一試自己實(shí)現(xiàn)一下,做出來成就感滿滿!
以上就是基于vue編寫一個(gè)月餅連連看游戲的詳細(xì)內(nèi)容,更多關(guān)于vue月餅連連看的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決axios:"timeout of 5000ms exceeded"
這篇文章主要介紹了解決axios:"timeout of 5000ms exceeded"超時(shí)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08IOS上微信小程序密碼框光標(biāo)離開提示存儲(chǔ)密碼的完美解決方案
ios密碼框輸入密碼光標(biāo)離開之后會(huì)提示存儲(chǔ)密碼的彈窗,關(guān)于這樣的問題怎么解決呢,下面給大家分享IOS上微信小程序密碼框光標(biāo)離開提示存儲(chǔ)密碼的完美解決方案,感興趣的朋友一起看看吧2024-07-07查看當(dāng)前vue項(xiàng)目所需Node.js版本的方法
這篇文章主要大家介紹了查看當(dāng)前vue項(xiàng)目所需Node.js版本的方法,文章通過代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-11-11如何通過Vue自定義指令實(shí)現(xiàn)前端埋點(diǎn)詳析
埋點(diǎn)分析是網(wǎng)站分析的一種常用的數(shù)據(jù)采集方法,下面這篇文章主要給大家介紹了關(guān)于如何通過Vue自定義指令實(shí)現(xiàn)前端埋點(diǎn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07