Concepto de diseño de serpiente

             <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .map {
    
    
            width: 800px;
            height: 600px;
            position: relative;
            background-color: #ccc;
            margin: 50px auto;
        }
    </style>
</head>

<body>
    <div class="map"></div>
    <script>
        //    食物
        (function () {
    
    
            var elements = [];
            function Food(x, y, width, height, color) {
    
    
                this.x = x;
                this.y = y;;
                this.width = width || 20;
                this.height = height || 20;
                this.color = color || "blue";
            };

            //暴露Food
            window.Food = Food;
            //    原型初始化
            Food.prototype.init = function (map) {
    
    
                //  添加食物
                remove();
                var div = document.createElement("div");
                map.appendChild(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.style.color = this.color;
                elements.push(div);
            }
            remove();
            //   删除食物
            function remove() {
    
    
                for (var i = 0; i < elements.length; i++) {
    
    
                    var ele = elements[i];
                    ele.parentNode.removeChild(ele);
                    elements.splice(i, 1);
                }
            }


        }());


        // 小蛇的自调用函数
        (function () {
    
    
            // 存放小蛇的身体
            var elements = [];
            function Snake(width, height, direction) {
    
    
                this.width = width || 20;
                this.height = height || 20;
                // this.backgroundColor = color|| 'red';
                this.body = [
                    {
    
     x: 3, y: 2, color: 'red' },
                    {
    
     x: 2, y: 2, color: 'orange' },
                    {
    
     x: 1, y: 2, color: 'orange' }
                ];
                this.direction = direction || 'right';
            }
            Snake.prototype.init = function (map) {
    
    
                remove();
                for (var i = 0; i < this.body.length; i++) {
    
    
                    //    每个元素都是一个对象
                    var obj = this.body[i];
                    
                    var div = document.createElement('div');
                    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.backgroundColor = obj.color;


                    elements.push(div);
                    //  暴露对象

                };
            };
            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;
                }
                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;
                if(headX==food.x&&headY==food.y){
    
    
                      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() {
    
    
                var i = elements.length - 1;
                for (; i >= 0; i--) {
    
    
                    // 先找到父元素,再删除子元素
                    var ele = elements[i];
                    ele.parentNode.removeChild(ele);
                    elements.splice(i, 1);
                }
            }
            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.bindKey();
            };
            // 添加原型方法
            Game.prototype.runSnake =function(food,map){
    
    
            var timer =  setInterval(function(){
    
    
                    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>=40){
    
    
                        clearInterval(timer);
                        alert('游戏结束');
                    }
                    if(headY<0||headY>=30){
    
    
                        clearInterval(timer);
                        alert('游戏结束');
                    }
                }.bind(that),150)
            }
            Game.prototype.bindKey =function(){
    
    
                document.addEventListener('keydown',function(e){
    
    
                    switch (e.keyCode){
    
    
                    case 37:this.snake.direction ="left";break;
                    case 38:this.snake.direction ="top";break;
                    case 39:this.snake.direction ="right";break;
                    case 40:this.snake.direction ="bottom";break;
                    }
                }.bind(that),false)
            }

            window.Game = Game;
        }());
        var gm = new Game(document.querySelector('.map'));
        gm.init();
 


    </script>
</body>

</html>

Supongo que te gusta

Origin blog.csdn.net/weixin_45176415/article/details/104854634
Recomendado
Clasificación