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

posted @ 2011-10-11 14:22  海底的鱼  阅读(426)  评论(0编辑  收藏  举报