minesweeper / index.html
manmonth's picture
Add 2 files
9592d32 verified
Raw History Blame Contribute Delete
21.4 kB
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>精美扫雷游戏</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
/* 自定义样式 */
.cell {
transition: all 0.2s ease;
transform-style: preserve-3d;
perspective: 1000px;
}
.cell:hover {
transform: translateY(-2px) scale(1.02);
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}
.cell.revealed {
background-color: #f0f0f0;
transform: none; /* 移除旋转效果 */
}
.cell.flagged::after {
content: "🚩";
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 1.2rem;
}
.cell.mine::after {
content: "💣";
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 1.2rem;
}
.game-container {
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
border-radius: 15px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}
.number-1 { color: #1976D2; }
.number-2 { color: #388E3C; }
.number-3 { color: #D32F2F; }
.number-4 { color: #7B1FA2; }
.number-5 { color: #FF8F00; }
.number-6 { color: #0097A7; }
.number-7 { color: #5D4037; }
.number-8 { color: #455A64; }
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.05); }
100% { transform: scale(1); }
}
.pulse {
animation: pulse 1.5s infinite;
}
.modal {
transition: all 0.3s ease;
opacity: 0;
visibility: hidden;
}
.modal.active {
opacity: 1;
visibility: visible;
}
.difficulty-btn {
transition: all 0.2s ease;
}
.difficulty-btn:hover {
transform: translateY(-3px);
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body class="bg-gray-100 min-h-screen flex flex-col items-center justify-center p-4">
<!-- 游戏标题 -->
<div class="text-center mb-6">
<h1 class="text-4xl font-bold text-gray-800 mb-2">扫雷游戏</h1>
<p class="text-gray-600">点击方块揭开它们,标记可能有地雷的方块</p>
</div>
<!-- 游戏控制面板 -->
<div class="bg-white rounded-lg shadow-md p-4 mb-6 flex justify-between items-center w-full max-w-md">
<div class="flex items-center space-x-4">
<div class="bg-gray-100 p-2 rounded-lg">
<span class="text-gray-700 font-medium">剩余地雷:</span>
<span id="mines-count" class="ml-2 text-xl font-bold text-red-600">10</span>
</div>
<div class="bg-gray-100 p-2 rounded-lg">
<span class="text-gray-700 font-medium">用时:</span>
<span id="timer" class="ml-2 text-xl font-bold text-blue-600">0</span>
</div>
</div>
<button id="reset-btn" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg transition flex items-center">
<i class="fas fa-redo mr-2"></i> 重新开始
</button>
</div>
<!-- 游戏主界面 -->
<div class="game-container p-6 shadow-lg rounded-xl">
<div id="board" class="grid gap-1"></div>
</div>
<!-- 难度选择按钮 -->
<div class="mt-6 flex space-x-4">
<button id="easy-btn" class="difficulty-btn bg-green-500 hover:bg-green-600 text-white px-4 py-2 rounded-lg">
简单 (9×9, 10雷)
</button>
<button id="medium-btn" class="difficulty-btn bg-yellow-500 hover:bg-yellow-600 text-white px-4 py-2 rounded-lg">
中等 (16×16, 40雷)
</button>
<button id="hard-btn" class="difficulty-btn bg-red-500 hover:bg-red-600 text-white px-4 py-2 rounded-lg">
困难 (30×16, 99雷)
</button>
</div>
<!-- 游戏结束模态框 -->
<div id="game-over-modal" class="modal fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
<div class="bg-white rounded-xl p-8 max-w-md w-full mx-4 text-center transform transition-all">
<h2 id="result-title" class="text-3xl font-bold mb-4">游戏结束!</h2>
<p id="result-message" class="text-xl mb-6">你踩到了地雷!</p>
<div class="text-2xl font-bold mb-6">
用时: <span id="final-time" class="text-blue-600">0</span> 秒
</div>
<button id="play-again-btn" class="bg-blue-500 hover:bg-blue-600 text-white px-6 py-3 rounded-lg text-lg font-medium transition">
再玩一次
</button>
</div>
</div>
<!-- 游戏胜利模态框 -->
<div id="win-modal" class="modal fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
<div class="bg-white rounded-xl p-8 max-w-md w-full mx-4 text-center transform transition-all">
<div class="pulse text-5xl mb-4">🎉</div>
<h2 class="text-3xl font-bold mb-4 text-green-600">恭喜你赢了!</h2>
<p class="text-xl mb-6">你成功避开了所有地雷!</p>
<div class="text-2xl font-bold mb-6">
用时: <span id="win-time" class="text-blue-600">0</span> 秒
</div>
<button id="win-play-again-btn" class="bg-green-500 hover:bg-green-600 text-white px-6 py-3 rounded-lg text-lg font-medium transition">
再玩一次
</button>
</div>
</div>
<!-- 游戏说明 -->
<div class="mt-8 bg-white p-6 rounded-lg shadow-md max-w-2xl w-full">
<h3 class="text-xl font-bold mb-4 text-gray-800">游戏说明</h3>
<ul class="space-y-2 text-gray-700">
<li><i class="fas fa-mouse-pointer text-blue-500 mr-2"></i> 左键点击方块揭开它</li>
<li><i class="fas fa-flag text-red-500 mr-2"></i> 右键点击方块标记可能有地雷的方块</li>
<li><i class="fas fa-bomb text-gray-700 mr-2"></i> 数字表示周围8个方块中有多少地雷</li>
<li><i class="fas fa-lightbulb text-yellow-500 mr-2"></i> 揭开所有非地雷方块即可获胜</li>
</ul>
</div>
<script>
// 游戏状态
const gameState = {
board: [],
rows: 9,
cols: 9,
mines: 10,
revealedCount: 0,
flaggedCount: 0,
gameOver: false,
gameWon: false,
firstClick: true,
timer: 0,
timerInterval: null,
difficulty: 'easy'
};
// DOM 元素
const boardElement = document.getElementById('board');
const minesCountElement = document.getElementById('mines-count');
const timerElement = document.getElementById('timer');
const resetBtn = document.getElementById('reset-btn');
const easyBtn = document.getElementById('easy-btn');
const mediumBtn = document.getElementById('medium-btn');
const hardBtn = document.getElementById('hard-btn');
const gameOverModal = document.getElementById('game-over-modal');
const winModal = document.getElementById('win-modal');
const resultTitle = document.getElementById('result-title');
const resultMessage = document.getElementById('result-message');
const finalTime = document.getElementById('final-time');
const winTime = document.getElementById('win-time');
const playAgainBtn = document.getElementById('play-again-btn');
const winPlayAgainBtn = document.getElementById('win-play-again-btn');
// 初始化游戏
function initGame() {
// 重置游戏状态
gameState.board = [];
gameState.revealedCount = 0;
gameState.flaggedCount = 0;
gameState.gameOver = false;
gameState.gameWon = false;
gameState.firstClick = true;
gameState.timer = 0;
// 清除计时器
if (gameState.timerInterval) {
clearInterval(gameState.timerInterval);
gameState.timerInterval = null;
}
// 更新UI
updateMinesCount();
updateTimer();
// 创建游戏板
createBoard();
// 关闭所有模态框
gameOverModal.classList.remove('active');
winModal.classList.remove('active');
}
// 创建游戏板
function createBoard() {
// 清空游戏板
boardElement.innerHTML = '';
// 设置网格样式
boardElement.className = 'grid gap-1';
boardElement.style.gridTemplateColumns = `repeat(${gameState.cols}, minmax(0, 1fr))`;
// 初始化游戏板数组
for (let row = 0; row < gameState.rows; row++) {
gameState.board[row] = [];
for (let col = 0; col < gameState.cols; col++) {
gameState.board[row][col] = {
isMine: false,
revealed: false,
flagged: false,
neighborMines: 0
};
// 创建单元格元素
const cell = document.createElement('div');
cell.className = 'cell w-10 h-10 bg-blue-100 rounded-md flex items-center justify-center cursor-pointer relative overflow-hidden';
cell.dataset.row = row;
cell.dataset.col = col;
// 添加事件监听器
cell.addEventListener('click', handleCellClick);
cell.addEventListener('contextmenu', handleRightClick);
boardElement.appendChild(cell);
}
}
}
// 放置地雷
function placeMines(firstRow, firstCol) {
let minesPlaced = 0;
while (minesPlaced < gameState.mines) {
const row = Math.floor(Math.random() * gameState.rows);
const col = Math.floor(Math.random() * gameState.cols);
// 确保第一个点击的方块及其周围没有地雷
const isFirstClickArea = Math.abs(row - firstRow) <= 1 && Math.abs(col - firstCol) <= 1;
if (!gameState.board[row][col].isMine && !isFirstClickArea) {
gameState.board[row][col].isMine = true;
minesPlaced++;
}
}
// 计算每个方块周围的地雷数
calculateNeighborMines();
}
// 计算每个方块周围的地雷数
function calculateNeighborMines() {
for (let row = 0; row < gameState.rows; row++) {
for (let col = 0; col < gameState.cols; col++) {
if (!gameState.board[row][col].isMine) {
let count = 0;
// 检查周围8个方块
for (let r = Math.max(0, row - 1); r <= Math.min(gameState.rows - 1, row + 1); r++) {
for (let c = Math.max(0, col - 1); c <= Math.min(gameState.cols - 1, col + 1); c++) {
if (gameState.board[r][c].isMine) {
count++;
}
}
}
gameState.board[row][col].neighborMines = count;
}
}
}
}
// 处理单元格点击
function handleCellClick(event) {
if (gameState.gameOver || gameState.gameWon) return;
const cell = event.target;
const row = parseInt(cell.dataset.row);
const col = parseInt(cell.dataset.col);
const cellData = gameState.board[row][col];
// 如果已标记或已揭开,则忽略
if (cellData.flagged || cellData.revealed) return;
// 第一次点击时放置地雷并开始计时
if (gameState.firstClick) {
placeMines(row, col);
startTimer();
gameState.firstClick = false;
}
// 如果点击到地雷
if (cellData.isMine) {
gameOver(row, col);
return;
}
// 揭开方块
revealCell(row, col);
// 检查是否获胜
checkWin();
}
// 处理右键点击(标记)
function handleRightClick(event) {
event.preventDefault();
if (gameState.gameOver || gameState.gameWon) return;
const cell = event.target;
const row = parseInt(cell.dataset.row);
const col = parseInt(cell.dataset.col);
const cellData = gameState.board[row][col];
// 如果已揭开,则忽略
if (cellData.revealed) return;
// 切换标记状态
cellData.flagged = !cellData.flagged;
// 更新标记计数
gameState.flaggedCount += cellData.flagged ? 1 : -1;
updateMinesCount();
// 更新UI
if (cellData.flagged) {
cell.classList.add('flagged');
} else {
cell.classList.remove('flagged');
}
}
// 揭开方块
function revealCell(row, col) {
const cellData = gameState.board[row][col];
// 如果已揭开或已标记,则忽略
if (cellData.revealed || cellData.flagged) return;
// 标记为已揭开
cellData.revealed = true;
gameState.revealedCount++;
// 获取对应的DOM元素
const cellIndex = row * gameState.cols + col;
const cell = boardElement.children[cellIndex];
// 添加揭开样式
cell.classList.add('revealed');
// 如果是空白方块,递归揭开周围的方块
if (cellData.neighborMines === 0) {
for (let r = Math.max(0, row - 1); r <= Math.min(gameState.rows - 1, row + 1); r++) {
for (let c = Math.max(0, col - 1); c <= Math.min(gameState.cols - 1, col + 1); c++) {
if (r !== row || c !== col) {
revealCell(r, c);
}
}
}
} else {
// 显示周围地雷数
const numberSpan = document.createElement('span');
numberSpan.className = `number-${cellData.neighborMines} font-bold`;
numberSpan.textContent = cellData.neighborMines;
cell.appendChild(numberSpan);
}
}
// 游戏结束
function gameOver(row, col) {
gameState.gameOver = true;
clearInterval(gameState.timerInterval);
// 显示所有地雷
for (let r = 0; r < gameState.rows; r++) {
for (let c = 0; c < gameState.cols; c++) {
const cellData = gameState.board[r][c];
const cellIndex = r * gameState.cols + c;
const cell = boardElement.children[cellIndex];
if (cellData.isMine) {
cell.classList.add('mine');
cell.classList.add('revealed');
}
}
}
// 高亮引爆的地雷
const cellIndex = row * gameState.cols + col;
const cell = boardElement.children[cellIndex];
cell.classList.add('bg-red-200');
// 显示游戏结束模态框
resultTitle.textContent = '游戏结束!';
resultMessage.textContent = '你踩到了地雷!';
finalTime.textContent = gameState.timer;
gameOverModal.classList.add('active');
}
// 检查是否获胜
function checkWin() {
const totalSafeCells = gameState.rows * gameState.cols - gameState.mines;
if (gameState.revealedCount === totalSafeCells) {
gameState.gameWon = true;
clearInterval(gameState.timerInterval);
// 标记所有地雷
for (let r = 0; r < gameState.rows; r++) {
for (let c = 0; c < gameState.cols; c++) {
const cellData = gameState.board[r][c];
const cellIndex = r * gameState.cols + c;
const cell = boardElement.children[cellIndex];
if (cellData.isMine) {
cell.classList.add('flagged');
}
}
}
// 显示胜利模态框
winTime.textContent = gameState.timer;
winModal.classList.add('active');
}
}
// 开始计时器
function startTimer() {
gameState.timer = 0;
updateTimer();
gameState.timerInterval = setInterval(() => {
gameState.timer++;
updateTimer();
}, 1000);
}
// 更新计时器显示
function updateTimer() {
timerElement.textContent = gameState.timer;
}
// 更新剩余地雷数显示
function updateMinesCount() {
minesCountElement.textContent = gameState.mines - gameState.flaggedCount;
}
// 设置游戏难度
function setDifficulty(difficulty) {
gameState.difficulty = difficulty;
switch (difficulty) {
case 'easy':
gameState.rows = 9;
gameState.cols = 9;
gameState.mines = 10;
break;
case 'medium':
gameState.rows = 16;
gameState.cols = 16;
gameState.mines = 40;
break;
case 'hard':
gameState.rows = 16;
gameState.cols = 30;
gameState.mines = 99;
break;
}
// 更新按钮样式
easyBtn.classList.remove('ring-2', 'ring-blue-500');
mediumBtn.classList.remove('ring-2', 'ring-blue-500');
hardBtn.classList.remove('ring-2', 'ring-blue-500');
document.getElementById(`${difficulty}-btn`).classList.add('ring-2', 'ring-blue-500');
// 重新初始化游戏
initGame();
}
// 事件监听器
resetBtn.addEventListener('click', initGame);
playAgainBtn.addEventListener('click', initGame);
winPlayAgainBtn.addEventListener('click', initGame);
easyBtn.addEventListener('click', () => setDifficulty('easy'));
mediumBtn.addEventListener('click', () => setDifficulty('medium'));
hardBtn.addEventListener('click', () => setDifficulty('hard'));
// 初始化游戏
setDifficulty('easy');
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=manmonth/minesweeper" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>