fightangeI 2020-04-29 17:32 采纳率: 100%
浏览 205
已采纳

最近在写一个贪吃蛇代码,有一个问题理解不了了。

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style type="text/css">
        body {
            margin: 0;
            padding: 0;
        }

        .main {
            width: 800px;
            height: 400px;
            margin: 50px auto;
        }

        .btn {
            width: 100px;
            height: 40px;
        }

        .map {
            position: relative;
            width: 800px;
            height: 400px;
            background: #ccc;
        }
    </style>
</head>

<body>
    <div class="main">
        <button class="btn" id="begin">开始游戏</button>
        <div class="map" id="map"></div>
        <script type="text/javascript">
            var map = document.getElementById('map');
            // 使用构造方法创建蛇,
            function Snake() {
                // 设置蛇的宽、高、默认走的方向
                this.width = 10;
                this.height = 10;
                this.direction = 'right';
                // 记住蛇的状态,当吃完食物的时候,就要加一个,初始为3个小点为一个蛇,
                this.body = [{
                        x: 2,
                        y: 0
                    }, // 蛇头,第一个点
                    {
                        x: 1,
                        y: 0
                    }, // 蛇脖子,第二个点
                    {
                        x: 0,
                        y: 0
                    } // 蛇尾,第三个点
                ];
                // 显示蛇
                this.display = function () {
                    // 创建蛇
                    for (var i = 0; i < this.body.length; i++) {
                        if (this.body[i].x != null) { // 当吃到食物时,x==null,不能新建,不然会在0,0处新建一个
                            var s = document.createElement('div');
                            // 将节点保存到状态中,以便于后面删除
                            this.body[i].flag = s;
                            // 设置宽高
                            s.style.width = this.width + 'px';
                            s.style.height = this.height + 'px';
                            s.style.borderRadius = "50%";
                            s.style.background = "green";
                            // 设置位置
                            s.style.position = 'absolute';
                            s.style.left = this.body[i].x * this.width + 'px';
                            s.style.top = this.body[i].y * this.height + 'px';
                            // 添加进去
                            map.appendChild(s);
                        }
                    }
                };
                // 让蛇跑起来,后一个元素到前一个元素的位置
                // 蛇头根据方向处理,所以i不能等于0
                this.run = function () {
                    // 后一个元素到前一个元素的位置
                    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 "left":
                            this.body[0].x -= 1;
                            break;
                        case "right":
                            this.body[0].x += 1;
                            break;
                        case "up":
                            this.body[0].y -= 1;
                            break;
                        case "down":
                            this.body[0].y += 1;
                            break;
                    }
                    // 判断是否出界,一蛇头判断,出界的话,
                    if (this.body[0].x < 0 || this.body[0].x > 79 || this.body[0].y < 0 || this.body[0].y > 39) {
                        clearInterval(timer); // 清除定时器,
                        alert("你瞎吗?撞死了!");
                        // 删除旧的
                        for (var i = 0; i < this.body.length; i++) {
                            if (this.body[i].flag != null) { // 如果刚吃完就死掉,会加一个值为null的
                                map.removeChild(this.body[i].flag);
                            }
                        }
                        this.body = [ // 回到初始状态,
                            {
                                x: 2,
                                y: 0
                            },
                            {
                                x: 1,
                                y: 0
                            },
                            {
                                x: 0,
                                y: 0
                            }
                        ];
                        this.direction = 'right';
                        this.display(); // 显示初始状态
                        return false; // 结束
                    }
                    // 判断蛇头吃到食物,xy坐标重合,
                    if (this.body[0].x == food.x && this.body[0].y == food.y) {
                        // 蛇加一节,因为根据最后节点定,下面display时,会自动赋值的
                        this.body.push({
                            x: null,
                            y: null,
                            flag: null
                        });
                    //     // 清除食物,重新生成食物
                        map.removeChild(food.flag);
                        food.display();
                    }
                    // 吃到自己死亡,从第五个开始与头判断,因为前四个永远撞不到
                    for (var i = 4; i < this.body.length; i++) {
                        if (this.body[0].x == this.body[i].x && this.body[0].y == this.body[i].y) {
                            clearInterval(timer); // 清除定时器,
                            alert("傻子!你怎么能吃自己呢?");
                            // 删除旧的
                            for (var i = 0; i < this.body.length; i++) {
                                if (this.body[i].flag != null) { // 如果刚吃完就死掉,会加一个值为null的
                                    map.removeChild(this.body[i].flag);
                                }
                            }
                            this.body = [ // 回到初始状态,
                                {
                                    x: 2,
                                    y: 0
                                },
                                {
                                    x: 1,
                                    y: 0
                                },
                                {
                                    x: 0,
                                    y: 0
                                }
                            ];
                            this.direction = 'right';
                            this.display(); // 显示初始状态
                            return false; // 结束
                        }
                    }
                    // // 先删掉初始的蛇,再显示新蛇
                    for (var i = 0; i < this.body.length; i++) {
                        if (this.body[i].flag != null) { // 当吃到食物时,flag是等于null,且不能删除
                            map.removeChild(this.body[i].flag);
                        }
                    }
                    // 重新显示蛇
                    this.display();
                }

            }
            // 构造食物
            function Food() {
                this.width = 10;
                this.height = 10;
                this.display = function () {
                    var f = document.createElement('div');
                    this.flag = f;
                    f.style.width = this.width + 'px';
                    f.style.height = this.height + 'px';
                    f.style.background = 'red';
                    f.style.borderRadius = '50%';
                    f.style.position = 'absolute';
                    this.x = Math.floor(Math.random() * 80);
                    this.y = Math.floor(Math.random() * 40);
                    f.style.left = this.x * this.width + 'px';
                    f.style.top = this.y * this.height + 'px';
                    map.appendChild(f);
                }
            }
            var snake = new Snake();
            var food = new Food();
            snake.display(); // 初始化显示
            food.display();
            // 给body加按键事件,上下左右
            document.body.onkeydown = function (e) {
                // 有事件对象就用事件对象,没有就自己创建一个,兼容低版本浏览器
                var ev = e || window.event;
                switch (ev.keyCode) {
                    case 38:
                        if (snake.direction != 'down') { // 不允许返回,向上的时候不能向下
                            snake.direction = "up";
                        }
                        break;
                    case 40:
                        if (snake.direction != "up") {
                            snake.direction = "down";
                        }
                        break;
                    case 37:
                        if (snake.direction != "right") {
                            snake.direction = "left";
                        }
                        break;
                    case 39:
                        if (snake.direction != "left") {
                            snake.direction = "right";
                        }
                        break;
                }
            };
            // 点击开始时,动起来
            var begin = document.getElementById('begin');
            var timer;
            begin.onclick = function () {
                clearInterval(timer);
                // timer = setInterval(snake.run(), 500);  // 先执行run函数,把执行得到的结果,每500毫秒执行一次,不会在执行内部代码
                timer = setInterval("snake.run()", 100); // 小技巧,每500毫秒执行字符串,字符串执行内部代码
            };
        </script>
    </div>
</body>

</html>

如果我用

var rel = [ {
                                x: 2,
                                y: 0
                            },
                            {
                                x: 1,
                                y: 0
                            },
                            {
                                x: 0,
                                y: 0
                            }
                        ];

然后下面的回到初始化状态将伪数组用rel代替,则rel会变成当前body的x和y,请问这是为什么

  this.body = rel //回到初始状态
  • 写回答

1条回答 默认 最新

  • SNiFe_Blog 2020-04-29 23:43
    关注

    赋的是vel的引用,所以this.body也会影响vel的值。
    一维数组深度拷贝直接使用slice()或则contact()这两个方法就行,而这是二维数组,要深度拷贝的话用这个方法不行,因为二维数组第一维存储的是第二维的引用,第二维才是真实值,那么应该怎么办呢?这里提供一条链接
    修改过后的代码如下:

    <!doctype html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
            content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style type="text/css">
            body {
                margin: 0;
                padding: 0;
            }
    
            .main {
                width: 800px;
                height: 400px;
                margin: 50px auto;
            }
    
            .btn {
                width: 100px;
                height: 40px;
            }
    
            .map {
                position: relative;
                width: 800px;
                height: 400px;
                background: #ccc;
            }
        </style>
    </head>
    
    <body>
        <div class="main">
            <button class="btn" id="begin">开始游戏</button>
            <div class="map" id="map"></div>
            <script type="text/javascript">
                var map = document.getElementById('map');
                var vel=[{
                            x: 2,
                            y: 0
                        }, // 蛇头,第一个点
                        {
                            x: 1,
                            y: 0
                        }, // 蛇脖子,第二个点
                        {
                            x: 0,
                            y: 0
                        } // 蛇尾,第三个点
                    ];
                // 使用构造方法创建蛇,
                function Snake() {
                    // 设置蛇的宽、高、默认走的方向
                    this.width = 10;
                    this.height = 10;
                    this.direction = 'right';
                    // 记住蛇的状态,当吃完食物的时候,就要加一个,初始为3个小点为一个蛇,
                    this.body = JSON.parse(JSON.stringify(vel));
                    console.log(vel[1]);
                    // 显示蛇
                    this.display = function () {
                        // 创建蛇
                        for (var i = 0; i < this.body.length; i++) {
                            if (this.body[i].x != null) { // 当吃到食物时,x==null,不能新建,不然会在0,0处新建一个
                                var s = document.createElement('div');
                                // 将节点保存到状态中,以便于后面删除
                                this.body[i].flag = s;
                                // 设置宽高
                                s.style.width = this.width + 'px';
                                s.style.height = this.height + 'px';
                                s.style.borderRadius = "50%";
                                s.style.background = "green";
                                // 设置位置
                                s.style.position = 'absolute';
                                s.style.left = this.body[i].x * this.width + 'px';
                                s.style.top = this.body[i].y * this.height + 'px';
                                // 添加进去
                                map.appendChild(s);
                            }
                        }
                    };
                    // 让蛇跑起来,后一个元素到前一个元素的位置
                    // 蛇头根据方向处理,所以i不能等于0
                    this.run = function () {
                        // 后一个元素到前一个元素的位置
                        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 "left":
                                this.body[0].x -= 1;
                                break;
                            case "right":
                                this.body[0].x += 1;
                                break;
                            case "up":
                                this.body[0].y -= 1;
                                break;
                            case "down":
                                this.body[0].y += 1;
                                break;
                        }
                        // 判断是否出界,一蛇头判断,出界的话,
                        if (this.body[0].x < 0 || this.body[0].x > 79 || this.body[0].y < 0 || this.body[0].y > 39) {
                            clearInterval(timer); // 清除定时器,
                            alert("你瞎吗?撞死了!");
                            // 删除旧的
                            for (var i = 0; i < this.body.length; i++) {
                                if (this.body[i].flag != null) { // 如果刚吃完就死掉,会加一个值为null的
                                    map.removeChild(this.body[i].flag);
                                }
                            }
                            this.body = JSON.parse(JSON.stringify(vel));
                            this.direction = 'right';
                            this.display(); // 显示初始状态
                            return false; // 结束
                        }
                        // 判断蛇头吃到食物,xy坐标重合,
                        if (this.body[0].x == food.x && this.body[0].y == food.y) {
                            // 蛇加一节,因为根据最后节点定,下面display时,会自动赋值的
                            this.body.push({
                                x: null,
                                y: null,
                                flag: null
                            });
                        //     // 清除食物,重新生成食物
                            map.removeChild(food.flag);
                            food.display();
                        }
                        // 吃到自己死亡,从第五个开始与头判断,因为前四个永远撞不到
                        for (var i = 4; i < this.body.length; i++) {
                            if (this.body[0].x == this.body[i].x && this.body[0].y == this.body[i].y) {
                                clearInterval(timer); // 清除定时器,
                                alert("傻子!你怎么能吃自己呢?");
                                // 删除旧的
                                for (var i = 0; i < this.body.length; i++) {
                                    if (this.body[i].flag != null) { // 如果刚吃完就死掉,会加一个值为null的
                                        map.removeChild(this.body[i].flag);
                                    }
                                }
                                this.body = JSON.parse(JSON.stringify(vel));
                                this.direction = 'right';
                                this.display(); // 显示初始状态
                                return false; // 结束
                            }
                        }
                        // // 先删掉初始的蛇,再显示新蛇
                        for (var i = 0; i < this.body.length; i++) {
                            if (this.body[i].flag != null) { // 当吃到食物时,flag是等于null,且不能删除
                                map.removeChild(this.body[i].flag);
                            }
                        }
                        // 重新显示蛇
                        this.display();
                    }
    
                }
                // 构造食物
                function Food() {
                    this.width = 10;
                    this.height = 10;
                    this.display = function () {
                        var f = document.createElement('div');
                        this.flag = f;
                        f.style.width = this.width + 'px';
                        f.style.height = this.height + 'px';
                        f.style.background = 'red';
                        f.style.borderRadius = '50%';
                        f.style.position = 'absolute';
                        this.x = Math.floor(Math.random() * 80);
                        this.y = Math.floor(Math.random() * 40);
                        f.style.left = this.x * this.width + 'px';
                        f.style.top = this.y * this.height + 'px';
                        map.appendChild(f);
                    }
                }
                var snake = new Snake();
                var food = new Food();
                snake.display(); // 初始化显示
                food.display();
                // 给body加按键事件,上下左右
                document.body.onkeydown = function (e) {
                    // 有事件对象就用事件对象,没有就自己创建一个,兼容低版本浏览器
                    var ev = e || window.event;
                    switch (ev.keyCode) {
                        case 38:
                            if (snake.direction != 'down') { // 不允许返回,向上的时候不能向下
                                snake.direction = "up";
                            }
                            break;
                        case 40:
                            if (snake.direction != "up") {
                                snake.direction = "down";
                            }
                            break;
                        case 37:
                            if (snake.direction != "right") {
                                snake.direction = "left";
                            }
                            break;
                        case 39:
                            if (snake.direction != "left") {
                                snake.direction = "right";
                            }
                            break;
                    }
                };
                // 点击开始时,动起来
                var begin = document.getElementById('begin');
                var timer;
                begin.onclick = function () {
                    clearInterval(timer);
                    // timer = setInterval(snake.run(), 500);  // 先执行run函数,把执行得到的结果,每500毫秒执行一次,不会在执行内部代码
                    timer = setInterval("snake.run()", 100); // 小技巧,每500毫秒执行字符串,字符串执行内部代码
                };
            </script>
        </div>
    </body>
    
    </html>
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?