天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

JS 拼圖游戲 面向?qū)ο螅⑨屚暾?/h1>

在線演示 http://img.jb51.NET/online/pintu/pintu.htm
復(fù)制代碼 代碼如下:
<html>
<head>
<title>JS拼圖游戲</title>
<style>
    body{
        font-size:9pt;
    }
table{
border-collapse: collapse;
}
input{
    width:20px;
}
</style>
</head>
<body>
    JS原創(chuàng)作品:JS拼圖游戲<br>
    注釋完整, 面向?qū)ο?lt;br>
    轉(zhuǎn)載請注明來自<a href="http://blog.csdn.NET/sunxing007">http://blog.csdn.NET/sunxing007</a><br>
    <input type="text" id="lines" value='3'/>行<input type="text" id="cols" value='3'/>列    <button id="start"> 開 始 </button><br>
<table id="board" border=1 cellspacing=0 cellpadding=0>
        <tr><td></td><td></td><td></td></tr>
        <tr><td></td><td></td><td></td></tr>
</table>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<img id='img' src="http://img.jb51.NET/online/pintu/dog.jpg" style="" /><br>
轉(zhuǎn)載請注明來自<a href="http://blog.csdn.NET/sunxing007">http://blog.csdn.NET/sunxing007</a><br>
</body>
</html>
<script>
//ie7以下默認(rèn)不緩存背景圖像,造成延遲和閃爍,修正此bug.
//(csdn網(wǎng)友wtcsy提供http://blog.csdn.NET/wtcsy/)
try{
document.execCommand("BackgroundImageCache", false, true);
}catch(e){alert(e)};
    //輔助函數(shù)
    function $(id){return document.getElementById(id)};
    /*************************************************
    * js拼圖游戲 v1.6
    * 作者 sunxing007
    * 轉(zhuǎn)載請注明來自http://blog.csdn.NET/sunxing007
    **************************************************/
    var PicGame = {
            //行數(shù)
            x: 3,
            //列數(shù)
            y: 3,
            //圖片源
            img: $('img'),
            //單元格高度
            cellHeight: 0,
            //單元格寬度
            cellWidth: 0,
            //本游戲最主要的對象:表格,每個td對應(yīng)著一個可以移動的小格子
            board: $('board'),
            //初始函數(shù)
            init: function(){
                        //確定拼圖游戲的行數(shù)和列數(shù)
                        this.x = $('lines').value>1?$('lines').value : 3;
                        this.y = $('cols').value>1?$('cols').value : 3;
                        //刪除board原有的行
                        while(this.board.rows.length>0){
                                this.board.deleteRow(0);
                        }
                        //按照行數(shù)和列數(shù)重新構(gòu)造board
                        for(var i=0; i<this.x; i++){
                                var tr = this.board.insertRow(-1);
                                for(var j=0; j<this.y; j++){
                                        var td=tr.insertCell(-1);
                                }
                        }
                        //計(jì)算單元格高度和寬度
                        this.cellHeight = this.img.height/this.x;
                        this.cellWidth = this.img.width/this.y;
                        //獲取所有的td
                        var tds = this.board.getElementsByTagName('td');
                        //對每個td, 設(shè)置樣式
                     for(var i=0; i<tds.length-1; i++){
                             tds[i].style.width = this.cellWidth;
                             tds[i].style.height = this.cellHeight;
                             tds[i].style.background = "url(" + this.img.src + ")";
                             tds[i].style.border = "solid #ccc 1px";
                             var currLine = parseInt(i/this.y);
                             var currCol = i%this.y;
                             //這里最重要,計(jì)算每個單元格的背景圖的位置,使他們看起來像一幅圖像
                             tds[i].style.backgroundPositionX = -this.cellWidth * currCol;
                             tds[i].style.backgroundPositionY = -this.cellHeight * currLine;
                     }
                     /** begin: 打亂排序*******************/
                     /**
                     *    打亂排序的算法是這樣的:比如我當(dāng)前是3*3布局,
                     * 則我為每一個td產(chǎn)生一個目標(biāo)位置,這些目標(biāo)位置小于9且各不相同,
                     * 然后把它們替換到那個地方。
                     **/

                     //目標(biāo)位置序列
                     var index = [];
                     index[0] = Math.floor(Math.random()*(this.x*this.y));
                     while(index.length<(this.x*this.y)){
                     var num = Math.floor(Math.random()*(this.x*this.y));
                     for(var i=0; i<index.length; i++){
                     if(index[i]==num){
                     break;
                     }
                     }
                     if(i==index.length){
                     index[index.length] = num;
                     }
                     //window.status = index;
                     }
                     var cloNETds = [];
                     //把每個td克隆, 然后依據(jù)目標(biāo)位置序列index,替換到目標(biāo)位置
                     for(var i=0; i<tds.length; i++){
                     cloNETds.push(tds[i].cloneNode(true));
                     }
                     for(var i=0; i<index.length; i++){
                     tds[i].parentNode.replaceChild(cloNETds[index[i]],tds[i]);
                     }
                     /** end: 打亂排序*********************/

                     //為每個td添加onclick事件。
                     tds = this.board.getElementsByTagName('td');
                     for(var i=0; i<tds.length; i++){
                             tds[i].onclick = function(){
                             //被點(diǎn)擊對象的坐標(biāo)
                         var row = this.parentNode.rowIndex;
                         var col = this.cellIndex;
                         //window.status = "row:" + row + " col:" + col;
                         //空白方塊的坐標(biāo)
                         var rowBlank = 0;
                         var colBlank = 0;
                         //reachable表示當(dāng)前被點(diǎn)擊的方塊是否可移動
                         var reachable = false;
                         if(row+1<PicGame.x && PicGame.board.rows[row+1].cells[col].style.background == ''){
                         rowBlank = row + 1;
                         colBlank = col;
                         reachable = true;
                         //window.status +=" reachable! rowBlank: " + rowBlank + " colBlank:" + colBlank;
                         }
                         else if(row-1>=0 && PicGame.board.rows[row-1].cells[col].style.background == ''){
                         rowBlank = row - 1;
                         colBlank = col;
                         reachable = true;
                         //window.status +=" reachable! rowBlank: " + rowBlank + " colBlank:" + colBlank;
                         }
                         else if(col+1<PicGame.y && PicGame.board.rows[row].cells[col+1].style.background == ''){
                         rowBlank = row;
                         colBlank = col + 1;
                         reachable = true;
                         //window.status +=" reachable! rowBlank: " + rowBlank + " colBlank:" + colBlank;
                         }
                         else if(col-1>=0 && PicGame.board.rows[row].cells[col-1].style.background == ''){
                         rowBlank = row;
                         colBlank = col - 1;
                         reachable = true;
                         //window.status +=" reachable! rowBlank: " + rowBlank + " colBlank:" + colBlank;
                         }
                         else{
                         //window.status +=" unreachable!";
                         reachable = false;
                         }
                         //如果可移動,則把當(dāng)前方塊和空白方塊交換
                         if(reachable){
                         var tmpBlankNode = PicGame.board.rows[rowBlank].cells[colBlank].cloneNode(true);
                         //需要注意的是克隆對象丟失了onclick方法,所以要補(bǔ)上
                         tmpBlankNode.onclick = arguments.callee;
                         var tmpCurrNode = PicGame.board.rows[row].cells[col].cloneNode(true);
                         tmpCurrNode.onclick = arguments.callee;
                         PicGame.board.rows[rowBlank].cells[colBlank].parentNode.replaceChild(tmpCurrNode,PicGame.board.rows[rowBlank].cells[colBlank]);
                         PicGame.board.rows[row].cells[col].parentNode.replaceChild(tmpBlankNode, PicGame.board.rows[row].cells[col]);
                         }
                         }
                     }
            }
    };
PicGame.init();
$('start').onclick = function(){
        PicGame.init();
}
</script>

JavaScript技術(shù)JS 拼圖游戲 面向?qū)ο螅⑨屚暾?/a>,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产国拍亚洲精品av麻豆 | 亚洲欧美韩国综合色 | YIN荡的老师系列第6部分视频 | 污污又黄又爽免费的网站 | 亚洲成人一区二区 | 秋霞电影网午夜鲁丝片无码 | 九九热这里都是精品 | 亚洲日本va中文字幕久久 | 久久精品国产亚洲AV久五月天 | 媚药调教被撑到合不拢h | 一边摸一边桶一边脱免费 | 91区国产福利在线观看午夜 | 黄色片软件大全 | 超碰免费视频公开观看 | 欧美多毛的大隂道 | 久久精品美女久久 | 自拍黄色片 | 在线观看国产高清免费不卡 | 精品四虎国产在免费观看 | 一本到2019线观看 | 国产国拍亚洲精品av麻豆 | 99久久久免费精品国产 | 久草在线精彩免费视频 | 99久久精品互换人妻AV | 泷泽萝拉首部av | 国产亚洲日韩欧美视频 | 国产精品一区二区免费 | 女人十八毛片水真多啊 | 亚洲 日韩 国产 中文视频 | 92午夜理论第1000集 app | 国产精品福利片 | 精品无码三级在线观看视频 | 国产精品亚洲国产三区 | FERRCHINA内入内射 | 久久婷婷丁香五月色综合啪免费 | 麻豆区蜜芽区 | 国产午夜人成在线视频麻豆 | 高干紧射H后入 | 久久黄视频 | 黄得让人湿的片段 | 久久综合久综合久久鬼色 |