JS/HTML5游戏常用算法之路径搜索算法A *寻路算法完整实例

  

本文实例讲述了JS/HTML5游戏常用算法之路径搜索算法A *寻路算法。分享给大家供大家参考,具体如下:

  

原理可参考:https://www.jb51.net/article/152744.htm

  

完整实例代码如下:

        & lt; !DOCTYPE html>   & lt; html lang=癳n”比;   & lt; head>   & lt;元name=笆哟啊蹦谌?翱矶?设备宽度,初始=1.0,最大范围=1.0,user-scalable=0”的在   & lt;元charset=皍tf - 8”比;   & lt; title> A *寻路算法& lt;/title>   & lt; style>   #{阶段   边界:1 px固体浅灰色;   }   & lt;/style>   & lt;/head>   & lt; body>   & lt;帆布id=敖锥巍弊4? lt;/canvas>   & lt;/body>   & lt; script>   窗口。onload=function () {   var=document.querySelector阶段(“#阶段”),   ctx=stage.getContext (2 d);   阶段。宽度=600;   阶段。身高=600;   var=7行,列=7,r=40;//取区域随机数x>=分钟,,x<马克斯   函数randInt(最小,最大){   max=max | | 0;   min=分钟| | 0;   var=步数学。abs(最大值-最小值);   var圣=(参数。长度& lt;2)& # 63;0:min;//参数只有一个的时候,圣=0;   var的结果;   结果=圣+ (Math.ceil (math . random() *步骤))- 1;   返回结果;   }//普里姆算法生成连通图的二维数组行行列列   函数primMaze (r、c) {//初始化数组   init函数(r, c) {   var=新数组(2 * r + 1);//全部置1   (让我=0,len=a.length;我& lt;兰;我+ +){   var关口=2 * c + 1;   [我]=new Array(关口);   (让j=0, len1=[我]. length;j & lt;len1;j + +) {   [我][j]=1;   }   }//中间格子为0   (让我=0;我& lt;r;我+ +)   (让j=0;j & lt;c;j + +) {   (2 * i + 1) (2 * j + 1)=0;   }   返回一个;   }//处理数组,产生最终的数组   函数过程(arr) {//acc存放已访问队列,noacc存放没有访问队列   var acc=[], noacc=[];   var r=加勒比海盗。,长度比的在1,c=arr [0]。,长度比的在1;   var count=r * c;   (var=0;我& lt;计数;我+ +){   noacc[我]=0;   }//定义空单元上下左右偏移   var偏移=(- c, c, 1, 1) offR=(1, - 1, 0, 0), offC=(0, 0, 1, 1);//随机从noacc取出一个位置   var pos=randInt(数);   noacc (pos)=1;   acc.push (pos);   而(acc。长度& lt;数){   var ls=1, offPos=1;   offPos=1;//找出pos位置在二维数组中的坐标   var公关=pos/c | 0, pc=pos % c=0, o=0;//随机取上下左右四个单元   而(+ +有限公司& lt;5){   o=randInt (0 5);   ls=杀死[o] + pos;   var tpr=公关+ offR [o];   var tpc=pc + offC [o];   如果(tpr祝辞=0,,tpc祝辞=0,,tpr & lt;=r - 1,,tpc & lt;=c - 1,,noacc (ls)==0) {   offPos=o;   打破;   }   }   如果(offPos & lt;0){   pos=acc [randInt (acc.length)];   }   其他{   公关=2 *公关+ 1;   电脑=2 * pc + 1;//相邻空单元中间的位置置0   加勒比海盗[公关+ offR [offPos]] [pc + offC [offPos]]=0;   pos=ls;   noacc (pos)=1;   acc.push (pos);   }   }   }   var=init (r、c);   过程(a);   返回一个;//返回一个二维数组,行的数据为2 r + 1个,列的数据为2 c + 1个   }//栅格线条   函数drawGrid(上下文,颜色、stepx stepy) {   上下文。strokeStyle=颜色;   上下文。线宽=0.5;   我对(var=stepx + 0.5;我& lt;context.canvas.width;我+=stepx) {   context.beginPath ();   上下文。函数(0);   上下文。画线(我context.canvas.height);   context.stroke ();   }   我对(var=stepy + 0.5;我& lt;context.canvas.height;我+=stepy) {   context.beginPath ();   上下文。函数(0,1);   context.lineTo (context.canvas。宽度,我);   context.stroke ();   }   }//方块创造方法   函数createRect (x, y, r, c) {   ctx.beginPath ();   ctx。fillStyle=c;   ctx。矩形(x, y, r, r);   ctx.fill ();   }//定义点对象【*点对象】   功能点(x, y) {   这一点。x=x;   这一点。y=y;   这一点。父母=零;   这一点。f=0;   这一点。g=0;   这一点。h=0;//当前点状态,0:表示在openlist 1:表示closelist, 1表示还没处理   这一点。国家=1;//标记表明该点是否可通过   这一点。国旗=0;   }//把普通二维数组(全部由1 0表示)的转换成一个*所需要的点数组   函数convertArrToAS (arr) {   var r=加勒比海盗。长度,c=arr [0] . length;   var=new Array (r);   (var=0;我& lt;r;我+ +){   [我]=新数组(c);   (var j=0;j & lt;c;j + +) {   var pos=new点(i, j);   pos.flag=arr[我][j];   [我][j]=pos;   }   }   返回一个;   }//*算法,pathArr表示最后返回的路径   函数findPathA (pathArr,开始,结束,行,上校){//添加数据到排序数组中   函数addArrSort (descSortedArr,元素,比较){   var左=0;   var=descSortedArr。长度- 1;   var=(左+右)中期祝辞祝辞1;   而(左& lt;=右){   var=(左+右)中期祝辞祝辞1;   如果(比较(descSortedArr(中期),元素)==1){   左=中期+ 1;   }   else if(比较(descSortedArr(中期),元素)==1){   对中期=1;   }   其他{   打破;   }   }   我(var=descSortedArr。长度- 1;我在=左;我(){   descSortedArr (i + 1)=descSortedArr[我];   }   descSortedArr(左)=元素;   }//判断两个点是否相同   函数pEqual (p1, p2) {   返回p1。x==p2。x和,p1。y==p2.y;   }//获取两个点距离,采用曼哈顿方法   pos1函数posDist (pos) {   (Math.abs (pos1返回。x - pos.x) + Math.abs (pos1。y - pos.y));   }   函数之间(val,最小,最大){   返回(val祝辞=分钟,,val & lt;=max)   }//比较两个点f值大小   函数compPointF (pt2 pt1) {   pt1返回。f - pt2.f;   }//处理当前节点   函数processCurrpoint (arr openList,行,坳,currPoint destPoint) {//起床,下,左,右直接   var第=currPoint。x - 1;   var lty=currPoint。y - 1;   (var=0;我& lt;3;我+ +){   (var j=0;j & lt;3;j + +) {   var残雪=第+我;   var cy=lty + j;   如果((cx===currPoint。x | | cy===currPoint.y),,(第0行- 1),,之间(lty 0坳- 1)){   var tp=arr (cx) (cy);   如果(tp。标志===0,,tp。状态!==1){   如果(pEqual (tp, destPoint)) {   tp。父母=currPoint;   返回true;   }   如果(tp。状态===1){   tp。父母=currPoint;   tp。g=1 + currPoint.g;   tp。h=posDist (tp, destPoint);   tp。f=tp。h + tp.f;   tp。国家=0;   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

JS/HTML5游戏常用算法之路径搜索算法A *寻路算法完整实例