Vue.js写一个扫雷游戏

时间: 2023-12-18 21:04:49 浏览: 33
好的,下面是一个简单的Vue.js扫雷游戏的代码示例: HTML模板: ``` <template> <div class="minesweeper"> <h1>扫雷游戏</h1> <div class="grid"> <div v-for="(row, rowIndex) in board" :key="rowIndex" class="row" > <div v-for="(cell, cellIndex) in row" :key="cellIndex" :class="['cell', `cell-${cell.status}`]" @click="handleClick(rowIndex, cellIndex)" @contextmenu.prevent="handleRightClick(rowIndex, cellIndex)" > <span v-if="cell.status === 'hidden'">{{ cell.flag ? '🚩' : '' }}</span> <span v-if="cell.status === 'revealed'">{{ cell.value === 0 ? '' : cell.value }}</span> <span v-if="cell.status === 'exploded'">💣</span> </div> </div> </div> <div class="controls"> <button @click="handleReset">重新开始</button> <span class="message">{{ message }}</span> </div> </div> </template> ``` CSS样式: ``` .minesweeper { display: flex; flex-direction: column; align-items: center; font-size: 1.5rem; padding: 1rem; } .grid { display: flex; flex-direction: column; justify-content: center; align-items: center; border: 1px solid black; } .row { display: flex; justify-content: center; } .cell { display: flex; justify-content: center; align-items: center; width: 2.5rem; height: 2.5rem; border: 1px solid black; cursor: pointer; font-size: 1.5rem; } .cell-hidden { background-color: grey; } .cell-revealed { background-color: white; } .cell-exploded { background-color: red; color: white; } .controls { display: flex; justify-content: center; align-items: center; margin-top: 1rem; } .message { margin-left: 1rem; } ``` JavaScript代码: ``` <script> export default { data() { return { board: [], message: '', gameOver: false, numMines: 10, numRevealed: 0, numFlags: 0, }; }, created() { this.reset(); }, methods: { reset() { this.board = this.generateBoard(); this.message = ''; this.gameOver = false; this.numRevealed = 0; this.numFlags = 0; }, generateBoard() { const board = []; for (let i = 0; i < 8; i++) { board[i] = []; for (let j = 0; j < 8; j++) { board[i][j] = { value: 0, status: 'hidden', flag: false }; } } this.placeMines(board); this.calculateNumbers(board); return board; }, placeMines(board) { let numMines = this.numMines; while (numMines > 0) { const i = Math.floor(Math.random() * 8); const j = Math.floor(Math.random() * 8); if (board[i][j].value !== -1) { board[i][j].value = -1; numMines--; } } }, calculateNumbers(board) { for (let i = 0; i < 8; i++) { for (let j = 0; j < 8; j++) { if (board[i][j].value === -1) { continue; } let numMines = 0; if (i > 0 && j > 0 && board[i - 1][j - 1].value === -1) numMines++; if (i > 0 && board[i - 1][j].value === -1) numMines++; if (i > 0 && j < 7 && board[i - 1][j + 1].value === -1) numMines++; if (j > 0 && board[i][j - 1].value === -1) numMines++; if (j < 7 && board[i][j + 1].value === -1) numMines++; if (i < 7 && j > 0 && board[i + 1][j - 1].value === -1) numMines++; if (i < 7 && board[i + 1][j].value === -1) numMines++; if (i < 7 && j < 7 && board[i + 1][j + 1].value === -1) numMines++; board[i][j].value = numMines; } } }, handleClick(i, j) { const cell = this.board[i][j]; if (this.gameOver || cell.status !== 'hidden') { return; } if (cell.value === -1) { cell.status = 'exploded'; this.gameOver = true; this.message = '你输了!'; } else { this.revealCell(i, j); if (this.numRevealed === 64 - this.numMines) { this.gameOver = true; this.message = '你赢了!'; } } }, revealCell(i, j) { const cell = this.board[i][j]; if (cell.status !== 'hidden' || cell.flag) { return; } cell.status = 'revealed'; this.numRevealed++; if (cell.value === 0) { this.revealNeighbors(i, j); } }, revealNeighbors(i, j) { if (i > 0 && j > 0) this.revealCell(i - 1, j - 1); if (i > 0) this.revealCell(i - 1, j); if (i > 0 && j < 7) this.revealCell(i - 1, j + 1); if (j > 0) this.revealCell(i, j - 1); if (j < 7) this.revealCell(i, j + 1); if (i < 7 && j > 0) this.revealCell(i + 1, j - 1); if (i < 7) this.revealCell(i + 1, j); if (i < 7 && j < 7) this.revealCell(i + 1, j + 1); }, handleRightClick(i, j) { const cell = this.board[i][j]; if (this.gameOver || cell.status === 'revealed') { return; } cell.flag = !cell.flag; this.numFlags += cell.flag ? 1 : -1; }, }, }; </script> ``` 这个示例中,我们使用了一个8x8的网格来表示扫雷游戏的棋盘。每个单元格可以有三种状态:未揭示、已揭示和爆炸。我们还可以在单元格上放置旗帜来标记它们可能有地雷。 要玩这个游戏,玩家需要点击单元格来揭示它们,或者右键单击来标记它们。如果一个单元格有地雷,它将爆炸并结束游戏。如果玩家成功揭示所有没有地雷的单元格,那么他们就赢了。

相关推荐

最新推荐

recommend-type

利用Vue.js+Node.js+MongoDB实现一个博客系统(附源码)

本文主要介绍了利用Vue.js+Node.js+MongoDB实现一个博客系统,这个博客使用Vue做前端框架,Node+express做后端,数据库使用的是MongoDB。实现了用户注册、用户登录、博客管理、文章编辑、标签分类等功能,需要的朋友...
recommend-type

详解django模板与vue.js冲突问题

在本文里小编给各位整理了关于django模板与vue.js冲突问题以及实例代码,需要的朋友们参考下。
recommend-type

vue.js编译时给生成的文件增加版本号

主要介绍了vue.js编译时给生成的文件增加版本号,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

Vue.js仿Select下拉框效果

主要为大家详细介绍了Vue.js仿 Select下拉框效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Vue-cli3项目配置Vue.config.js实战记录

主要给大家介绍了关于Vue-cli3项目配置Vue.config.js的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

hive中 的Metastore

Hive中的Metastore是一个关键的组件,它用于存储和管理Hive中的元数据。这些元数据包括表名、列名、表的数据类型、分区信息、表的存储位置等信息。Hive的查询和分析都需要Metastore来管理和访问这些元数据。 Metastore可以使用不同的后端存储来存储元数据,例如MySQL、PostgreSQL、Oracle等关系型数据库,或者Hadoop分布式文件系统中的HDFS。Metastore还提供了API,使得开发人员可以通过编程方式访问元数据。 Metastore的另一个重要功能是跟踪表的版本和历史。当用户对表进行更改时,Metastore会记录这些更改,并且可以让用户回滚到
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。