Html+javaScript实现贪吃蛇网页小游戏

1.html结构如下(CSS忽略)

<h1>苏菲发的贪吃蛇</h1>
<div class="map"></div>

<input type="button" value="" id="btn" disabled="disabled" />

2.JS部分

// 根据id获取对象
function my$(Id){
    return document.getElementById(Id);

}

<script>

// 根据id获取对象
    function my$(Id){
    return document.getElementById(Id);

    }

    my$("btn").onclick=function () {
        location.reload();
    }
</script>
<script>
    //食物
    (function () {
        var elements=[];
        function Food(x,y,width,height,color) {
            this.x=x||0;
            this.y=y||0;
            this.width=width||20;
            this.height=height||20;
            this.color=color||"green";
        }
        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";
            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:"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.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;
            var foodx=food.x;
            var foody=food.y;
            if(headX==foodx&&headY==foody){
                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;
        var kaiShi=true;
        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();
            my$("btn").value="按空格开始";
        };
        //让蛇移动
        Game.prototype.runSnake=function (food,map) {
                my$("btn").value="正在游戏";
                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);
                        my$("btn").removeAttribute("disabled");
                        my$("btn").value="单击再玩一次";
                    }
                    if(headY<0||headY>=maxY){
                        clearInterval(timeId);
                        my$("btn").removeAttribute("disabled");
                        my$("btn").value="单击再玩一次";
                    }
                }.bind(that),150);//bind的作用是讲实例化对象传给this,不传的话this就是window,会导致报错
        };
        //设置按键,改变蛇的方向
        Game.prototype.bindKey=function () {
            //获取用户按键,改变方向
            document.addEventListener("keydown",function (e) {
                switch(e.keyCode){
                    case 32 : if(kaiShi){this.runSnake(this.food,this.map);kaiShi=false;}break;//调用小蛇自动移动方法
                    case 37 : this.snake.direction=(this.snake.direction=="right"?"right":"left");break;
                    case 38 : this.snake.direction=(this.snake.direction=="bottom"?"bottom":"top");break;
                    case 39 : this.snake.direction=(this.snake.direction=="left"?"left":"right");break;
                    case 40 : this.snake.direction=(this.snake.direction=="top"?"top":"bottom");break;
                }
            }.bind(that),false);
        };
        window.Game=Game;
    })();
    //初始化游戏对象及游戏gm
    var gm=new Game(document.querySelector(".map"));
    gm.init();
</script>

猜你喜欢

转载自blog.csdn.net/weixin_42272653/article/details/80790892