Silverlight中实现人物的A Star寻径

        Rectangle rect;
         private IPathFinder PathFinder =  null;
         private  byte[,] Matrix =  new  byte[ 10241024];  // 寻路用二维矩阵
         private  int GridSize =  20// 单位格子大小
        Point Start = new Point( 0, 0);  // 移动起点坐标
        Point End;  // 移动终点坐标  

构造初始化矩阵

  

privatevoid InitializeMatrix()
        {
             for ( int y =  0; y < Matrix.GetUpperBound( 1); y++)
            {
                 for ( int x =  0; x < Matrix.GetUpperBound( 0); x++)
                {
                     // 默认值可以通过(非障碍物)在矩阵中用1表示
                    Matrix[x, y] =  1;
                }
            }
             // 构建障碍物(举例)
             for ( int i =  0; i <  18; i++)
            {
                 // 障碍物在矩阵中用0表示
                Matrix[i,  12] =  0;
                rect =  new Rectangle();
                rect.Fill =  new SolidColorBrush(Colors.Red);
                rect.Width = GridSize;
                rect.Height = GridSize;
                Carrier.Children.Add(rect);
                Canvas.SetLeft(rect, i * GridSize);
                Canvas.SetTop(rect,  12 * GridSize);
            }

             for ( int i =  12; i <  20; i++)
            {
                 // 障碍物在矩阵中用0表示
                Matrix[ 18, i] =  0;
                rect =  new Rectangle();
                rect.Fill =  new SolidColorBrush(Colors.Red);
                rect.Width = GridSize;
                rect.Height = GridSize;
                Carrier.Children.Add(rect);
                Canvas.SetLeft(rect,  18 * GridSize);
                Canvas.SetTop(rect, i * GridSize);
            }

             for ( int i =  12; i <  19; i++)
            {
                 // 障碍物在矩阵中用0表示
                Matrix[i,  20] =  0;
                rect =  new Rectangle();
                rect.Fill =  new SolidColorBrush(Colors.Red);
                rect.Width = GridSize;
                rect.Height = GridSize;
                Carrier.Children.Add(rect);
                Canvas.SetLeft(rect, i * GridSize);
                Canvas.SetTop(rect,  20 * GridSize);
       }             

} 

 

 

寻径方式调用:一般我们通过鼠标左击事件来确定鼠标位置

 

private  void Carrier_MouseLeftButtonDown( object sender, MouseButtonEventArgs e)
        {
            Point p = e.GetPosition(Carrier);

             int x = ( int)p.X / GridSize;
             int y = ( int)p.Y / GridSize;
            End =  new Point(x, y);  // 计算终点坐标

            PathFinder =  new PathFinderFast(Matrix);
            PathFinder.HeavyDiagonals =  true// 深度对角线移动
            
// PathFinder.Diagonals = true; // 对角线
            PathFinder.Formula = HeuristicFormula.Manhattan;  // 使用我个人觉得最快的曼哈顿A*算法
            PathFinder.SearchLimit =  2000// 寻径范围,即在多大的范围内寻找

            List<PathFinderNode> path = PathFinder.FindPath(Start, End);  // 开始寻径

             if (path ==  null)
            {
                MessageBox.Show( " 路径不存在! ");
            }
             else
            {
                 string output =  string.Empty;
                 for ( int i = path.Count -  1; i >=  0; i--)
                {
                    output =  string.Format(output
                        +  " {0} "
                        + path[i].X.ToString()
                        +  " {1} "
                        + path[i].Y.ToString()
                        +  " {2} ",
                         " ( "" , "" ");
                    rect =  new Rectangle();
                    rect.Fill =  new SolidColorBrush(Colors.Green);
                    rect.Width = GridSize;
                    rect.Height = GridSize;
                    Carrier.Children.Add(rect);
                    Canvas.SetLeft(rect, path[i].X * GridSize);
                    Canvas.SetTop(rect, path[i].Y * GridSize);
                }
                MessageBox.Show( " 路径坐标分别为: " + output);
            }

 } 

 

 当然这里还要调用最主要的寻径的dll ,这个是寻径的精华,附上源码/Files/Caceolod/SilverGame.zip

转载于:https://www.cnblogs.com/Caceolod/archive/2011/10/11/2207283.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值