jquery做贪吃蛇教程

以下要用到jquery-2.1.1.min.js

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <style>
    .black{
        background-color: black;
    }
    .bo{
        width: 30px;
        height: 30px;
    }
    .snake{
        background-color: blue;
    }
    .food{
        background-color: green;
    }
    </style>
</head>
<body>
    <input type="button" value="star" id="kaishi">
    <table border="1px">
        <tbody id="tbody"></tbody>
    </table>
    <script src="jquery-2.1.1.min.js" type="text/javascript"></script>
    <script>
        var height=20;
        var width=30; 
        var timer;
        tab(height,width);
        var snake= new Object();
        snake.body=[[1,1]];
        $("#kaishi").on("click",function(){
        var a=Math.floor(Math.random()*height+1);
        var b=Math.floor(Math.random()*width+1);        
        $("tbody").children().eq(a).children().eq(b).addClass("food");
        snake.direction="right";
        for(var i=0;i<snake.body.length;i++){
            $("tbody").children().eq(snake.body[i][0]).children().eq(snake.body[i][1]).addClass("snake");
        }
        timer=setInterval(function(){
            if(snake.direction=="right"){
                if($("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).next().hasClass("black") || $("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).next().hasClass("snake")){
                    alert("GG");
                    window.clearInterval(timer);
                }else{
                var arr=[snake.body[0][0],snake.body[0][1]+1];
                snake.body.unshift(arr);
                $("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).addClass("snake");
                $("tbody").children().eq(snake.body[snake.body.length-1][0]).children().eq(snake.body[snake.body.length-1][1]).removeClass("snake");
                if(!$("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).hasClass("food")){
                    snake.body.pop();
                }else{
                    $("tbody").children().eq(a).children().eq(b).removeClass("food");
                    a=Math.floor(Math.random()*height+1);
                    b=Math.floor(Math.random()*width+1);
                    while(true){
                        if(!$("tbody").children().eq(a).children().eq(b).hasClass("snake")){
                            $("tbody").children().eq(a).children().eq(b).addClass("food");
                            break;
                        }else{
                            a=Math.floor(Math.random()*height+1);
                            b=Math.floor(Math.random()*width+1);
                        }
                    }                    
                }
            }
                
            }else if(snake.direction=="left"){
                    if($("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).prev().hasClass("black") || $("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).prev().hasClass("snake")){
                        alert("GG");
                        window.clearInterval(timer);    
                    }else{
                        var arr=[snake.body[0][0],snake.body[0][1]-1];
                        snake.body.unshift(arr);
                        $("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).addClass("snake");
                        $("tbody").children().eq(snake.body[snake.body.length-1][0]).children().eq(snake.body[snake.body.length-1][1]).removeClass("snake");
                        if(!$("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).hasClass("food")){
                            snake.body.pop();
                        }else{
                        $("tbody").children().eq(a).children().eq(b).removeClass("food");
                        a=Math.floor(Math.random()*height+1);
                        b=Math.floor(Math.random()*width+1);
                        while(true){
                        if(!$("tbody").children().eq(a).children().eq(b).hasClass("snake")){
                            $("tbody").children().eq(a).children().eq(b).addClass("food");
                            break;
                        }else{
                            a=Math.floor(Math.random()*height+1);
                            b=Math.floor(Math.random()*width+1);
                        }
                    }    
                    }
                }
                    
            }else if(snake.direction=="top"){
                    if($("tbody").children().eq(snake.body[0][0]).prev().children().eq(snake.body[0][1]).hasClass("black") || $("tbody").children().eq(snake.body[0][0]).prev().children().eq(snake.body[0][1]).hasClass("snake")){
                        alert("GG");
                        window.clearInterval(timer);    
                    }else{
                        var arr=[snake.body[0][0]-1,snake.body[0][1]];
                        snake.body.unshift(arr);
                        $("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).addClass("snake");
                        $("tbody").children().eq(snake.body[snake.body.length-1][0]).children().eq(snake.body[snake.body.length-1][1]).removeClass("snake");
                        if(!$("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).hasClass("food")){
                            snake.body.pop();
                        }else{
                            $("tbody").children().eq(a).children().eq(b).removeClass("food");
                            a=Math.floor(Math.random()*height+1);
                            b=Math.floor(Math.random()*width+1);
                            while(true){
                                if(!$("tbody").children().eq(a).children().eq(b).hasClass("snake")){
                                    $("tbody").children().eq(a).children().eq(b).addClass("food");
                                    break;
                                }else{
                                    a=Math.floor(Math.random()*height+1);
                                    b=Math.floor(Math.random()*width+1);
                                }
                            }    
                    }
                }
                    
            }else if(snake.direction=="bottom"){
                    if($("tbody").children().eq(snake.body[0][0]).next().children().eq(snake.body[0][1]).hasClass("black") || $("tbody").children().eq(snake.body[0][0]).next().children().eq(snake.body[0][1]).hasClass("snake")){
                        alert("game over");
                        window.clearInterval(timer);    
                    }else{
                        var arr=[snake.body[0][0]+1,snake.body[0][1]];
                        snake.body.unshift(arr);
                        $("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).addClass("snake");
                        $("tbody").children().eq(snake.body[snake.body.length-1][0]).children().eq(snake.body[snake.body.length-1][1]).removeClass("snake");
                        if(!$("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).hasClass("food")){
                            snake.body.pop();
                        }else{
                            $("tbody").children().eq(a).children().eq(b).removeClass("food");
                            a=Math.floor(Math.random()*height+1);
                            b=Math.floor(Math.random()*width+1);
                            while(true){
                                if(!$("tbody").children().eq(a).children().eq(b).hasClass("snake")){
                                $("tbody").children().eq(a).children().eq(b).addClass("food");
                                break;
                            }else{
                                a=Math.floor(Math.random()*height+1);
                                b=Math.floor(Math.random()*width+1);
                            }
                        }    
                        }
                    }                    
            }
        },150);

        $(document).on("keydown",function(){
            switch(event.keyCode){
                case 37:
                    if(snake.direction=="left"||snake.direction=="right"){
                        return;
                    }else{
                        snake.direction="left";
                    }
                    break;
                case 38:
                    if(snake.direction=="top"||snake.direction=="bottom"){
                        return;
                    }else{
                        snake.direction="top";
                    }
                    break;
                case 39:
                    if(snake.direction=="left"||snake.direction=="right"){
                        return;
                    }else{
                        snake.direction="right";
                    }
                    break;
                case 40:
                    if(snake.direction=="top"||snake.direction=="bottom"){
                        return;
                    }else{
                        snake.direction="bottom";
                    }
                    break;
            }
        })
        })


        
        
        function tab(height,width){
            for(var i=1;i<=height+2;i++){
                var tr=$("<tr></tr>");
                for(var j=1;j<=width+2;j++){
                    if(i==1||i==height+2){
                        $(tr).append("<td class='black bo'></td>");
                        continue;
                    }
                    if(j==1||j==width+2){
                        $(tr).append("<td class='black bo'></td>");
                        continue;
                    }
                    $(tr).append("<td class='bo'></td>");
                }
                $("#tbody").append(tr);                
            }
        }
    </script>
</body>
</html>

结束了,开始玩贪吃蛇吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值