Html5 Canvas 扫雷 (IE9测试通过)

扫雷是一个非常经典的游戏,记得在第一次接触的windows 3.22 上就有扫雷了,到现在的Win7,依然保留着这个经典的游戏,结合Html5 Canvas,模仿Win7的UI,将老板扫雷进行了升级。在 FireFox, Chrome, 和 IE9 下测试通过。
实现中设计的类: CellInfo, MineInfo, InfoProcess, CellRender, MineArea, GameInfo, GameStatus, Game, StorageInfo, GameStorage。
游戏界面:

GameMode:
Beginner 初级设置 Width:7 Height:7 Mine:10。
Intermediate 中级设置 Width:15 Height:15 Mine:40。
Expert 高级设置 Width:30 Height:15 Mine:99。
Random   随机模式。
CustomerSetting:    用户自定义扫雷的 Width Height Mine。
Performance Test:   测试游戏在浏览器上的反应。
Game Record:         游戏记录。
Pause: 暂停游戏。
Save: 存盘游戏。


当点击Save存盘后,在GameStorage下面会记录当前游戏的缩略图。鼠标进入存盘缩略图,当前缩略图高亮一个小红边,点击左键恢复存盘点,点击右键删除存盘点.
由于游戏中使用的图片比较多, 这个链接是AllInOne的地址: http://zhangsichu.com/html5/minesweeper/canvasminesweeperrelease.htm
所有内容都在这一个Html里,不包含外部链接。你可以直接保存这个网页,这样就可以随时玩了。


也可以下载未压缩版本:

附件:MineSweeper.rar

转载于:https://www.cnblogs.com/powertoolsteam/archive/2011/03/16/1985865.html

好的,以下是一个简单的HTML5 Canvas 扫雷游戏的示例代码,希望能帮到您: ```html <!DOCTYPE html> <html> <head> <title>扫雷游戏</title> <style> canvas { border: 1px solid black; } </style> </head> <body> <canvas id="canvas" width="300" height="300"></canvas> <script> // 初始化游戏 var grid = []; var gridSize = 10; var bombCount = 10; var cellSize = 30; for (var i = 0; i < gridSize; i++) { var row = []; for (var j = 0; j < gridSize; j++) { row.push({ x: j * cellSize, y: i * cellSize, width: cellSize, height: cellSize, isBomb: false, isRevealed: false, isFlagged: false, neighborCount: 0 }); } grid.push(row); } // 随机布置炸弹 for (var i = 0; i < bombCount; i++) { var row = Math.floor(Math.random() * gridSize); var col = Math.floor(Math.random() * gridSize); var cell = grid[row][col]; if (cell.isBomb) { i--; } else { cell.isBomb = true; } } // 计算每个格子周围的炸弹数量 for (var i = 0; i < gridSize; i++) { for (var j = 0; j < gridSize; j++) { var cell = grid[i][j]; if (!cell.isBomb) { var count = 0; for (var row = i - 1; row <= i + 1; row++) { for (var col = j - 1; col <= j + 1; col++) { if (row >= 0 && row < gridSize && col >= 0 && col < gridSize) { if (grid[row][col].isBomb) { count++; } } } } cell.neighborCount = count; } } } // 绘制游戏 var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); for (var i = 0; i < gridSize; i++) { for (var j = 0; j < gridSize; j++) { var cell = grid[i][j]; context.fillStyle = "gray"; context.fillRect(cell.x, cell.y, cell.width, cell.height); if (cell.isRevealed) { if (cell.isBomb) { context.fillStyle = "red"; context.fillRect(cell.x, cell.y, cell.width, cell.height); } else { context.fillStyle = "white"; context.fillRect(cell.x, cell.y, cell.width, cell.height); if (cell.neighborCount > 0) { context.font = "20px Arial"; context.fillStyle = "black"; context.fillText(cell.neighborCount, cell.x + cell.width / 2, cell.y + cell.height / 2); } } } else { if (cell.isFlagged) { context.font = "20px Arial"; context.fillStyle = "black"; context.fillText("F", cell.x + cell.width / 2, cell.y + cell.height / 2); } } } } // 点击格子 canvas.addEventListener("click", function(event) { var x = event.pageX - canvas.offsetLeft; var y = event.pageY - canvas.offsetTop; var cell = getCell(x, y); if (cell !== null && !cell.isRevealed && !cell.isFlagged) { if (cell.isBomb) { revealBombs(); alert("你输了!"); } else { revealCell(cell); if (checkWin()) { alert("你赢了!"); } } } }); // 右击格子 canvas.addEventListener("contextmenu", function(event) { event.preventDefault(); var x = event.pageX - canvas.offsetLeft; var y = event.pageY - canvas.offsetTop; var cell = getCell(x, y); if (cell !== null && !cell.isRevealed) { cell.isFlagged = !cell.isFlagged; drawCell(cell); } }); // 根据坐标获取格子 function getCell(x, y) { for (var i = 0; i < gridSize; i++) { for (var j = 0; j < gridSize; j++) { var cell = grid[i][j]; if (cell.x < x && x < cell.x + cell.width && cell.y < y && y < cell.y + cell.height) { return cell; } } } return null; } // 揭开格子 function revealCell(cell) { cell.isRevealed = true; drawCell(cell); if (cell.neighborCount === 0) { for (var row = cell.row - 1; row <= cell.row + 1; row++) { for (var col = cell.col - 1; col <= cell.col + 1; col++) { if (row >= 0 && row < gridSize && col >= 0 && col < gridSize) { var neighbor = grid[row][col]; if (!neighbor.isRevealed) { revealCell(neighbor); } } } } } } // 揭开所有炸弹 function revealBombs() { for (var i = 0; i < gridSize; i++) { for (var j = 0; j < gridSize; j++) { var cell = grid[i][j]; if (cell.isBomb) { cell.isRevealed = true; drawCell(cell); } } } } // 检查是否胜利 function checkWin() { for (var i = 0; i < gridSize; i++) { for (var j = 0; j < gridSize; j++) { var cell = grid[i][j]; if (!cell.isRevealed && !cell.isBomb) { return false; } } } return true; } // 绘制格子 function drawCell(cell) { context.fillStyle = "gray"; context.fillRect(cell.x, cell.y, cell.width, cell.height); if (cell.isRevealed) { if (cell.isBomb) { context.fillStyle = "red"; context.fillRect(cell.x, cell.y, cell.width, cell.height); } else { context.fillStyle = "white"; context.fillRect(cell.x, cell.y, cell.width, cell.height); if (cell.neighborCount > 0) { context.font = "20px Arial"; context.fillStyle = "black"; context.fillText(cell.neighborCount, cell.x + cell.width / 2, cell.y + cell.height / 2); } } } else { if (cell.isFlagged) { context.font = "20px Arial"; context.fillStyle = "black"; context.fillText("F", cell.x + cell.width / 2, cell.y + cell.height / 2); } } } </script> </body> </html> ``` 请注意,这只是一个简单的示例代码,您可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值