1118-js-笔记

练习笔记:贪吃蛇游戏案例

html代码:

<div class="map" id="map"></div>

css代码:

<style>
    *{
        margin: 0;
        padding: 0;
    }
    .map{
        margin: 100px auto;
        width: 800px;
        height: 600px;
        background-color: #ccc;
        position: relative;
        overflow: hidden;
    }
</style>

js代码:

<script src="../../function/function.js"></script>
    <script>
        var num=0;
    (function(){
        var elements=[];//用来保存每个小方块食物的
        
        //食物就是一个对象,有宽,高,颜色,横纵坐标,先定义构造函数,再创建对象
        function Food(x,y,width,height,color){
            this.x=x||0;
            this.y=y||0;
            this.width=width||20;
            this.height=height||20;
            this.color=color||"green";
        }
        //为原型添加初始化的方法,(在页面上显示这个食物)
        //因为食物在地图上显示,
        Food.prototype.init=function(map){
            remove();
            //创建div
            var div=document.createElement("div");
            //把div加到map中
            map.appendChild(div);
            //设置div样式
            div.style.width=this.width+"px";
            div.style.height=this.height+"px";
            div.style.backgroundColor=this.color;
            div.style.position="absolute";
            //横纵坐标随机产生
            this.x=parseInt(Math.random()*(map.offsetWidth/this.width))*this.width;
            this.y=parseInt(Math.random()*(map.offsetHeight/this.height))*this.height;
            div.style.left=this.x+"px";
            div.style.top=this.y+"px";
            //把div加到数组中element
            elements.push(div);
        }
            //私有函数删除食物
            function remove(){
                //element数组中有这个食物
                for(var i=0;i<elements.length;i++){
                    var ele=elements[i];
                    //找到这个子元素的父级元素,然后删除这个子元素
                    ele.parentNode.removeChild(ele);
                    //再次把elements中的子元素也删除
                    elements.splice(i,1);//从第i个元素删除1个 
                }
            }
        //把Food暴露给window,外部可以使用
        window.Food=Food;
    }());
    //小蛇的自调用函数
    (function(){
        var elements=[];//存放小蛇的每个身体部分
        //小蛇的构造函数
        function Snake(width,height,direction){
            //小蛇每个部分的宽
            this.width=width||20;
            this.height=height||20;
            //小蛇的身体
            this.body=[
                {x:3,y:2,color:"red",zIndex:9},//头
                {x:2,y:2,color:"orange",zIndex:2},//身体
                {x:1,y:2,color:"orange",zIndex:2}//身体
            ];
            //x小蛇的方向
            this.direction=direction||"right";
        }
        //为原型添加方法--小蛇初始化的方法
        Snake.prototype.init=function(map){
            remove();
            //循环遍历创建div
            for(var i=0;i<this.body.length;i++){
                console.log("ssssss");
                var Obj=this.body[i];
                //创建div
                var div=document.createElement("div");
                //把div加入到map中
                map.appendChild(div);
                //div 脱离文档流
                div.style.position="absolute";
                div.style.width=this.width+"px";
                div.style.height=this.height+"px";
                div.style.left=Obj.x*this.width+"px";
                div.style.top=Obj.y*this.height+"px";
                div.style.zIndex=Obj.zIndex;
                div.style.backgroundColor=Obj.color;
                elements.push(div);
            }
        };  
            //删除小蛇的私有函数 
            function remove() {
                var i = elements.length - 1;
                for(; i >= 0; i--){
                    //先从当前元素的子元素找到该元素的父级元素,然后再删除这个元素
                    var ele = elements[i];
                    ele.parentNode.removeChild(ele);
                    elements.splice(i,1);
                }
            }
        
        //为小蛇添加方法
        Snake.prototype.move=function(food,map){
                //改变小蛇身体的坐标
                var i=this.body.length-1;
                for(;i>0;i--){
                    this.body[i].x=this.body[i-1].x;
                    this.body[i].y=this.body[i-1].y;
                } 
                //判断方向改变小蛇的头
                // this.direction=tatt;
                switch(this.direction){
                    case "right":this.body[0].x+=1;break;
                    case "left" :this.body[0].x-=1;break;
                    case "top"  :this.body[0].y-=1;break;
                    case "bottom":this.body[0].y+=1;break;            
                } 
                //判断有没有吃到食物
                //小蛇头坐标和食物坐标一致
                var headX=(this.body[0].x)*this.width;
                var headY=(this.body[0].y)*this.height;
                //食物的横纵坐标
                var foodX=food.x;
                var foodY=food.y;
                console.log(headX+20+"---------------"+foodX);
                console.log(headY+"---------------"+foodY);
                if(headX==foodX&&headY==foodY){
                    //获取小蛇的最后的尾巴
                    var last=this.body[this.body.length-1];
                    this.body.push({x:last.x,y:last.y,color:last.color});
                    num++;
                    console.log(num);
                    //把食物删除重新初始化食物
                    food.init(map);
                    // console.log("sdsdsdsdseeeeeeeeeeeeeeeeeeeeeeds");
                    alert(num);
                }
            };
        //把Snake暴露给window,外部可以访问
        window.Snake=Snake;
    }());
    (function(){
        var that;
        function Game(map){
            this.food=new Food();//食物对象
            this.snake=new Snake();//小蛇对象
            this.map=map;//地图
            that =this; //保存当前的实例对象到that变量中
        }
        Game.prototype.init=function(){
            //初始化游戏
            //食物初始化
            this.food.init(this.map);
            this.snake.init(this.map);
            // setInterval(function(){
            //     that.snake.move(that.food,that.map);//小蛇移动
            //     that.snake.init(that.map);
            // },150);
            this.ranSnake(this.food,this.map);
            this.bindKeyValue();
        };
        Game.prototype.ranSnake=function(food,map){
            // clearInterval(timeId);
            //自动去移动
            var timeId=setInterval(function(){
                //此时this是window
                //移动小蛇
                this.snake.move(food,map);
                //初始化小蛇
                this.snake.init(map);
                //横纵坐标最大值
                var maxX=map.offsetWidth/this.snake.width;
                var maxY=map.offsetHeight/this.snake.height;
                //小蛇头的坐标
                var headX = this.snake.body[0].x;
                var headY = this.snake.body[0].y;
                //横坐标
                if(headX<0||headX>=maxX){
                    //撞墙了停止定时器
                    clearInterval(timeId);
                    alert("GAME OVER!");
                    clearInterval(timeId);
                }
                //纵坐标a
                if(headY<0||headY>=maxY){
                    //撞墙了停止定时器
                    clearInterval(timeId);
                    alert("GAME OVER!");
                }
            }.bind(that),150); //bind改变了this的指向
        };
        Game.prototype.bindKeyValue=function(){
            document.addEventListener("keydown",function(e){
                switch(e.keyCode){
                    case 87:this.snake.direction="top";break;
                    case 83:this.snake.direction="bottom";break;
                    case 65:this.snake.direction="left";break;
                    case 68:this.snake.direction="right";break;
                }
            }.bind(that),false)
            console.log(this.snake.direction);
        };
        window.Game=Game;
    }());
    //初始化游戏对象
    var gm= new Game(document.querySelector(".map"));
    //初始化游戏--开始游戏
    gm.init();
    //外部测试代码
    // var fd=new Food();
    // fd.init(document.querySelector(".map"));
    // console.log(fd.x+"-------------"+fd.y);
    // var sk=new Snake();
    // sk.init(document.querySelector(".map"));
    // // sk.move(fd,document.querySelector(".map"));
    // setInterval(function(){
    //     sk.move(fd,document.querySelector(".map"));
    //     sk.init(document.querySelector(".map"));
    // },150);

    </script>

图示:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值