最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript版經(jīng)典游戲之掃雷游戲完整示例【附demo源碼下載】

 更新時(shí)間:2016年12月12日 08:45:10   作者:wangmeijian  
這篇文章主要介紹了JavaScript版經(jīng)典游戲之掃雷游戲?qū)崿F(xiàn)方法,結(jié)合完整實(shí)例形式分析了掃雷游戲的原理與具體實(shí)現(xiàn)流程,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下

本文實(shí)例講述了JavaScript掃雷游戲。分享給大家供大家參考,具體如下:

翻出年初寫(xiě)的游戲貼上來(lái),掃雷相信大家都玩過(guò),先上圖:

源碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Javascript版掃雷</title>
<style>
  input{ margin:0; padding:0;}
  input{ outline:none;}
  #game_box{ width:400px; height:430px; margin:100px auto; border:#333 1px solid; background:#ccc -webkit-repeating-linear-gradient(top,#ccc,#000);background:#ccc -moz-repeating-linear-gradient(top,#ccc,#000);background:#ccc -o-repeating-linear-gradient(top,#ccc,#000);background:#ccc -ms-repeating-linear-gradient(top,#ccc,#000); box-shadow:0 0 50px 10px #333; box-shadow:0 0 50px 10px #333;
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#CBCBCB', endColorstr='#000000');
   }
  #map{width:400px; height:348px;*height:380px;}
  #time{ height:18px; line-height:22px; text-align:center;margin:6px 0; margin-top:10px; _margin-top:40px; color:#fff; position:relative;}
  #time input{ width:40px; height:18px; line-height:18px; text-align:center;}
  #table_map{ width:336px; height:336px; border:none; border-left:#000 1px solid;border-top:#000 1px solid; margin:32px 32px 0px 32px; background:#D8ECF6; text-align:center; border-collapse: collapse}
  #table_map td{ width:20px; height:20px; border:none;border-right:#000 1px solid;border-bottom:#000 1px solid; color:#333333; transition:all 0.7s; cursor:pointer;}
  #table_map td.mask{ border:none;border-right:#000 1px solid;border-bottom:#000 1px solid; background:#333; }
  #table_map td.over{ border:none;border-right:#000 1px solid;border-bottom:#000 1px solid; }
  .over_bg{ background:#E1E1E1;}
  @-webkit-keyframes round{
    from{ -webkit-transform:rotateX(0);}
    to{ -webkit-transform:rotateX(360deg);}
  }
  @-webkit-keyframes show{
    from{ -webkit-transform:rotateX(180deg) rotateY(0) scale(0);}
    to{ -webkit-transform:rotateX(360deg) rotateY(360deg) scale(1);}
  }
  @-moz-keyframes round{
    from{ -moz-transform:rotateX(0);}
    to{ -moz-transform:rotateX(360deg);}
  }
  @-moz-keyframes show{
    from{ -moz-transform:rotateX(180deg) rotateY(0) scale(0);}
    to{ -moz-transform:rotateX(360deg) rotateY(360deg) scale(1);}
  }
  @-ms-keyframes round{
    from{ -ms-transform:rotateX(0);}
    to{ -ms-transform:rotateX(360deg);}
  }
  @-ms-keyframes show{
    from{ -ms-transform:rotateX(180deg) rotateY(0) scale(0);}
    to{ -ms-transform:rotateX(360deg) rotateY(360deg) scale(1);}
  }
  #game_box{-webkit-animation:show 3s;-moz-animation:show 3s;-ms-animation:show 3s;}
</style>
</head>
<body>
<div id="game_box">
  <div id="map"></div>
  <div id="time">時(shí)間:<input type="text" value="0" disabled="disabled" />&nbsp;&nbsp;炸彈:<input type="text" value="50" disabled="disabled" /></div>
</div>
<script>
// 2014年3月 by 王美建 QQ1207526854
  window.onload=function()
  {
    Game.init('game_box','map');
  };
  var Game={
    data : {  //關(guān)卡數(shù)據(jù)
      mine : 40, col : 16, row : 16
    },
    init : function(box_id,mapbox_id){ //初始化
      this.oBox = document.getElementById(box_id);
      this.mapBox = document.getElementById(mapbox_id)
      this.mapBox.innerHTML = this.createMap();
      this.oMap = this.mapBox.getElementsByTagName('table')[0];
      this.aTd = this.oMap.getElementsByTagName('td');
      this.time = document.getElementById('time').getElementsByTagName('input')[0];
      this.mineNum = document.getElementById('time').getElementsByTagName('input')[1];
      this.mineNum.value = this.data.mine;
      this.surplus = [];
      this.iCount = this.data.col*this.data.row-this.data.mine;
      this.createMine();
      this.addVal();
      this.play();
    },
    createMap : function() //生成畫(huà)布
    {
      var html = '';
      var This = this.data;
      var i=0,j=0;
      function createTd()
      {
        var tds = '';
        for(j = 0; j < This.row; j++)
        {
          tds += '<td class = "mask" index='+ (This.col*i+j) +'></td>';
        };
        return tds;
      }
      for(i = 0; i < This.col; i++)
      {
        html += '<tr>' + createTd() + '</tr>';
      };
      return ('<table id="table_map" cellpadding="0" cellspacing="0" ><tbody>'+html+'</table></tbody>');
    },
    createMine : function(){ //生成炸彈
      var This = this.data;
      this.indexArr = [];
      this.mineArr = [];
      for(var i = 0,j = This.col*This.row; i < j; i++ )
      {
        this.indexArr.push(i) ;  //所有單元格的索引
      };
      for( var i = 0; i < This.mine; i++ ) //隨機(jī)取出This.mine個(gè)做為炸彈的索引,不重復(fù)
      {
        var index = Math.round(Math.random()*(this.indexArr.length-1)); //索引
        this.mineArr.push(this.indexArr.splice(index,1)[0]);
      };
      this.mineArr.sort( function(a,b){return a-b;} );
    },
    addVal : function() //給有炸彈的td添加自定義屬性hasMine值為true
    {
      for(var i = 0, j = this.mineArr.length; i < j; i++)
      {
        this.aTd[ this.mineArr[i] ].hasMine = true;
      };
    },
    play : function()
    {//鼠標(biāo)左右鍵同時(shí)按下  ev.button=3
      this.timeOnoff = false;
      var This = this;
      this.markNum = [];
      this.oMap.oncontextmenu=function(ev)  //插旗標(biāo)記
      {
        var ev = ev || window.event;
        var target = ev.srcElement || ev.target;
        if( target.tagName.toLowerCase() == 'td' && target.className == 'mask' )
        {
          !target.mark ? (target.innerHTML = '▲',target.style.color = '#FFEFAD',target.mark = true,This.mineNum.value--,This.markNum.push( target.getAttribute( 'index' ) )) : (target.innerHTML = '',target.style.color = '#333333',target.mark = false,This.mineNum.value++,This.markNum.splice( This.findIndex( This.markNum,target.getAttribute( 'index' ) ),1 ));
        };
        return false;
      };
      this.oMap.onclick = function(ev)
      {
        var ev = ev || window.event;
        var target = ev.srcElement || ev.target;
        if(!This.timeOnoff) //開(kāi)始計(jì)時(shí)
        {
          This.timeOnoff = true;
          This.timer=setInterval(function(){
            This.time.style.webkitAnimation = 'round 1s infinite';   //計(jì)時(shí)器翻轉(zhuǎn)
            This.time.value++;
          },1000)
        };
        function openTd(aTd,meg,len)
        {
          var num = 0;
          var show = null;
          var t = Math.ceil( 15*This.data.col*This.data.row/len );  //未打開(kāi)格子越多,翻開(kāi)時(shí)間間隔越短
          show = setInterval(function(){
            aTd[ This.surplus[num] ].className = 'over';
            aTd[ This.surplus[num] ].style.webkitAnimation = 'round 1s 1';
            if( aTd[ This.surplus[num] ].hasMine && aTd[ This.surplus[num] ].className == 'over' )
            {
              aTd[ This.surplus[num] ].style.color = '#333333';
              aTd[ This.surplus[num] ].innerHTML = '★';
            }else{
              aTd[ This.surplus[num] ].innerHTML = '';
            };
            num++;
            if(num == len)
            {
              clearInterval(show);
              alert(meg);
            }
          },t)
        };
        function countSur() //統(tǒng)計(jì)沒(méi)打開(kāi)的格子的索引
        {
          var iCheck = 0;
          This.surplus = [];
          for( var i = 0,j = This.data.col*This.data.row; i < j; i++ )
          {
            if( This.aTd[i].className == 'mask' )
            {
              iCheck++;
              This.surplus.push( i );
            };
          };
          return iCheck;
        };
        if( target.tagName.toLowerCase() == 'td' && target.className == 'mask' && !target.hasMine )//沒(méi)踩到雷
        {
          This.count( parseInt(target.getAttribute('index')) ); //遞歸
          target.style.webkitAnimation = 'round 1s 1';
          if( This.iCount <= 10 ) //通關(guān)檢測(cè)
          {
            //console.log( 'iCheck='+iCheck+'iCount='+This.iCount )
            if( countSur() == This.data.mine ) //剩下格子數(shù)等于炸彈數(shù)而又沒(méi)踩到炸彈,可不就是過(guò)關(guān)了
            {
              clearInterval( This.timer );
              This.time.style.webkitAnimation = '';   //停止計(jì)時(shí)器翻轉(zhuǎn)
              openTd( This.aTd,'不錯(cuò)小伙子,過(guò)關(guān)了!用時(shí):'+This.time.value+'s',This.surplus.length );
            };
          };
        }else if( target.tagName.toLowerCase() == 'td' && target.className == 'mask' && target.hasMine ){ //踩到雷
          clearInterval(This.timer);   //停止計(jì)時(shí)器
          This.time.style.webkitAnimation = '';   //停止計(jì)時(shí)器翻轉(zhuǎn)
          var mineArr = This.mineArr;  //優(yōu)化:全局變局部
          var aTd = This.aTd;
          for( var i = 0,j = mineArr.length; i < j; i++ )
          {
            aTd[ mineArr[i] ].style.color = '#333333';
            aTd[ mineArr[i] ].className = 'over';
            aTd[ mineArr[i] ].innerHTML = '★';   //顯示炸彈標(biāo)志
          };
          target.style.color = 'red';
          countSur();
          openTd( aTd,'小朋友,你輸了~',This.surplus.length );
        };
      };
    },
    findIndex : function(arr,ele) //找到數(shù)組某個(gè)元素在數(shù)組中的位置
    {
      for(var i=0,j=arr.length;i<j;i++)
      {
        if(ele === arr[i])
        {
          return i;
        };
      };
      return -1;
    },
    count : function(iNow) //統(tǒng)計(jì)事件源周?chē)◤?
    {
      var arr = []; //事件源周?chē)母褡铀饕?
      var iNum = 0; //事件源周?chē)◤梻€(gè)數(shù)
      var len = this.data.col;
      if( iNow % len == 0 )  //點(diǎn)擊最左邊的格子時(shí)
      {
        arr = [iNow+1,iNow-len,iNow-len+1,iNow+len,iNow+len+1];
      }else if( iNow == ( Math.floor( iNow/len ) + 1 ) *len -1 ){ //點(diǎn)擊靠右邊的格子時(shí)
        arr = [iNow-1,iNow-len,iNow-len-1,iNow+len,iNow+len-1];
      }else{ //點(diǎn)擊不靠邊的格子時(shí)
        arr = [iNow-1,iNow+1,iNow-len,iNow-len+1,iNow-len-1,iNow+len,iNow+len-1,iNow+len+1];
      };
      for( var i = 0; i < arr.length; i++ ) //統(tǒng)計(jì)周?chē)◤?
      {
        if( this.aTd[ arr[ i ] ] && this.aTd[ arr[ i ] ].hasMine )
        {
          iNum++;
        }
      };
      if( iNum == 0 )  //當(dāng)前點(diǎn)擊格子周?chē)鷽](méi)有炸彈則遞歸
      {
        this.aTd[iNow].className = '';
        this.aTd[ iNow ].innerHTML == '▲' ? this.aTd[ iNow ].innerHTML = '' : '';
        for( var i = 0,j = arr.length; i < j; i++ )
        {
          if( this.aTd[ arr[ i ] ] && this.aTd[ arr[i] ].className == 'mask')
          {
            this.aTd[ arr[i] ].className = '';
            this.aTd[ arr[i] ].innerHTML == '▲' ? this.aTd[ arr[i] ].innerHTML = '' : '';
            this.iCount--;
            this.count( arr[i] );
          };
        };
      }else{ //當(dāng)前點(diǎn)擊格子周?chē)姓◤梽t顯示炸彈個(gè)數(shù)
        this.aTd[iNow].innerHTML = iNum;
        this.aTd[iNow].style.color = '#333333';
        this.aTd[iNow].style.webkitAnimation = 'round 1s 1';
        this.aTd[iNow].className == 'mask' ? (this.aTd[iNow].className = '',this.iCount--) : '';
      };
    }
  };
</script>
</body>
</html>

完整實(shí)例代碼點(diǎn)擊此處本站下載。

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動(dòng)畫(huà)特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》及《JavaScript遍歷算法與技巧總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 原生js實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)菜單

    原生js實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)菜單

    這篇文章主要為大家詳細(xì)介紹了原生js實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • web.js.字符串與正則表達(dá)式操作

    web.js.字符串與正則表達(dá)式操作

    這篇文章主要介紹了web.js.字符串與正則表達(dá)式操作的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • javascript判斷圖片是否加載完成的方法推薦

    javascript判斷圖片是否加載完成的方法推薦

    下面小編就為大家?guī)?lái)一篇javascript判斷圖片是否加載完成的方法推薦。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。
    2016-05-05
  • Javascript函數(shù)式編程簡(jiǎn)單介紹

    Javascript函數(shù)式編程簡(jiǎn)單介紹

    什么是函數(shù)式編程?根據(jù)百度百科的描述,“函數(shù)式編程是種編程典范,它將電腦運(yùn)算視為函數(shù)的計(jì)算。函數(shù)編程語(yǔ)言最重要的基礎(chǔ)是 λ 演算(lambda calculus)。而且λ演算的函數(shù)可以接受函數(shù)當(dāng)作輸入(參數(shù))和輸出(返回值)。”
    2015-10-10
  • js如何計(jì)算斐波那契數(shù)列第n項(xiàng)的值

    js如何計(jì)算斐波那契數(shù)列第n項(xiàng)的值

    這篇文章主要介紹了js如何計(jì)算斐波那契數(shù)列第n項(xiàng)的值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 通過(guò)函數(shù)作用域和塊級(jí)作用域看javascript的作用域鏈

    通過(guò)函數(shù)作用域和塊級(jí)作用域看javascript的作用域鏈

    這篇文章給大家分享了通過(guò)函數(shù)作用域和塊級(jí)作用域看javascript的作用域鏈的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友參考學(xué)習(xí)下。
    2018-08-08
  • javascript對(duì)象的多種合并方式詳解

    javascript對(duì)象的多種合并方式詳解

    這篇文章主要介紹了JavaScript中的多種對(duì)象合并方法,通過(guò)代碼的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下,希望能夠給你帶來(lái)幫助
    2021-08-08
  • 一文詳解uniapp中如何使用easycom自定義組件

    一文詳解uniapp中如何使用easycom自定義組件

    easycom是uniapp的一種組件自動(dòng)引入的規(guī)則,使用這種規(guī)則可以使?jié)M足規(guī)則的組件無(wú)需注冊(cè)直接使用,下面這篇文章主要給大家介紹了關(guān)于uniapp中如何使用easycom自定義組件的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • js使用removeChild方法動(dòng)態(tài)刪除div元素

    js使用removeChild方法動(dòng)態(tài)刪除div元素

    本節(jié)為大家介紹了js使用removeChild方法動(dòng)態(tài)刪除div元素,需要的朋友可以參考下
    2014-08-08
  • 一段非常簡(jiǎn)單的js判斷瀏覽器的內(nèi)核

    一段非常簡(jiǎn)單的js判斷瀏覽器的內(nèi)核

    先說(shuō)明,此處的方法是說(shuō)超級(jí)簡(jiǎn)單的方法,不是指代碼超級(jí)少,而是用非常簡(jiǎn)單的知識(shí)點(diǎn),只要懂得怎么寫(xiě)JavaScript的行內(nèi)樣式就可以判斷。
    2014-08-08

最新評(píng)論

亳州市| 平塘县| 板桥市| 林芝县| 甘谷县| 金华市| 南京市| 托里县| 梨树县| 新建县| 静安区| 天水市| 和静县| 温泉县| 广南县| 白河县| 台湾省| 阳城县| 卓尼县| 灵台县| 什邡市| 三明市| 神农架林区| 嵩明县| 宁阳县| 三明市| 府谷县| 禹州市| 河津市| 潮州市| 尚义县| 怀化市| 车险| 呼伦贝尔市| 万州区| 仙桃市| 西乌| 松滋市| 新和县| 永泰县| 和平县|