canvas标签应用 简单"贪吃蛇"游戏

<!DOCTYPE HTML>
<body>
    <canvas width="400px" height="500px" id="bk_canvas"/>
    <script type="text/javascript">
        var foods = new Array();    //存放食物坐标
        var g_nStopFlag = 0;
        function node(x,y,w)
        {
            var _this = this;
            this.x = x
            this.y = y;
            this.w = w;
            this.equals = function(node){
               if(this.x == node.x && this.y == node.y)return true;
                   return false;
            };
            this.foodInit = function(){
                 var obj = document.getElementById("bk_canvas");
                 var cxt = obj.getContext("2d");
                 cxt.fillStyle = "#FF0000";
                 //cxt.strokeStyle = "#000000";
                 cxt.fillRect(x,y,w,w);
                 //cxt.strokeRect(x,y,w,w);
            };
            this.snakeInit = function(){
                 var obj = document.getElementById("bk_canvas");
                 var cxt = obj.getContext("2d");
                 cxt.fillStyle = "#000000";
                 cxt.strokeStyle = "#FFFFFF";
                 cxt.fillRect(x,y,w,w);
                 cxt.strokeRect(x,y,w,w);
            };
            this.clear = function(){
                 var obj = document.getElementById("bk_canvas");
                 var cxt = obj.getContext("2d");
                 cxt.fillStyle = "#E8FFFF";
                 cxt.strokeStyle = "#E8FFFF";
                 cxt.fillRect(x,y,w,w);
                 cxt.strokeRect(x,y,w,w);
            };
        }
                function farm()
        {
            var _this = this;
            var init = function()
            {
                var bk_farm = document.getElementById("bk_canvas");
                var ctx = bk_farm.getContext("2d");
                ctx.fillStyle = "#E8FFFF";          //填充
				ctx.strokeStyle = "#000000";        //勾勒边框
				ctx.fillRect(0,0,bk_farm.width,bk_farm.height); 
				ctx.strokeRect(0, 0, bk_farm.width,bk_farm.height)
            }
            this.addFood = function()
            {
                if(g_nStopFlag)
                {
                    window.clearInterval(id_food);
                    window.clearInterval(id_draw);
                }
                var obj = document.getElementById("bk_canvas");
                var x = parseInt(obj.width - parseInt(Math.random()*obj.width/10)*10);
                var y = parseInt(obj.height - parseInt(Math.random()*obj.height/10)*10);
                var food = new node(x,y,10);
                food.foodInit();
                foods.push(food);
            }
            this.reDraw = function()
            {
                if(g_nStopFlag)
                {
                    window.clearInterval(id_food);
                    window.clearInterval(id_draw);
                }
                var bk_farm = document.getElementById("bk_canvas");
                var ctx = bk_farm.getContext("2d");
				ctx.strokeStyle = "#000000";        //勾勒边框
				ctx.strokeRect(0, 0, bk_farm.width,bk_farm.height)
            }
            var id_food = setInterval(_this.addFood,1000*10);
            var id_draw = setInterval(_this.reDraw,1000);
            init();
        }
        var farm = new farm();         
        function snake(x,y,len,speed)
        {
            var _this = this;
            this.nodes = new Array();
            this.odir = "L";
            this.dir = "L";
            this.len = 5;
            this.speed = parseInt(speed)*100;
            var init = function(){
                _this.len = len;
                _this.dir = "R";
                var nx = x;
                var ny = y;
                var nw = 10;
                for(var i=0;i<len;i++)
                {
                    var no = new node(nx,ny,nw);
                    _this.nodes[i] = no;
                    no.snakeInit();
                    nx -= nw;
                }
            };
            this.move = function(){
                var obj = document.getElementById("bk_canvas");
                var maxX = obj.width;
                var maxY = obj.height;
                var len = parseInt(_this.len);
                var head = _this.nodes[0]; 
                var hx = parseInt(head.x);
                var hy = parseInt(head.y);
                var hw = parseInt(head.w);  
                switch(_this.dir){
                    case "R":{//穿墙判断
                        if(hx + hw >= maxX) var nHead = new node(0,hy,hw);              
                        else var nHead = new node(hx+hw,hy,hw);                     
                    };break;
                    case "L":{ 
                        if(hx-hw < 0) var nHead = new node(maxX,hy,hw);              
                        else var nHead = new node(hx-hw,hy,hw);
                    };break;
                    case "U":{
                        if(hy-hw < 0 ) var nHead = new node(hx,maxY,hw);               
                        else var nHead = new node(hx,hy-hw,hw);
                    };break;
                    case "D":{ 
                        if(hy + hw >maxY ) var nHead = new node(hx,0,hw);                
                        else var nHead = new node(hx,hy+hw,hw);
                    };break;
                }
                if(_this.nodes[1].equals(nHead)){//运行反方向
                    _this.dir = _this.odir;
                    return;
                }
                for(var i=0;i<len;i++)
                {//穿过自己身体
                    if(_this.nodes[i].equals(nHead))
                    {
                        _this.stop();
                        return;
                    }
                }
                nHead.snakeInit();
                var tail = _this.nodes[len-1];
                tail.clear();
                for(var i=len-1;i>0;i--)
                {
                    _this.nodes[i] = _this.nodes[i-1];
                }
                _this.nodes[0] = nHead;
                for(var i=0;i<foods.length;i++)
                {
                    if(foods[i].equals(nHead)){
                        _this.addNode();
                        foods.pop(nHead);
                        break;
                    }
                }
            };
            this.stop = function(){
                window.clearInterval(_this.run);
                g_nStopFlag = 1;
            }
            this.addNode = function(){
                var len = parseInt(_this.len);
                var tail1 = _this.nodes[len-1];
                var tail2 = _this.nodes[len-2];
                var x1 = parseInt(tail1.x);
                var x2 = parseInt(tail2.x);
                var y1 = parseInt(tail1.y);
                var y2 = parseInt(tail2.y);
                var w  = parseInt(tail1.w);
                if(x1 == x2 )
                {
                    if(y1>y2) var no = new node(x1,y1+w,w);
                    else var no = new node(x1,y1-w,w);
                }
                else
                {
                    if(x1>x2) var no = new node(x1+w,y1,w);
                    else var no = new node(x1-w,y1,w);
                }
                no.snakeInit();
                _this.nodes[len] = no;
                _this.len = len+1;
                
                _this.stop();
                _this.speed = parseInt(_this.speed)-5;
                this.run = setInterval(_this.move,_this.speed);
                farm.addFood();                
            }
            this.run = setInterval(_this.move,_this.speed);
            document.onkeydown = function (event){
                _this.odir = _this.dir;
				var code = event.keyCode;
				switch(code){
				    case 65:_this.dir = "L";break;
				    case 87:_this.dir = "U";break;
				    case 68:_this.dir = "R";break;
				    case 83:_this.dir = "D";break;
				    case 37:_this.dir = "L";break;
				    case 38:_this.dir = "U";break;
				    case 39:_this.dir = "R";break;
				    case 40:_this.dir = "D";break;
				}
            }
            init();
        }      
        farm.addFood();  
        var Snake = new snake(200,200,25,1);
    </script>
<body>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值