贪吃蛇JS版

HTML部分

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>来玩贪吃蛇吧</title>
    <style>
        .map{
            width: 800px;
            height: 600px;
            background-color: seagreen;
            position: relative;
        }
    </style>
</head>
<body>
<div class="map"></div>
<script src="食物JS.js"></script>
<script src="贪吃蛇JS.js"></script>
<script src="游戏JS.js"></script>
<script>
var gm=new Game(document.querySelector(".map"));
    gm.init();
</script>
</body>
</html>

JS部分

食物对象

//自调函数--食物
((function () {
    //自定义构造函数创建对象
    var elements=[];
    function Food(width,height,color,x,y) {
        this.width=width||20;
        this.height=height||20;
        this.color=color||"black";
        this.x=x||0;
        this.y=y||0;
    }
    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.position="absolute";
        div.style.backgroundColor=this.color;
        //随机横纵坐标
        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";
        elements.push(div);
    }
    function remove() {
        for(var i=0;i<elements.length;i++){
            var ele=elements[i];
            ele.parentNode.removeChild(ele);
            elements.splice(i,1);
        }
    }
    window.Food=Food;
})());

贪吃蛇对象

//自调函数--贪吃蛇
((function () {
    var elements=[];
    function Snake(width,height,direction) {
        this.width=width||20;
        this.height=height||20;
        //贪吃蛇的身体
        this.body=[
            {x:3,y:2,color:"blue"},
            {x:2,y:2,color:"white"},
            {x:1,y:2,color:"white"},
        ];
        this.direction=direction||"right";
    }
    Snake.prototype.init=function (map) {
        remove();
        for(var i=0;i<this.body.length;i++){
            var div=document.createElement("div");
            map.appendChild(div);
            var obj=this.body[i];
            div.style.width=this.width+"px";
            div.style.height=this.height+"px";
            div.style.position="absolute";
            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 "left":this.body[0].x-=1;break;
            case "right":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 timeId=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>=maxX){
                clearInterval(timeId);
                alert("很遗憾,游戏结束了");
            };
            if(headY<0||headY>=maxY){
                clearInterval(timeId);
                alert("很遗憾,游戏结束了");
            };
            //头不能撞到自己的身体(头的坐标不能等于任何一个身体部位的坐标)
            var currentBody=this.snake.body.slice(1);
            for(var i=5;i<currentBody.length;i++){
                if(headX==currentBody[i].x&&headY==currentBody[i].y){
                    clearInterval(timeId);
                    alert("呀,撞到自己了");
                }
            }
        }.bind(that),200)
    }
    //设置用户按键控制贪吃蛇移动
    Game.prototype.bindKey=function(){
        //阻止贪吃蛇反向
        //为键盘按下注册多个事件
        document.addEventListener("keydown",function (e) {
            switch (e.keyCode){
                case 37:this.snake.direction="left";break;
                case 39:this.snake.direction="right";break;
                case 38:this.snake.direction="top";break;
                case 40:this.snake.direction="bottom";break;
                default:;
            }
        }.bind(that),false);

    }
    window.Game=Game;
})())

猜你喜欢

转载自blog.csdn.net/weixin_44478408/article/details/86766007