日期:2014-05-16  浏览次数:21411 次

【散分】花了一晚上写了个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=="&qu