推箱子

效果圖

Paste_Image.png

資料圖片

player10.png

player11.png

player12.png

wall.png

background.png

box.png

dest.png

player1.png

player2.png

player3.png

player4.png

player5.png

player6.png

player7.png

player8.png

player9.png

代碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0 auto;
            padding: 0;
        }
        #screen{
            width: 290px;
            height: 290px;
            background: url(background.png);
            border: 3px solid black;
            position: relative;
        }
        #girl{
            width: 27px;
            height: 27px;
            background: url("player1.png");
            position: absolute;
            -webkit-background-size: 27px;
            background-size: 27px;
            left: 88px;
            top: 117px;
        }
        #score{
            width: 100px;
            height: 20px;
            /*background-color: blue;*/
            color: red;
            position: absolute;
            right: 0px;
            top: 20px;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div id="screen">   
        <div id="girl"></div>
        <div id="score">步數(shù):0</div>
    </div>
    <script type="text/javascript">
        // 獲取分?jǐn)?shù)節(jié)點(diǎn)
        var jsScore = document.getElementById("score");
        // 獲取屏幕節(jié)點(diǎn)
        var jsScreen = document.getElementById("screen");
        // 獲取小人節(jié)點(diǎn)
        var jsGirl = document.getElementById("girl");
        var arrBrick = new Array();
        var arrBox = new Array();
        var arrRedPoint = new Array();
        console.log(arrBox);
        // 創(chuàng)建小人對(duì)象
        function Girl(){
            this.div = jsGirl;
        }
        var girl = new Girl(); 

        // 讓小人移動(dòng)
        var speed = 29;
        var count = 0;
        var score = 0;
        document.onkeydown = function(e){
            var evt = e || window.event;
            // 將分?jǐn)?shù)寫(xiě)進(jìn)HTML
            
            
            switch(evt.keyCode){
                case 37:
                jsGirl.style.left = jsGirl.offsetLeft - speed + "px";
                for(var i in arrBox){   
                    if (brick1.crashExamine.call(arrBox[i],girl)) {
                        arrBox[i].div.style.left  = (girl.div.offsetLeft -arrBox[i].div.offsetWidth - 2) + "px";
                    }
                }
                // 檢測(cè)小木箱撞左邊墻
                for(var j in arrBrick ){
                    for(var k in arrBox){
                        if (arrBrick[j].crashExamine(arrBox[k])) {
                            // 小木箱撞到墻后木箱停下來(lái)
                            arrBox[k].div.style.left = (arrBrick[j].div.offsetLeft +arrBrick[j].div.offsetWidth + 1)+ "px";
                            // 小木箱撞到墻后人停下
                            jsGirl.style.left = (arrBrick[j].div.offsetLeft +arrBrick[j].div.offsetWidth + 3 + 27)+ "px";
                        }
                    }
                }

                // 檢測(cè)人撞墻后停下
                for(var j in arrBrick){
                    if (arrBrick[j].crashExamine(girl)) {
                        // 小人撞到墻后木箱停下來(lái)
                        girl.div.style.left = (arrBrick[j].div.offsetLeft +arrBrick[j].div.offsetWidth + 1)+ "px";
                    }
                }

                // 檢測(cè)箱子撞到箱子后停下

                for(var j in arrBox ){
                    for(var k in arrBox){
                        if (brick1.crashExamine.call(arrBox[j],arrBox[k])&&(j != k)) {
                            arrBox[j].div.style.left = (arrBox[k].div.offsetLeft + 27 + 2)+ "px";
                            girl.div.style.left = (arrBox[k].div.offsetLeft + 27 + 27 + 4)+ "px";
                        }
                    }
                }

                count = 1;
                score++;
                break;
                // =====================左==========================================
                case 38:
                jsGirl.style.top = jsGirl.offsetTop - speed + "px";
                count = 2;
                for(var i in arrBox){   
                    if (brick1.crashExamine.call(arrBox[i],girl)) {
                        arrBox[i].div.style.top  = (girl.div.offsetTop -arrBox[i].div.offsetHeight - 2) + "px";
                    }
                }

                // 檢測(cè)小木箱撞上邊墻
                for(var j in arrBrick ){
                    for(var k in arrBox){
                        if (arrBrick[j].crashExamine(arrBox[k])) {
                            arrBox[k].div.style.top = (arrBrick[j].div.offsetTop + arrBrick[j].div.offsetHeight + 1)+ "px";
                            jsGirl.style.top = (arrBrick[j].div.offsetTop +arrBrick[j].div.offsetHeight + 3 + 27)+ "px";
                        }
                    }
                }

                // 檢測(cè)人撞墻后停下
                for(var j in arrBrick){
                    if (arrBrick[j].crashExamine(girl)) {
                        // 小人撞到墻后木箱停下來(lái)
                        girl.div.style.top = (arrBrick[j].div.offsetTop + arrBrick[j].div.offsetHeight + 1)+ "px";
                    }
                }
                // 檢測(cè)箱子撞到箱子后停下
                for(var j in arrBox ){
                    for(var k in arrBox){
                        if (brick1.crashExamine.call(arrBox[j],arrBox[k])&&(j != k)) {
                            arrBox[j].div.style.top = (arrBox[k].div.offsetTop + 27 + 2)+ "px";
                            girl.div.style.top = (arrBox[k].div.offsetTop + 27 + 27 +4)+ "px";
                        }
                    }
                }
                score++;
                break;
                // ===================================上===================================
                case 39:
                jsGirl.style.left = jsGirl.offsetLeft + speed + "px";
                count = 3;
                for(var i in arrBox){   
                    if (brick1.crashExamine.call(arrBox[i],girl)) {
                        arrBox[i].div.style.left  = (girl.div.offsetLeft + girl.div.offsetWidth + 2) + "px";
                    }
                }
                // 檢測(cè)小木箱撞右邊墻
                for(var j in arrBrick ){
                    for(var k in arrBox){
                        if (arrBrick[j].crashExamine(arrBox[k])) {
                            arrBox[k].div.style.left = (arrBrick[j].div.offsetLeft - 27 -1)+ "px";
                            jsGirl.style.left = (arrBrick[j].div.offsetLeft  - 3 - 27 - 27)+ "px";
                        }
                    }
                }

                    // 檢測(cè)人撞墻后停下
                for(var j in arrBrick){
                    if (arrBrick[j].crashExamine(girl)) {
                        // 小人撞到墻后木箱停下來(lái)
                        girl.div.style.left = (arrBrick[j].div.offsetLeft - 27 -1)+ "px";
                    }
                }

                // 檢測(cè)箱子撞到箱子后停下
                for(var j in arrBox ){
                    for(var k in arrBox){
                        if (brick1.crashExamine.call(arrBox[j],arrBox[k])&&(j != k)) {
                            arrBox[j].div.style.left = (arrBox[k].div.offsetLeft - 27 - 2)+ "px";
                            // jsGirl.style.left = (arrBox[k].div.offsetLeft - 27 - 3 - 27)+ "px";
                            girl.div.style.left = (arrBox[k].div.offsetLeft - 27 - 27 -4)+ "px";
                        }
                    }
                }
                score++;
                break;
                // =====================================右=========================================
                case 40:
                jsGirl.style.top = jsGirl.offsetTop + speed + "px";
                count = 4;
                for(var i in arrBox){   
                    if (brick1.crashExamine.call(arrBox[i],girl)) {
                        arrBox[i].div.style.top  = (girl.div.offsetTop + girl.div.offsetHeight + 2) + "px";
                    }
                }
                // 檢測(cè)小木箱撞下邊墻
                for(var j in arrBrick ){
                    for(var k in arrBox){
                        if (arrBrick[j].crashExamine(arrBox[k])) {
                            arrBox[k].div.style.top = (arrBrick[j].div.offsetTop - 27 -1)+ "px";
                            jsGirl.style.top = (arrBrick[j].div.offsetTop   - 27 - 27 - 3) + "px";
                        }
                    }
                }

                for(var j in arrBrick){
                    if (arrBrick[j].crashExamine(girl)) {
                        // 小人撞到墻后木箱停下來(lái)
                        girl.div.style.top = (arrBrick[j].div.offsetTop - 27 -1)+ "px";
                    }
                }

                // 檢測(cè)箱子撞到箱子后停下
                for(var j in arrBox ){
                    for(var k in arrBox){
                        if (brick1.crashExamine.call(arrBox[j],arrBox[k])&&(j != k)) {
                            arrBox[j].div.style.top = (arrBox[k].div.offsetTop - 27 - 2)+ "px";
                            girl.div.style.top = (arrBox[k].div.offsetTop - 27 - 27 -4)+ "px";
                        }
                    }
                }
                score++;
                break;
                // =======================================下=============================================
                
            }
            // 讓小人動(dòng)起來(lái)
            var picNum = 1;
            var timer = setInterval(function(){
                if (count == 4) {   
                    jsGirl.style.background = "url(player" + picNum + ".png)";
                    jsGirl.style.backgroundSize = "27px 27px";
                }
                if (count == 1) {
                    jsGirl.style.background = "url(player" + (picNum + 3) + ".png)";
                    jsGirl.style.backgroundSize = "27px 27px";
                }
                if (count == 2) {
                    jsGirl.style.background = "url(player" + (picNum + 6) + ".png)";
                    jsGirl.style.backgroundSize = "27px 27px";
                }
                if (count == 3) {
                    jsGirl.style.background = "url(player" + (picNum + 9) + ".png)";
                    jsGirl.style.backgroundSize = "27px 27px";
                }
                if (picNum == 3) {
                    picNum = 1;
                }
                picNum++;

            }, 67);
            // 碰撞檢測(cè)
            jsScore.innerHTML = "步數(shù):" + score;

            // =====================檢測(cè)小球與箱子重合==========================================
            for(var i in arrRedPoint){
                arrRedPoint[i]["checked"] = false;
                for(var j in arrBox){
                    if(brick1.crashExamine.call(arrRedPoint[i],arrBox[j])){
                        arrRedPoint[i]["checked"] = true;
                        arrRedPoint[i].div.style.display = "none";
                        if (arrRedPoint[0]["checked"]&&arrRedPoint[1]["checked"]&&arrRedPoint[2]["checked"]&&arrRedPoint[3]["checked"]) {
                            console.log("gameover");
                        }
                    };
                };
            };
        }
        // 創(chuàng)建磚的構(gòu)造函數(shù)
        function Brick(top,left){
            this.width = "29px";
            this.height = "29px";
            this.background = "url(wall.png)"
            
            this.top = top;
            this.left = left;
            this.div = document.createElement("div");
        };
        Brick.prototype = {
            position : "absolute",
            initPositin : function(){
                this.div.style.top = this.top;
                this.div.style.left = this.left;
                this.div.style.width = this.width;
                this.div.style.height = this.height;
                this.div.style.background = this.background;
                this.div.style.position = this.position;
                jsScreen.appendChild(this.div);
            },
            crashExamine: function(obj2) {
                var obj1Left = this.div.offsetLeft;
                var obj1Width = this.div.offsetLeft + this.div.offsetWidth;
                var obj1Top = this.div.offsetTop;
                var obj1Height = this.div.offsetTop + this.div.offsetHeight;

                var obj2Left = obj2.div.offsetLeft;
                var obj2Width = obj2.div.offsetLeft + obj2.div.offsetWidth;
                var obj2Top = obj2.div.offsetTop;
                var obj2Height = obj2.div.offsetTop + obj2.div.offsetHeight;


                if ( !(obj1Left > obj2Width || obj1Width < obj2Left || obj1Top > obj2Height || obj1Height < obj2Top) ) {
                    return true;
                }
                return false;
            },
            crashExamine1: function(obj2) {
                var obj1Left = this.div.offsetLeft;
                var obj1Width = this.div.offsetLeft + this.div.offsetWidth;
                var obj1Top = this.div.offsetTop;
                var obj1Height = this.div.offsetTop + this.div.offsetHeight;

                var obj2Left = obj2.div.offsetLeft;
                var obj2Width = obj2.div.offsetLeft + obj2.div.offsetWidth;
                var obj2Top = obj2.div.offsetTop;
                var obj2Height = obj2.div.offsetTop + obj2.div.offsetHeight;

                if ( obj1Left < obj2Width) {
                    return 1;
                }
                if (obj1Width < obj2Left) {
                    return 2;
                }
                if (obj1Top > obj2Height) {
                    return 3;
                }
                if (obj1Height < obj2Top) {
                    return 4;
                }
            },
        }
        // 創(chuàng)建箱子函數(shù)
        function Box(top,left){
            this.width = "27px";
            this.height = "27px";
            this.background = "url(box.png)"
            this.position = "absolute";
            this.top = top;
            this.left = left;
            this.div = document.createElement("div");
        };
        // 創(chuàng)建紅點(diǎn)函數(shù)
        function RedPoint(top,left){
            this.width = "19px";
            this.height = "20px";
            this.background = "url(dest.png)"
            this.position = "absolute";
            this.top = top;
            this.left = left;
            this.div = document.createElement("div");
        };
        

        // 第一排
        var brick1 = new Brick("0px","87px");
        var brick2 = new Brick("0px","116px");
        var brick3 = new Brick("0px","145px");
        // 第二排
        var brick4 = new Brick("29px","58px");
        var brick5 = new Brick("29px","145px");
        // 第san排
        var brick6 = new Brick("58px","58px");
        var brick7 = new Brick("58px","145px");
        // 第四排
        var brick8 = new Brick("87px","29px");
        var brick9 = new Brick("87px","58px");
        var brick10 = new Brick("87px","145px");
        var brick11 = new Brick("87px","174px");
        var brick12 = new Brick("87px","203px");
        var brick13 = new Brick("87px","232px");
        var brick14 = new Brick("87px","261px");
        // 第五排
        var brick15 = new Brick("116px","0px");
        var brick16 = new Brick("116px","261px");
        // 第六排
        var brick17 = new Brick("145px","0px");
        var brick18 = new Brick("145px","58px");
        var brick19 = new Brick("145px","203px");
        var brick20 = new Brick("145px","232px");
        var brick21 = new Brick("145px","261px");
        // 第七排
        var brick22 = new Brick("174px","0px");
        var brick23 = new Brick("174px","116px");
        var brick24 = new Brick("174px","203px");
        // 第八排
        var brick25 = new Brick("203px","0px");
        var brick26 = new Brick("203px","29px");
        var brick27 = new Brick("203px","116px");
        var brick28 = new Brick("203px","203px");
        // 第九排
        var brick29 = new Brick("232px","58px");
        var brick30 = new Brick("232px","87px");
        var brick31 = new Brick("232px","116px");
        var brick32 = new Brick("232px","203px");
        // 第十排
        var brick33 = new Brick("261px","116px");
        var brick34 = new Brick("261px","145px");
        var brick35 = new Brick("261px","174px");
        var brick36 = new Brick("261px","203px");

        // 將所有對(duì)象放進(jìn)數(shù)組里面
        
        arrBrick.push(brick1,brick2,brick3,brick4,brick5,brick6,brick7,brick8,brick9,brick10,brick11,brick12,brick13,brick14,brick15,brick16,brick17,brick18,brick19,brick20,brick21,brick22,brick23,brick24,brick25,brick26,brick27,brick28,brick29,brick30,brick31,brick32,brick33,brick34,brick35,brick36)


        // 遍歷所有對(duì)象,對(duì)其使用initPositin方法
        for(var i in arrBrick){
            arrBrick[i].initPositin();
        };

        // 箱子對(duì)象
        var box1 = new Box("88px","88px");
        var box2 = new Box("117px","146px");
        var box3 = new Box("175px","88px");
        var box4 = new Box("175px","146px");

        arrBox.push(box1,box2,box3,box4);
        //box1借用brick1的initPosition方法
        brick1.initPositin.call(box1);
        brick1.initPositin.call(box2);
        brick1.initPositin.call(box3);
        brick1.initPositin.call(box4);

        // 紅點(diǎn)對(duì)象
        var redPoint1 = new RedPoint("35px","122px");
        var redPoint2 = new RedPoint("120px","235px");
        var redPoint3 = new RedPoint("180px","33px");
        var redPoint4 = new RedPoint("237px","180px");

        arrRedPoint.push(redPoint1,redPoint2,redPoint3,redPoint4);
        //redPoint1借用brick1的initPosition方法
        brick1.initPositin.call(redPoint1);
        brick1.initPositin.call(redPoint2);
        brick1.initPositin.call(redPoint3);
        brick1.initPositin.call(redPoint4);

        // 碰撞檢測(cè)
    </script>
</body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末需频,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌芍阎,老刑警劉巖永品,帶你破解...
    沈念sama閱讀 207,248評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件脱篙,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡除破,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,681評(píng)論 2 381
  • 文/潘曉璐 我一進(jìn)店門(mén)癌别,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)皂岔,“玉大人,你說(shuō)我怎么就攤上這事展姐≡甓猓” “怎么了剖毯?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,443評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀(guān)的道長(zhǎng)教馆。 經(jīng)常有香客問(wèn)我逊谋,道長(zhǎng),這世上最難降的妖魔是什么土铺? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,475評(píng)論 1 279
  • 正文 為了忘掉前任胶滋,我火速辦了婚禮,結(jié)果婚禮上悲敷,老公的妹妹穿的比我還像新娘究恤。我一直安慰自己,他們只是感情好后德,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,458評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布部宿。 她就那樣靜靜地躺著,像睡著了一般瓢湃。 火紅的嫁衣襯著肌膚如雪理张。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,185評(píng)論 1 284
  • 那天绵患,我揣著相機(jī)與錄音雾叭,去河邊找鬼。 笑死落蝙,一個(gè)胖子當(dāng)著我的面吹牛织狐,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播掘殴,決...
    沈念sama閱讀 38,451評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼赚瘦,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了奏寨?” 一聲冷哼從身側(cè)響起起意,我...
    開(kāi)封第一講書(shū)人閱讀 37,112評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎病瞳,沒(méi)想到半個(gè)月后揽咕,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,609評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡套菜,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,083評(píng)論 2 325
  • 正文 我和宋清朗相戀三年亲善,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片逗柴。...
    茶點(diǎn)故事閱讀 38,163評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蛹头,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情渣蜗,我是刑警寧澤屠尊,帶...
    沈念sama閱讀 33,803評(píng)論 4 323
  • 正文 年R本政府宣布,位于F島的核電站耕拷,受9級(jí)特大地震影響讼昆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜骚烧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,357評(píng)論 3 307
  • 文/蒙蒙 一浸赫、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧赃绊,春花似錦既峡、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,357評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至么夫,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間肤视,已是汗流浹背档痪。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,590評(píng)論 1 261
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留邢滑,地道東北人腐螟。 一個(gè)月前我還...
    沈念sama閱讀 45,636評(píng)論 2 355
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像困后,于是被迫代替她去往敵國(guó)和親乐纸。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,925評(píng)論 2 344

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)摇予、插件汽绢、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,033評(píng)論 4 62
  • 念念不忘必有回應(yīng)
    連家駿閱讀 349評(píng)論 0 0
  • 最近在亞馬遜購(gòu)書(shū)的時(shí)候,想到了小狗錢(qián)錢(qián)這本書(shū)侧戴。小時(shí)候曾經(jīng)買(mǎi)過(guò)它的盜版——大概是盜版宁昭,因?yàn)闀?shū)的質(zhì)量很是堪憂(yōu)——獲得過(guò)...
    蔑視魔山閱讀 272評(píng)論 0 0
  • 1、安裝 Git 客戶(hù)端 下載地址: https://git-scm.com/downloads 2酗宋、占山為王 ...
    小學(xué)生的博客閱讀 395評(píng)論 0 0
  • 文/976同學(xué) 遠(yuǎn)道隔江漢积仗,孤舟無(wú)歲年。我們?cè)诮觾砂锻擅ǎь^望著天空的時(shí)候寂曹,明白彼此在欣賞著同一...
    976同學(xué)閱讀 335評(píng)論 2 5