一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

JS 拼圖游戲 面向對象,注釋完整。

在線演示 http://img.jb51.NET/online/pintu/pintu.htm
復制代碼 代碼如下:
<html>
<head>
<title>JS拼圖游戲</title>
<style>
    body{
        font-size:9pt;
    }
table{
border-collapse: collapse;
}
input{
    width:20px;
}
</style>
</head>
<body>
    JS原創作品:JS拼圖游戲<br>
    注釋完整, 面向對象<br>
    轉載請注明來自<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>
轉載請注明來自<a href="http://blog.csdn.NET/sunxing007">http://blog.csdn.NET/sunxing007</a><br>
</body>
</html>
<script>
//ie7以下默認不緩存背景圖像,造成延遲和閃爍,修正此bug.
//(csdn網友wtcsy提供http://blog.csdn.NET/wtcsy/)
try{
document.execCommand("BackgroundImageCache", false, true);
}catch(e){alert(e)};
    //輔助函數
    function $(id){return document.getElementById(id)};
    /*************************************************
    * js拼圖游戲 v1.6
    * 作者 sunxing007
    * 轉載請注明來自http://blog.csdn.NET/sunxing007
    **************************************************/
    var PicGame = {
            //行數
            x: 3,
            //列數
            y: 3,
            //圖片源
            img: $('img'),
            //單元格高度
            cellHeight: 0,
            //單元格寬度
            cellWidth: 0,
            //本游戲最主要的對象:表格,每個td對應著一個可以移動的小格子
            board: $('board'),
            //初始函數
            init: function(){
                        //確定拼圖游戲的行數和列數
                        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);
                        }
                        //按照行數和列數重新構造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);
                                }
                        }
                        //計算單元格高度和寬度
                        this.cellHeight = this.img.height/this.x;
                        this.cellWidth = this.img.width/this.y;
                        //獲取所有的td
                        var tds = this.board.getElementsByTagName('td');
                        //對每個td, 設置樣式
                     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;
                             //這里最重要,計算每個單元格的背景圖的位置,使他們看起來像一幅圖像
                             tds[i].style.backgroundPositionX = -this.cellWidth * currCol;
                             tds[i].style.backgroundPositionY = -this.cellHeight * currLine;
                     }
                     /** begin: 打亂排序*******************/
                     /**
                     *    打亂排序的算法是這樣的:比如我當前是3*3布局,
                     * 則我為每一個td產生一個目標位置,這些目標位置小于9且各不相同,
                     * 然后把它們替換到那個地方。
                     **/

                     //目標位置序列
                     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克隆, 然后依據目標位置序列index,替換到目標位置
                     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(){
                             //被點擊對象的坐標
                         var row = this.parentNode.rowIndex;
                         var col = this.cellIndex;
                         //window.status = "row:" + row + " col:" + col;
                         //空白方塊的坐標
                         var rowBlank = 0;
                         var colBlank = 0;
                         //reachable表示當前被點擊的方塊是否可移動
                         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;
                         }
                         //如果可移動,則把當前方塊和空白方塊交換
                         if(reachable){
                         var tmpBlankNode = PicGame.board.rows[rowBlank].cells[colBlank].cloneNode(true);
                         //需要注意的是克隆對象丟失了onclick方法,所以要補上
                         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技術JS 拼圖游戲 面向對象,注釋完整。,轉載需保留來源!

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

主站蜘蛛池模板: 亚洲国产一区在线二区三区 | 男人精品一线视频在线观看 | 午夜一级在线 | 亚洲午夜国产精品无卡 | 欧美zooz人禽交免费观看 | 久久亚洲精品中文字幕二区 | 五月婷在线观看 | 激情图片激情文学 | 伊人三区 | 日日噜噜夜夜狠狠tv视频免费 | 色图综合| 国内精品手机在线观看视频 | 久久久久久久免费 | 男人精品网站一区二区三区 | 日韩成人一级 | 日韩美女色高清在线看 | 欧美福利第一页 | 亚洲国产一区二区三区在线观看 | 女人18毛片久久鬼色 | 欧美1314www伊人久久香网 | 日韩一二三 | 影音先锋国产资源 | 久久大香萑太香蕉综合网 | 国产精品日韩欧美一区二区三区 | 精品伊人久久久久7777人 | 国产成人精品亚洲77美色 | 国产美女久久久久 | 美女毛片视频 | 91短视频在线 | 成人国产在线24小时播放视频 | 成人午夜精品视频在线观看 | 免费视频91| 第一页亚洲 | 91成人在线观看 | 色综合综合在线 | 精品国产亚洲一区二区三区 | 国产高清精品久久久久久久 | 国产精品久久国产精麻豆99网站 | 91精选在线观看 | 99综合色| 色综合色狠狠天天综合色 |