推箱子

效果图

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">步数:0</div>
    </div>
    <script type="text/javascript">
        // 获取分数节点
        var jsScore = document.getElementById("score");
        // 获取屏幕节点
        var jsScreen = document.getElementById("screen");
        // 获取小人节点
        var jsGirl = document.getElementById("girl");
        var arrBrick = new Array();
        var arrBox = new Array();
        var arrRedPoint = new Array();
        console.log(arrBox);
        // 创建小人对象
        function Girl(){
            this.div = jsGirl;
        }
        var girl = new Girl(); 

        // 让小人移动
        var speed = 29;
        var count = 0;
        var score = 0;
        document.onkeydown = function(e){
            var evt = e || window.event;
            // 将分数写进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";
                    }
                }
                // 检测小木箱撞左边墙
                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 +arrBrick[j].div.offsetWidth + 1)+ "px";
                            // 小木箱撞到墙后人停下
                            jsGirl.style.left = (arrBrick[j].div.offsetLeft +arrBrick[j].div.offsetWidth + 3 + 27)+ "px";
                        }
                    }
                }

                // 检测人撞墙后停下
                for(var j in arrBrick){
                    if (arrBrick[j].crashExamine(girl)) {
                        // 小人撞到墙后木箱停下来
                        girl.div.style.left = (arrBrick[j].div.offsetLeft +arrBrick[j].div.offsetWidth + 1)+ "px";
                    }
                }

                // 检测箱子撞到箱子后停下

                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";
                    }
                }

                // 检测小木箱撞上边墙
                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";
                        }
                    }
                }

                // 检测人撞墙后停下
                for(var j in arrBrick){
                    if (arrBrick[j].crashExamine(girl)) {
                        // 小人撞到墙后木箱停下来
                        girl.div.style.top = (arrBrick[j].div.offsetTop + arrBrick[j].div.offsetHeight + 1)+ "px";
                    }
                }
                // 检测箱子撞到箱子后停下
                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";
                    }
                }
                // 检测小木箱撞右边墙
                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";
                        }
                    }
                }

                    // 检测人撞墙后停下
                for(var j in arrBrick){
                    if (arrBrick[j].crashExamine(girl)) {
                        // 小人撞到墙后木箱停下来
                        girl.div.style.left = (arrBrick[j].div.offsetLeft - 27 -1)+ "px";
                    }
                }

                // 检测箱子撞到箱子后停下
                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";
                    }
                }
                // 检测小木箱撞下边墙
                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)) {
                        // 小人撞到墙后木箱停下来
                        girl.div.style.top = (arrBrick[j].div.offsetTop - 27 -1)+ "px";
                    }
                }

                // 检测箱子撞到箱子后停下
                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;
                // =======================================下=============================================
                
            }
            // 让小人动起来
            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);
            // 碰撞检测
            jsScore.innerHTML = "步数:" + score;

            // =====================检测小球与箱子重合==========================================
            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");
                        }
                    };
                };
            };
        }
        // 创建砖的构造函数
        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;
                }
            },
        }
        // 创建箱子函数
        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");
        };
        // 创建红点函数
        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");

        // 将所有对象放进数组里面
        
        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)


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

        // 箱子对象
        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);

        // 红点对象
        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);

        // 碰撞检测
    </script>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,016评论 4 62
  • 念念不忘必有回应
    连家骏阅读 349评论 0 0
  • 最近在亚马逊购书的时候,想到了小狗钱钱这本书。小时候曾经买过它的盗版——大概是盗版,因为书的质量很是堪忧——获得过...
    蔑视魔山阅读 270评论 0 0
  • 1、安装 Git 客户端 下载地址: https://git-scm.com/downloads 2、占山为王 ...
    小学生的博客阅读 394评论 0 0
  • 文/976同学 远道隔江汉,孤舟无岁年。我们在江河两岸,抬头望着天空的时候,明白彼此在欣赏着同一...
    976同学阅读 335评论 2 5