当前位置:   article > 正文

js实现贪吃蛇小游戏_贪吃蛇js

贪吃蛇js


一、body部分

    <style>
        #box{
            width: 800px;
            height: 600px;
            background-color: #999;
            position: relative;
        }
    </style>
</head>
<script src="./贪吃蛇js/食物.js"></script>
<script src="./贪吃蛇js/蛇.js"></script>
<script src="./贪吃蛇js/游戏.js"></script>

<body>
    <!-- 新建一个盒子用来做背景 -->
    <div id="box"></div>
</body>
<script>
    /* 
    食物对象:
        属性:宽,高,背景颜色,定位,左距离,上距离
        方法:食物的渲染方法(随机生成 )
    
    */
    var map =document.getElementById('box')
    var game = new Game(new Food(20, 20, 'blue', 'absolute', 0, 0, []),
    new Snake(20, 20, 'absolute', 'right', []),
    map
  )
    game.renderGame()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

二、食物部分

// 创建食物的构造函数
function Food(width, height, bgColor, position, left, top, foodArr) {
  this.width = width;
  this.height = height;
  this.bgColor = bgColor;
  this.position = position;
  this.left = left;
  this.top = top;
  this.foodArr =foodArr;
}
//在食物构造函数的原型中添加随机生成的方法
Food.prototype.renderFood = function () {
  //在地图中建立一个div
  var foodBox = map.appendChild(document.createElement("div"));
  // 给这个添加的div设置样式,设置的样式根据实例化
  foodBox.style.width = this.width + "px";
  foodBox.style.height = this.height + "px";
  foodBox.style.backgroundColor = this.bgColor;
  foodBox.style.position = this.position;

  //随机产生食物
  this.left =
    parseInt(Math.random() * (map.clientWidth / this.width)) * this.width;
  this.top =
    parseInt(Math.random() * (map.clientHeight / this.height)) * this.height;
  foodBox.style.left = this.left + "px";
  foodBox.style.top = this.top + "px";
  this.foodArr.push(foodBox);
};
Food.prototype.deleteFood = function () {
  for (var i = this.foodArr.length - 1; i >= 0; i--) {
    map.removeChild(this.foodArr[i]);
    this.foodArr.splice(i, 1);
  }
};

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
'
运行

三、蛇部分

function Snake(width, height, position, direction, snakeArr) {
    this.width = width;
    this.height = height;
    // this.bgColor = bgColor;
    this.position = position;
    // this.left = left;
    // this.top = top;
    this.direction = direction;
    this.body = [
        // 蛇头
        {
            x: 2,
            y: 1,
            color: 'yellow'
        },
        // 蛇身
        {
            x: 1,
            y: 1,
            color: 'red'
        },
        // 蛇尾
        {
            x: 0,
            y: 1,
            color: 'red'
        }
    ]
    this.snakeArr = snakeArr
}
Snake.prototype.renderSnake = function () {
    for (var i = 0; i < this.body.length; i++) {
        var snakeBox = map.appendChild(document.createElement('div'))
        snakeBox.style.width = this.width + 'px';
        snakeBox.style.height = this.height + "px";
        snakeBox.style.position = this.position;
        snakeBox.style.left = this.body[i].x * this.width + 'px';
        snakeBox.style.top = this.body[i].y * this.height + 'px';
        snakeBox.style.backgroundColor = this.body[i].color;
        this.snakeArr.push(snakeBox)
    }
    console.log(this.snakeArr);
}
Snake.prototype.moveSnake = function (food) {
    console.log('蛇移动了');
    /*     this.body[0].x++
        this.body[1].x++
        this.body[2].x++ */
    // for (var i = 0; i < this.body.length; i++) {
    //     // this.body[i].x++

    // }
    for (var i = this.body.length - 1; i > 0; i--) {
        /* 
        i = 2  蛇尾   蛇身
        i = 1  蛇身   蛇头
        */
        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--
            break;
        case 'right':
            this.body[0].x++
            break;
        case 'top':
            this.body[0].y--
            break;
        case 'bottom':
            this.body[0].y++
            break;
        default:
            break;
    }
    // 蛇吃食物  蛇尾变长  食物消失 重新渲染
    // 让蛇头的left和top 等于食物的left和top
    var snakeLeft = this.body[0].x * this.width
    var snakeTop = this.body[0].y * this.height
    console.log(food.left);
    if (food.left == snakeLeft && food.top == snakeTop) {
        console.log('重合了');
        this.body.push({
            x: this.body[this.body.length - 1].x,
            y: this.body[this.body.length - 1].y,
            color: this.body[this.body.length - 1].color
        })
        // 重新渲染
        this.renderSnake()
        // 食物消失
        food.deleteFood()
        // 重新渲染
        food.renderFood()
    }
}
// 蛇删除
Snake.prototype.deleteSnake = function () {
    // for (var i = 0; i < this.snakeArr.length; i++) {
    //     // pop()  shift()
    //     console.log(i); // 0 1
    //     this.snakeArr.splice(i, 1)
    // }
    for (var i = this.snakeArr.length - 1; i >= 0; i--) {
        // 删除DOM
        // 父元素.removeChild()
        map.removeChild(this.snakeArr[i])

        this.snakeArr.splice(i, 1)
    }
    console.log(this.snakeArr);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
'
运行

四、游戏的主体部分

function Game(food, snake, map) {
    this.food = food;
    this.snake = snake;
    this.map = map;
}
Game.prototype.renderGame = function () {
    this.food.renderFood()
    this.snake.renderSnake()
    this.startGame()
    this.directionSnake()
}
// 游戏开始
Game.prototype.startGame = function () {
    console.log('游戏开始');
    var timer = setInterval(function () {
        console.log('定时器');
        var maxX = map.clientWidth / this.snake.width - 1
        var maxY = map.clientHeight / this.snake.height - 1
        var x = this.snake.body[0].x
        var y = this.snake.body[0].y
        if (x > 0 && x < maxX && y > 0 && y < maxY) {
            // this.snake.deleteSnake()
            // 调用蛇移动方法
            // console.log(this);
            this.snake.moveSnake(this.food)
            // 重新渲染
            this.snake.renderSnake()
        } else {
            clearInterval(timer)
            alert('Game over')
        }

    }.bind(this), 150)
}
Game.prototype.directionSnake = function () {
    var that = this;
    console.log(that);
    document.onkeyup = function () {
        var event = event || window.event
        console.log(event.keyCode);
        // console.log(this);
        switch (event.keyCode) {
            case 37:
                that.snake.direction = 'left'
                break;
            case 38:
                that.snake.direction = 'top'
                break;
            case 39:
                that.snake.direction = 'right'
                break;
            case 40:
                that.snake.direction = 'bottom'
                break;
            default:
                break;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
'
运行
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/1001468
推荐阅读
相关标签
  

闽ICP备14008679号