贪吃蛇案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .map{
            position: relative;
            background-color: #666666;
            margin: 0 auto;
        }
    </style>

</head>
<body>

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

</script>
</body>
</html>
<script>
    var speed=0;
    var num=prompt("请选择难度:(1/2/3)");
    var map=document.getElementById("map");
    if(num==1){
        map.style.width=400+"px";
        map.style.height=300+"px";
        speed=200;
    }else if (num==2){
        map.style.width=600+"px";
        map.style.height=500+"px";
        speed=150
    }else {
        map.style.width=800+"px";
        map.style.height=700+"px";
        speed=100;
    }

    var map=document.querySelector(".map");
    //1.封装食物对象--->函数自调用
    ((function () {
        //存放小方块食物
        var elements=[];
        //2.写小方块的构造函数--->宽高颜色X\Y轴
        function Food(width,height,color,border,x,y) {
            this.width=width||20;
            this.height=height||20;
            this.color=color||"yellow";
            this.border=border||50;
            this.x=x||0;
            this.y=y||0;
            this.element=document.createElement("div");
        }
        Food.prototype.init=function (map) {

            var div = this.element;

            div.style.width=this.width+"px";
            div.style.height=this.height+"px";
            div.style.backgroundColor=this.color;
            div.style.borderRadius=this.border+"px";
            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放到数组里面
            elements.push(div);
            //把DIV添加到地图当中
            map.appendChild(div);
        }

        window.Food=Food;
    })());

    ((function () {
        //存放小蛇
        var elements=[];
        //写小蛇的构造函数
        function Snake(width,height,direction) {
            this.width=width||20;
            this.height=height||20;
            this.direction=direction||2;
            this.body=[
                {x:3, y:2, color:"green"},
                {x:2,y:2,color:"red"},
                {x:1,y:2,color:"red"}
            ]
        }
        //在原型中显示小蛇-->地图
        Snake.prototype.init=function (map) {
            remove();
            for (var i=0;i<this.body.length;i++){
                //存放小蛇的每一个部位
                var obj=this.body[i];
                //创建一个DIV
                var div=document.createElement("div");
                //把DIV添加到地图上
                map.appendChild(div);
                //设置样式
                div.style.position="absolute";
                div.style.width=this.width+"px";
                div.style.height=this.height+"px";
                //每个DIV的横纵坐标
                div.style.left = this.width*obj.x+"px";
                div.style.top = this.height*obj.y+"px";

                div.style.backgroundColor=obj.color;

                elements.push(div);

            }

        }

        //在原型中移动小蛇-->地图
        Snake.prototype.move=function(food,map){
            for (var i=this.body.length-1;i>0;i--){
                this.body[i].x=this.body[i-1].x;
                this.body[i].y=this.body[i-1].y;
            }
            //设置小蛇的头部
            switch (this.direction) {
                case 2:
                    this.body[0].x+=1;
                    break;
                case 0:
                    this.body[0].x-=1;
                    break;
                case 1:
                    this.body[0].y-=1;
                    break;
                case 3:
                    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;

            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
                })
                food.init(map);
            }

        };
        //封装删除小蛇的函数
        function remove() {
            //先删除蛇尾--->遍历数组
            for (var i=elements.length-1;i>=0;i--){
                //获取对应下标的数组的里面的元素
                var ele=elements[i];
                //删除地图上的小蛇
                ele.parentNode.removeChild(ele);
                //删除数组里面的div
                elements.splice(i,1);
            }
        }


        //暴露给window
        window.Snake=Snake;
    })());

    //封装游戏函数
    ((function () {
        var that=null;
        //游戏的构造函数
        function Game(map) {
            this.food=new Food();
            this.snake=new Snake();
            this.map=map;

            that=this;
        }
        //通过原型对象添加方法
        Game.prototype.init=function () {
            //初始化食物和小蛇
            this.food.init(this.map);
            this.snake.init(this.map);
            //原型可以互相访问
            this.runSnake(this.food,this.map);
            this.fang();
        }

        Game.prototype.runSnake = function(food,map){
            var t = setInterval(function () {
                that.snake.move(this.food,this.map);
                that.snake.init(map);

                // that.snake.move(map);
                // that.snake.init(map);
                //获取蛇头坐标
                var headX=that.snake.body[0].x;
                var headY=that.snake.body[0].y;
                //获取地图最大的最大坐标值
                var maxX = map.offsetWidth/this.snake.width;
                var maxY = map.offsetHeight/this.snake.height;

                if (headX<0||headX>maxX){
                    clearInterval(t);
                    alert("游戏结束!")
                }else if (headY<0||headY>maxY) {
                    clearInterval(t);
                    alert("游戏结束!")
                }

                for (var i = 1;i<that.snake.body.length;i++){
                    if (headX==that.snake.body[i].x&&headY==that.snake.body[i].y){
                        alert("游戏结束!");
                        clearInterval(t);
                    }
                }


            }.bind(that),speed);


            //判断

        };

        Game.prototype.fang = function(){
            document.addEventListener('keydown',function (e) {
                var n=event.keyCode-37;
                n=n<4?n:this.snake.direction;
                n=n>=0?n:this.snake.direction;
                this.snake.direction=Math.abs(this.snake.direction-n)==2?this.snake.direction:n;
            }.bind(that))
        }

        //暴露给window
        window.Game=Game;
    })());

    var game=new Game(document.querySelector('.map'));
    game.init();

</script>

猜你喜欢

转载自blog.csdn.net/m0_38027483/article/details/81672492