读书人

【散分】花了一晚上写了个JavaScript小

发布时间: 2012-02-05 12:07:14 作者: rapoo

【散分】花了一晚上写了个JavaScript小游戏
在 ie7,火狐,谷歌 都测试通过。
源码:

HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head>    <title>wujinjian</title>    <script type="text/javascript">        //11 个单元格,每个单元格的大小就 等于 地图的大小(mapWH)/mapSize        var mapSize=11;         //地图的大小        var mapWH=440;          //记录对方的ID        var computerID;         //这个方向是否可走        var isPath=true;         //记录四方位上距离对方的距离        var up=0;        var left=0;        var right=0;        var down=0;        //障碍物的最多个数(可重叠)        var za=3;                window.onerror=function()        {            alert("异常!点击确定重新开始");            window.location.href=window.location.href;        };                function createMap()        {            var x=Math.round(Math.random()*(mapSize-1));  //行            var y=Math.round(Math.random()*(mapSize-1));  //列                        if(x==0)                x=x+1;            else if(x==(mapSize-1))                x=x-1;            if(y==0)                y=y+1;            else if(y==(mapSize-1))                y=y-1;                            //var x=7;            //var y=2;                        computerID=x+"_"+y;                        var tabobj=document.createElement("table");            tabobj.style.width=mapWH+"px";            tabobj.style.height=mapWH+"px";                        tabobj.border="1";                        var tbodyobj=document.createElement("tbody");                        for(var i=0;i<mapSize;i++)            {                var trobj=document.createElement("tr");                                for(var j=0;j<mapSize;j++)                {                    var tdobj=document.createElement("td");                    tdobj.style.border="rgb(128,128,255) solid 1px";                    tdobj.id=i+"_"+j                    tdobj.onclick=tdClick;                                        if(i+"_"+j==computerID)                    {                        tdobj.style.backgroundColor="red";                    }                                        var txt=document.createTextNode(" ");                    tdobj.appendChild(txt);                                        trobj.appendChild(tdobj);                }                                tbodyobj.appendChild(trobj);            }                        tabobj.appendChild(tbodyobj);                        document.getElementById("map_div").appendChild(tabobj);                        //默认随机障碍物            for(var i=0;i<za;i++)            {                var _id=Math.round(Math.random()*(mapSize-1)) +"_"+ Math.round(Math.random()*(mapSize-1));                if(document.getElementById(_id).style.backgroundColor=="")                    document.getElementById(_id).style.backgroundColor="gray";            }                        for(var i=0;i<mapSize;i++)            {                document.getElementById(i+"_"+(mapSize-1)).style.border="rgb(223,223,223) solid 1px";                document.getElementById((mapSize-1)+"_"+i).style.border="rgb(223,223,223) solid 1px";                document.getElementById(i+"_0").style.border="rgb(223,223,223) solid 1px";                document.getElementById("0_"+i).style.border="rgb(223,223,223) solid 1px";            }        }                function tdClick()        {            if(this.style.backgroundColor=="")            {                this.style.backgroundColor="gray";                                up=0;                left=0;                right=0;                down=0;                                computerXZ();            }          }                function computerXZ()        {            var xy=computerID.split("_");            var x=xy[0]-0;            var y=xy[1]-0;                        //中心位置            var mid=(mapSize-1)/2;                        //左上角            if(x<=mid && y<=mid)             {                 //向上                if(x<=y)                {                    //向上不通,向左走 //false 表示是判断,true 表示行走                    if(!computerUp(x,y,false))                     {                        //向左不通,向右走                        if(!computerLeft(x,y,false))                        {                            //向右不通,向下走                            if(!computerRight(x,y,false))                            {                                //向下不通,向下走(往最长的方向走)                                if(!computerDown(x,y,false))                                {                                                                        direction(up,left,right,down,x,y)                                }                            }                        }                      }                       }                else  //向左                {                   if(!computerLeft(x,y,false))                    {                        if(!computerUp(x,y,false))                        {                            if(!computerDown(x,y,false))                            {                                if(!computerRight(x,y,false))                                {                                    direction(up,left,right,down,x,y)                                }                            }                        }                      }                    }            }           //右上角            else if(x<=mid && y>=mid)            {                if(x<=(mapSize-1-y)) //向上                {                    if(!computerUp(x,y,false))                     {                        if(!computerRight(x,y,false))                        {                            if(!computerLeft(x,y,false))                            {                                if(!computerDown(x,y,false))                                {                                    direction(up,left,right,down,x,y)                                }                            }                        }                      }                       }                else  //向右                {                   if(!computerRight(x,y,false))                    {                        if(!computerUp(x,y,false))                        {                            if(!computerDown(x,y,false))                            {                                if(!computerLeft(x,y,false))                                {                                    direction(up,left,right,down,x,y)                                }                            }                        }                      }                    }            }            //右下角            else if(x>=mid && y>=mid)            {                if(x>=y) //向下                {                    if(!computerDown(x,y,false))                     {                        if(!computerRight(x,y,false))                        {                            if(!computerLeft(x,y,false))                            {                                if(!computerUp(x,y,false))                                {                                    direction(up,left,right,down,x,y)                                }                            }                        }                      }                       }                else  //向右                {                   if(!computerRight(x,y,false))                    {                        if(!computerDown(x,y,false))                        {                            if(!computerUp(x,y,false))                            {                                if(!computerLeft(x,y,false))                                {                                    direction(up,left,right,down,x,y)                                }                            }                        }                      }                    }            }            //左下角            else if(x>=mid && y<=mid)            {                if((mapSize-1-x)<=y) //向下                {                    if(!computerDown(x,y,false))                     {                        if(!computerLeft(x,y,false))                        {                            if(!computerRight(x,y,false))                            {                                if(!computerUp(x,y,false))                                {                                    direction(up,left,right,down,x,y)                                }                            }                        }                      }                       }                else  //向左                {                   if(!computerLeft(x,y,false))                    {                        if(!computerDown(x,y,false))                        {                            if(!computerUp(x,y,false))                            {                                if(!computerRight(x,y,false))                                {                                    direction(up,left,right,down,x,y)                                 }                            }                        }                      }                    }            }        } 



[解决办法]
支持,谢谢
[解决办法]

[解决办法]

[解决办法]
还好
和我见过的一个SWF 围小猫的游戏差不多
但是也还没他那么智能
[解决办法]
帮你推荐一下

[解决办法]
探讨
刚发现
今天正好是我注册 CSDN 一周年纪念日啊

[解决办法]
支持,学习JavaScript,感谢
[解决办法]
好长,好有心思啊
[解决办法]
这个估计很好玩,哈哈
[解决办法]
代码编写的不少嘛,游戏还行啊,支持一下。
[解决办法]
楼主厉害,支持!

读书人网 >JavaScript

热点推荐