1. 程式人生 > >Javascript 綜合示例 網頁掃雷遊戲

Javascript 綜合示例 網頁掃雷遊戲

 ---------------認定了的事情,只要是對的,幹到底!

----------------------------------------------------------------------------------------------------------------------------------------------

分別建立 HTML CSS JS 三個檔案  

加上 儲存好的圖片 

 

----------------------------------------------------------------------------------------------------------------------------------------------

 

----------------------------------------------------------------------------------------------------------------------------------------------

 

 

----------------------------------------------------------------------------------------------------------------------------------------------

 HTML程式碼 

----------------------------------------------------------------------------------------------------------------------------------------------

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>原生JS~掃雷</title>
<link rel="stylesheet" href="demo.css">
</head>
<body>
<div class="wrapper">
<div class="btn" id="btn"></div>
<div class="box" id="box"></div>
<div class="flagBox" id="flagBox">
當前剩餘雷數:
<span id="score">10</span>
</div>
<div class="alertBox" id="alertBox">
<div class="alertImg" id="alertImg">
<div class="close" id="close"></div>
</div>
</div>
</div>
<script src="demo.js"></script>
</body>
</html>

----------------------------------------------------------------------------------------------------------------------------------------------

 CSS程式碼 

----------------------------------------------------------------------------------------------------------------------------------------------

*{
margin:0;
padding:0;
}
.wrapper {
width:100%;
height:1000px;
position: fixed;
top:0;
left:0;
background-image: url('img/bg.jpg');
background-size: 100% 100%;
}

.btn{
height:140px;
width:170px;
position:absolute;
left:50px;
background-image: url('img/startGame.png');
background-size: 100% 100%;
cursor: pointer;
}

.box{
height:500px;
width:500px;
transform: perspective(800px) rotateX(45deg);
margin:20px auto;
border-top:1px solid #B25F27;
border-left:1px solid #B25F27;
box-shadow: 5px 5px 5px rgba(0,0,0,0.3);

display:none;
}

.flagBox{
position:absolute;
top:50px;
left:50%;
width:200px;
height:50px;
margin-left:-100px;
color:#333;
font-size:20px;
font-weight: bolder;
display:none;
}
.alertBox{
display:none;
position:absolute;
width:100%;
height:100%;
left:0;
top:0;
background-color: rgba(0,0,0,0.2);
}

.alertImg{
width:600px;
height:400px;
background-size: 100% 100%;
position:absolute;
left:0;
top:0;
right:0;
bottom:0;
margin:auto;
border-radius: 20px;
}

.close{
position:absolute;
right:0;
top:0;
height:40px;
width:40px;
background-image: url('img/closeBtn.png');
background-size: 100% 100%;
cursor: pointer;

}

.block{
width:49px;
height:49px;
border-right:1px solid #B25F27;
border-bottom:1px solid #B25F27;
box-shadow: 0 0 4px #333 inset;
background-image: url('img/cao.jpg');
float: left;
}

.show{
background-image: url('img/dilei.jpg');
background-size: 100% 100%;
}

.num{
background:#ECD0A1;
font-size:18px;
font-weight:bold;
line-height: 49px;
text-align: center;
}
.flag{
background-image:url('img/hongqi.jpg');
background-size:100% 100%;
}

----------------------------------------------------------------------------------------------------------------------------------------------

JS程式碼 

----------------------------------------------------------------------------------------------------------------------------------------------

//點選開始遊戲 -》 動態生成100個小格--》100div
//leftClick 沒有雷 --》顯示數字(代表以當前小格為中心周圍8個格的雷數) 擴散(當前周圍八個格沒有雷)
// 有累 --》game Over
//rightClick 沒有標記並且沒有數字--》進行標記。 有標記 --》取消標記 --》標記是否正確,10個都正確標記,提示成功
//已經出現數字--》無效果


var startBtn = document.getElementById('btn');
var box = document.getElementById('box');
var flagBox = document.getElementById('flagBox');
var alertBox = document.getElementById('alertBox');
var alertImg = document.getElementById('alertImg');
var closeBtn = document.getElementById('close');
var score = document.getElementById('score');
var minesNum;
var mineOver;
var block;
var mineMap = [];
var startGameBool = true;

bindEvent();
function bindEvent() {
startBtn.onclick = function () {
if(startGameBool){
box.style.display = 'block';
flagBox.style.display = 'block';
init();
startGameBool = false;
}

}
box.oncontextmenu = function () {
return false;
}
box.onmousedown = function (e) {
var event = e.target;
if (e.which == 1) {
leftClick(event);
} else if (e.which == 3) {
rightClick(event);
}
}
closeBtn.onclick = function () {
alertBox.style.display = 'none';
flagBox.style.display = 'none';
box.style.display = 'none';
box.innerHTML = '';
startGameBool = true;
}
}

function init() {
minesNum = 10;
mineOver = 10;
score.innerHTML = mineOver;

for (var i = 0; i < 10; i++) {
for (var j = 0; j < 10; j++) {
var con = document.createElement('div');
con.classList.add('block');
con.setAttribute('id', i + '-' + j);
box.appendChild(con);
mineMap.push({ mine: 0 });
}
}
block = document.getElementsByClassName('block');
while (minesNum) {
var mineIndex = Math.floor(Math.random() * 100);
if (mineMap[mineIndex].mine === 0) {
mineMap[mineIndex].mine = 1;
block[mineIndex].classList.add('isLei');
minesNum--;
}
}
}

function leftClick(dom) {
if(dom.classList.contains('flag')){
return;
}
var isLei = document.getElementsByClassName('isLei');
if (dom && dom.classList.contains('isLei')) {
for (var i = 0; i < isLei.length; i++) {
isLei[i].classList.add('show');
}
setTimeout(function () {
alertBox.style.display = 'block';
alertImg.style.backgroundImage = 'url("img/over.jpg")';
}, 800)
} else {
var n = 0;
var posArr = dom && dom.getAttribute('id').split('-');
var posX = posArr && +posArr[0];
var posY = posArr && +posArr[1];
dom && dom.classList.add('num');
for (var i = posX - 1; i <= posX + 1; i++) {
for (var j = posY - 1; j <= posY + 1; j++) {
var aroundBox = document.getElementById(i + '-' + j);
if (aroundBox && aroundBox.classList.contains('isLei')) {
n++;
}
}
}
dom && (dom.innerHTML = n);
if (n == 0) {
for (var i = posX - 1; i <= posX + 1; i++) {
for (var j = posY - 1; j <= posY + 1; j++) {
var nearBox = document.getElementById(i + '-' + j);
if (nearBox && nearBox.length != 0) {
if (!nearBox.classList.contains('check')) {
nearBox.classList.add('check');
leftClick(nearBox);
}
}
}
}
}
}
}

function rightClick(dom){
if(dom.classList.contains('num')){
return;
}
dom.classList.toggle('flag');
if(dom.classList.contains('isLei') &&dom.classList.contains('flag')){
mineOver --;
}
if(dom.classList.contains('isLei') && !dom.classList.contains('flag')){
mineOver ++;
}

score.innerHTML = mineOver;
if(mineOver == 0){
alertBox.style.display = 'block';
alertImg.style.backgroundImage = 'url("img/success.png")';
}
}

----------------------------------------------------------------------------------------------------------------------------------------------

圖片檔案 

----------------------------------------------------------------------------------------------------------------------------------------------