当前位置: 首页 > news >正文

扫雷-HTML

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>扫雷游戏</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}

.game-container {
background: white;
border-radius: 20px;
padding: 30px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}

h1 {
text-align: center;
color: #333;
margin-bottom: 20px;
font-size: 2.5em;
}

.controls {
display: flex;
justify-content: center;
gap: 15px;
margin-bottom: 20px;
flex-wrap: wrap;
}

.controls select, .controls button {
padding: 10px 20px;
font-size: 16px;
border: 2px solid #667eea;
border-radius: 10px;
cursor: pointer;
background: white;
transition: all 0.3s;
}

.controls button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
}

.controls button:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

.status-bar {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
padding: 15px;
background: #f5f5f5;
border-radius: 10px;
}

.status-item {
font-size: 18px;
color: #333;
}

.status-item span {
font-weight: bold;
color: #667eea;
}

.emoji-btn {
font-size: 32px;
cursor: pointer;
background: none;
border: none;
transition: transform 0.2s;
}

.emoji-btn:hover {
transform: scale(1.2);
}

.game-board {
display: grid;
gap: 2px;
background: #bbada0;
padding: 5px;
border-radius: 10px;
margin: 0 auto;
width: fit-content;
}

.cell {
width: 40px;
height: 40px;
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
font-weight: bold;
cursor: pointer;
border-radius: 5px;
transition: all 0.1s;
user-select: none;
}

.cell.hidden {
background: linear-gradient(145deg, #c9c9c9, #d9d9d9);
box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
}

.cell.hidden:hover {
background: linear-gradient(145deg, #d9d9d9, #e9e9e9);
transform: scale(1.05);
}

.cell.revealed {
background: #e0e0e0;
cursor: default;
}

.cell.flagged {
background: linear-gradient(145deg, #c9c9c9, #d9d9d9);
}

.cell.mine {
background: #ff4444;
}

.cell.mine-revealed {
background: #ff4444;
animation: shake 0.5s;
}

@keyframes shake {
0%, 100% { transform: translateX(0); }
25% { transform: translateX(-5px); }
75% { transform: translateX(5px); }
}

.cell[data-number="1"] { color: #1976d2; }
.cell[data-number="2"] { color: #388e3c; }
.cell[data-number="3"] { color: #d32f2f; }
.cell[data-number="4"] { color: #7b1fa2; }
.cell[data-number="5"] { color: #ff8f00; }
.cell[data-number="6"] { color: #0097a7; }
.cell[data-number="7"] { color: #424242; }
.cell[data-number="8"] { color: #9e9e9e; }

.message {
text-align: center;
font-size: 24px;
font-weight: bold;
margin-top: 20px;
min-height: 30px;
}

.message.win {
color: #4caf50;
}

.message.lose {
color: #f44336;
}

.instructions {
margin-top: 20px;
padding: 15px;
background: #f0f0f0;
border-radius: 10px;
font-size: 14px;
color: #666;
}

.instructions h3 {
margin-bottom: 10px;
color: #333;
}

.instructions ul {
margin-left: 20px;
}

.instructions li {
margin: 5px 0;
}
</style>
</head>
<body>
<div class="game-container">
<h1>💣 扫雷 💣</h1>

<div class="controls">
<select id="difficulty">
<option value="easy">简单 (9×9, 10 雷)</option>
<option value="medium" selected>中等 (16×16, 40 雷)</option>
<option value="hard">困难 (30×16, 99 雷)</option>
</select>
<button onclick="initGame()">🔄 新游戏</button>
</div>

<div class="status-bar">
<div class="status-item">💣 地雷:<span id="mine-count">40</span></div>
<button class="emoji-btn" id="face-btn" onclick="initGame()">🙂</button>
<div class="status-item">⏱️ 时间:<span id="timer">0</span></div>
</div>

<div class="game-board" id="game-board"></div>

<div class="message" id="message"></div>

<div class="instructions">
<h3>📖 游戏规则</h3>
<ul>
<li>左键点击:揭开方块</li>
<li>右键点击:标记/取消标记地雷</li>
<li>揭开数字表示周围 8 格的地雷数量</li>
<li>揭开所有非雷方块即可获胜</li>
<li>点到地雷就游戏结束!</li>
</ul>
</div>
</div>

<script>
const difficulties = {
easy: { rows: 9, cols: 9, mines: 10 },
medium: { rows: 16, cols: 16, mines: 40 },
hard: { rows: 16, cols: 30, mines: 99 }
};

let currentDifficulty = 'medium';
let board = [];
let revealed = [];
let flagged = [];
let gameOver = false;
let timer = 0;
let timerInterval = null;
let firstClick = true;

function initGame() {
const settings = difficulties[currentDifficulty];
board = [];
revealed = [];
flagged = [];
gameOver = false;
timer = 0;
firstClick = true;

if (timerInterval) {
clearInterval(timerInterval);
timerInterval = null;
}

document.getElementById('timer').textContent = '0';
document.getElementById('mine-count').textContent = settings.mines;
document.getElementById('face-btn').textContent = '🙂';
document.getElementById('message').textContent = '';
document.getElementById('message').className = 'message';

for (let i = 0; i < settings.rows; i++) {
board[i] = [];
revealed[i] = [];
flagged[i] = [];
for (let j = 0; j < settings.cols; j++) {
board[i][j] = 0;
revealed[i][j] = false;
flagged[i][j] = false;
}
}

renderBoard();
}

function placeMines(excludeRow, excludeCol) {
const settings = difficulties[currentDifficulty];
let minesPlaced = 0;

while (minesPlaced < settings.mines) {
const row = Math.floor(Math.random() * settings.rows);
const col = Math.floor(Math.random() * settings.cols);

// 排除第一次点击的位置及其周围
if (Math.abs(row - excludeRow) <= 1 && Math.abs(col - excludeCol) <= 1) {
continue;
}

if (board[row][col] === 0) {
board[row][col] = -1; // -1 表示地雷
minesPlaced++;
}
}

// 计算每个格子周围的地雷数
for (let i = 0; i < settings.rows; i++) {
for (let j = 0; j < settings.cols; j++) {
if (board[i][j] !== -1) {
board[i][j] = countAdjacentMines(i, j);
}
}
}
}

function countAdjacentMines(row, col) {
const settings = difficulties[currentDifficulty];
let count = 0;

for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
const newRow = row + i;
const newCol = col + j;
if (newRow >= 0 && newRow < settings.rows && newCol >= 0 && newCol < settings.cols) {
if (board[newRow][newCol] === -1) {
count++;
}
}
}
}

return count;
}

function revealCell(row, col) {
const settings = difficulties[currentDifficulty];

if (gameOver || revealed[row][col] || flagged[row][col]) {
return;
}

// 第一次点击时放置地雷
if (firstClick) {
firstClick = false;
placeMines(row, col);
startTimer();
}

revealed[row][col] = true;
const cell = document.querySelector(`[data-row="${row}"][data-col="${col}"]`);

if (board[row][col] === -1) {
// 点到地雷,游戏结束
gameOver = true;
cell.classList.remove('hidden');
cell.classList.add('mine-revealed');
cell.textContent = '💣';
document.getElementById('face-btn').textContent = '😵';
document.getElementById('message').textContent = '💥 游戏结束!你踩到地雷了!';
document.getElementById('message').className = 'message lose';
revealAllMines();
stopTimer();
return;
}

cell.classList.remove('hidden');
cell.classList.add('revealed');

if (board[row][col] > 0) {
cell.textContent = board[row][col];
cell.setAttribute('data-number', board[row][col]);
} else {
// 空格子,递归揭开周围格子
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
const newRow = row + i;
const newCol = col + j;
if (newRow >= 0 && newRow < settings.rows && newCol >= 0 && newCol < settings.cols) {
revealCell(newRow, newCol);
}
}
}
}

checkWin();
}

function toggleFlag(row, col) {
if (gameOver || revealed[row][col]) {
return;
}

const settings = difficulties[currentDifficulty];
const cell = document.querySelector(`[data-row="${row}"][data-col="${col}"]`);

if (flagged[row][col]) {
flagged[row][col] = false;
cell.classList.remove('flagged');
cell.textContent = '';
document.getElementById('mine-count').textContent = parseInt(document.getElementById('mine-count').textContent) + 1;
} else {
flagged[row][col] = true;
cell.classList.add('flagged');
cell.textContent = '🚩';
document.getElementById('mine-count').textContent = parseInt(document.getElementById('mine-count').textContent) - 1;
}
}

function revealAllMines() {
const settings = difficulties[currentDifficulty];
for (let i = 0; i < settings.rows; i++) {
for (let j = 0; j < settings.cols; j++) {
if (board[i][j] === -1) {
const cell = document.querySelector(`[data-row="${i}"][data-col="${j}"]`);
cell.classList.remove('hidden');
if (flagged[i][j]) {
cell.classList.add('revealed');
} else {
cell.classList.add('mine-revealed');
}
cell.textContent = '💣';
}
}
}
}

function checkWin() {
const settings = difficulties[currentDifficulty];
let unrevealedSafeCells = 0;

for (let i = 0; i < settings.rows; i++) {
for (let j = 0; j < settings.cols; j++) {
if (!revealed[i][j] && board[i][j] !== -1) {
unrevealedSafeCells++;
}
}
}

if (unrevealedSafeCells === 0) {
gameOver = true;
document.getElementById('face-btn').textContent = '😎';
document.getElementById('message').textContent = '🎉 恭喜!你赢了!🎉';
document.getElementById('message').className = 'message win';
stopTimer();
}
}

function startTimer() {
timerInterval = setInterval(() => {
timer++;
document.getElementById('timer').textContent = timer;
}, 1000);
}

function stopTimer() {
if (timerInterval) {
clearInterval(timerInterval);
timerInterval = null;
}
}

function renderBoard() {
const settings = difficulties[currentDifficulty];
const gameBoard = document.getElementById('game-board');
gameBoard.innerHTML = '';
gameBoard.style.gridTemplateColumns = `repeat(${settings.cols}, 40px)`;

for (let i = 0; i < settings.rows; i++) {
for (let j = 0; j < settings.cols; j++) {
const cell = document.createElement('div');
cell.className = 'cell hidden';
cell.dataset.row = i;
cell.dataset.col = j;
cell.addEventListener('click', () => revealCell(i, j));
cell.addEventListener('contextmenu', (e) => {
e.preventDefault();
toggleFlag(i, j);
});
gameBoard.appendChild(cell);
}
}
}

document.getElementById('difficulty').addEventListener('change', (e) => {
currentDifficulty = e.target.value;
initGame();
});

// 初始化游戏
initGame();
</script>
</body>
</html>

http://www.jsqmd.com/news/586747/

相关文章:

  • 2026年会计学论文降AI工具推荐:数据表格和财务分析部分怎么降 - 还在做实验的师兄
  • 第二十四章 专属客服护航:落地售后不踩坑,全程有人帮
  • Keil中内存概念:Flash、SRAM、RO、RW、ZI、.data、.bss、heap、stack、MAP文件
  • 用干词背单词,30天轻松背完小学词库1200单词!
  • 告别EFI配置噩梦:OpCore-Simplify如何重新定义Hackintosh体验
  • 如何彻底解决Windows自动休眠问题?MouseJiggler全场景应用指南
  • MySQL的每一行数据永远都有三个隐藏字段吗?
  • 2026年4月克拉管品牌怎么选择,抗疲劳特性,克拉管长期使用佳 - 品牌推荐师
  • 【CSDN重磅】50+维度董事长智能建模系统:基于OpenCV的领导者数字孪生实战
  • tcc-g15:Dell G15笔记本的智能散热调控与全场景适配方案
  • 猫抓:网页资源下载终极解决方案,让媒体获取从未如此简单
  • 2026六安汽车贴膜第三方横向评测:四大官方授权门店深度对比 - GrowthUME
  • 第七章 技术栈全景:支撑千万级工业互联网平台的技术选型考量
  • 基于计算机视觉、利用NVIDIATAO工具包与YOLOv8实现印度智慧城市场景下骑行人员未佩戴头盔违规检测与车辆识别
  • 让旧款Mac焕发新生:OpenCore Legacy Patcher完全指南
  • 突破网盘下载瓶颈:开源工具如何重塑你的文件获取体验
  • 多显示器壁纸终极解决方案:Superpaper 完整指南
  • 5分钟掌握Label Studio ML Backend:打造企业级AI标注自动化系统
  • 【AI Engineering】身体已成功 Handshake 回家,内网 Agent 仍在 504 Timeout 里闭门思过!
  • AI教材编写秘籍:掌握这些方法,用AI写出低查重率的优质教材!
  • AI Coding与单元测试的协同进化:从验证到驱动
  • 1013 Battle Over Cities(比较好的一题)
  • 二分边界防止循环
  • 探寻ROHS2.0检测仪适合哪些行业使用,生产商哪家靠谱 - myqiye
  • 终极Dlib预编译包指南:高效解决Windows环境安装难题
  • STC15F2K60S2单片机最小系统板DIY指南:从选件到焊接,一次点亮
  • 杭州高端腕表鉴定真假全攻略:30+奢华品牌防伪解析、地域案例与6城服务对比 - 时光修表匠
  • 分析rohs2.0检测仪厂商哪家好,分享价格区间和品牌推荐 - mypinpai
  • B站Windows客户端高效解决方案:告别浏览器困扰,打造专业视频体验平台
  • 秒传技术突破:如何让文件分享效率提升10倍的底层逻辑与实践指南