贪吃蛇 【源代码】

<!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>贪吃蛇</title>
    <link rel="stylesheet" href="css/index.css">
    <style>
        .wrap {
            position: relative;
            width: 500px;
            height: 500px;
            margin: 50px auto;
            background-color: #000;
            overflow: hidden;
        }

        .round {
            display: block;
            position: absolute;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: green;
        }

        .round.head {
            background-color: red;
        }
    </style>
</head>

<body>
    <div class="wrap"></div>
    <script src="js/utils.js"></script>
    <script>
        window.onload = function () {
            init();
        }

        function init() {
            initGame();
        }

        var initGame = (function () {
            var wrap = document.getElementsByClassName('wrap')[0],
                t = null;
            var Snake = function () {
                this.bodyArr = [
                    { x: 0, y: 0 },
                    { x: 0, y: 20 },
                    { x: 0, y: 40 },
                    { x: 0, y: 60 },
                    { x: 0, y: 80 },
                    { x: 0, y: 100 },
                ];

                this.dir = 'DOWN';
            }

            Snake.prototype = {
                init: function () {
                    this.bindEvent();
                    this.initSnake();
                    this.run();
                },
                bindEvent: function () {
                    var _self = this;
                    addEvent(document, 'keydown', function () {
                        _self.changeDir();
                    })
                },

                initSnake: function () {
                    var arr = this.bodyArr,
                        len = arr.length,
                        frag = document.createDocumentFragment(), // 文档碎片
                        item;

                    for (var i = 0; i < len; i++) {
                        item = arr[i];

                        var round = document.createElement('i');
                        round.className = i === len - 1 ? 'round head' : 'round';
                        round.style.left = item.x + 'px';
                        round.style.top = item.y + 'px';
                        frag.appendChild(round);
                    }
                    wrap.appendChild(frag);
                },

                run: function () {
                    var _self = this;
                    t = setInterval(function () {
                        _self.move();
                    }, 300)
                },

                move: function () {
                    var arr = this.bodyArr,
                        len = arr.length,
                        head = arr[len - 1];

                    for (var i = 0; i < len; i++) {
                        if (i === len - 1) {
                            switch (this.dir) {
                                case 'LEFT':
                                    head.x -= 20;
                                    break;
                                case 'RIGHT':
                                    head.x += 20;
                                    break;
                                case 'UP':
                                    head.y -= 20;
                                    break;
                                case 'DOWN':
                                    head.y += 20;
                                    break;
                                default:
                                    break;
                            }
                        } else {
                            arr[i].x = arr[i + 1].x;
                            arr[i].y = arr[i + 1].y;
                        }
                    }

                    this.removeSnake();
                    this.initSnake();
                },

                removeSnake: function () {
                    var bodys = document.getElementsByClassName('round');

                    while (bodys.length > 0) {
                        bodys[0].remove();
                    }
                },

                changeDir: function (e) {
                    var e = e || window.event,
                        code = e.keyCode;

                    this.setDir(code);
                },

                setDir: function (code) {
                    switch (code) {
                        case 37: //左
                            if (this.dir !== 'RIGHT' && this.dir !== 'LEFT') {
                                this.dir = 'LEFT';
                            }
                            break;
                        case 39: //右
                            if (this.dir !== 'RIGHT' && this.dir !== 'LEFT') {
                                this.dir = 'RIGHT';
                            }
                            break;
                        case 38: //上
                            if (this.dir !== 'UP' && this.dir !== 'DOWN') {
                                this.dir = 'UP';
                            }
                            break;
                        case 40: //下
                            if (this.dir !== 'UP' && this.dir !== 'DOWN') {
                                this.dir = 'DOWN';
                            }
                            break;
                        default:
                            break;
                    }
                }
            }

            return new Snake().init();
        })
    </script>
</body>

</html>
上一篇:收益比较(Java)


下一篇:C#和sql 中的 四舍五入向下向上取整