一个迷宫游戏的开始-动态迷宫生成
关于迷宫的生成
View Code
namespace Silverlight_MazeGame { public class MazeGeneratorClass { private int _MAZE_WIDTH_GRID; public int MAZE_WIDTH_GRID { get { return _MAZE_WIDTH_GRID; } set { _MAZE_WIDTH_GRID = value; } } private int _MAZE_HIGHT_GRID; public int MAZE_HIGHT_GRID { get { return _MAZE_HIGHT_GRID; } set { _MAZE_HIGHT_GRID = value; } } private bool[,] _map; public bool[,] Map { get { return _map; } set { _map = value; } } public bool isInit = false; /// <summary> /// 返回从 1 - 2 * GridSize + 2; /// </summary> /// <param name="nWidth_D"></param> /// <param name="nHeight_D"></param> /// <returns></returns> public bool InitMap(ref int nWidth_D, ref int nHeight_D) { if (nWidth_D <= 2 || nHeight_D <= 2) { isInit = false; return false; } nWidth_D = nWidth_D % 2 != 0 ? nWidth_D - 1 : nWidth_D; nHeight_D = nHeight_D % 2 != 0 ? nHeight_D - 1 : nHeight_D; MAZE_WIDTH_GRID = nWidth_D / 2; MAZE_HIGHT_GRID = nHeight_D / 2; _map = new bool[MAZE_WIDTH_GRID * 2 + 3, MAZE_HIGHT_GRID * 2 + 3]; //x和y的值指定了这个要生成的迷宫的大小 Make_Maze(MAZE_WIDTH_GRID, MAZE_HIGHT_GRID); isInit = true; return true; } private Random rand = new Random();//随机数发生器初始化 private int[,] d = new int[,] { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; private void search(int x, int y) { int zx = x * 2, zy = y * 2, next, turn, i; _map[zx, zy] = true; turn = rand.Next() % 2 == 1 ? 1 : 3; for (i = 0, next = rand.Next() % 4; i < 4; i++, next = (next + turn) % 4) if (_map[zx + 2 * d[next, 0], zy + 2 * d[next, 1]] == false) { _map[zx + d[next, 0], zy + d[next, 1]] = true; search(x + d[next, 0], y + d[next, 1]); } return; } void Make_Maze(int x, int y) { int z1, z2; for (z1 = 0, z2 = 2 * y + 2; z1 <= 2 * x + 2; z1++) //数组最外围置1,表示无墙 _map[z1, 0] = _map[z1, z2] = true; for (z1 = 0, z2 = 2 * x + 2; z1 <= 2 * y + 2; z1++) _map[0, z1] = _map[z2, z1] = true; _map[2, 1] = _map[2 * x, 2 * y + 1] = true; //开口 search(rand.Next() % x + 1, rand.Next() % y + 1); } }
关于迷宫的调用
View Code
public partial class MainPage : UserControl { MazeGeneratorClass mazeGenerator = new MazeGeneratorClass(); Rectangle rect; int nGradSize = 40; public MainPage() { InitializeComponent(); } Random rand = new Random(); private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e) { updateMazeWindow(); } private void updateMazeWindow() { MazeRoot.Children.Clear(); double fWidth = this.ActualWidth; double fHeight = this.ActualHeight; int nGrads_Width = Convert.ToInt32(fWidth / nGradSize) - 1; int nGrads_Height = Convert.ToInt32(fHeight / nGradSize) - 1; mazeGenerator.InitMap(ref nGrads_Width, ref nGrads_Height); for (int j = 1; j <= nGrads_Height + 1; j++) for (int i = 1; i <= nGrads_Width + 1; i++ ){ { if (!mazeGenerator.Map[i,j]) //true 为空 false 为Wall { rect = new Rectangle() { Width = nGradSize, Height = nGradSize, Stretch = Stretch.Fill, Fill = new ImageBrush() { Stretch = Stretch.Fill, ImageSource = new BitmapImage(new Uri(@"/Silverlight_MazeGame;component/Res/wall.png", UriKind.Relative)) }}; } else if (rand.Next() % 2 == 0) { rect = new Rectangle() { Width = nGradSize, Height = nGradSize, Stretch = Stretch.Fill, Fill = new ImageBrush() { Stretch = Stretch.Fill, ImageSource = new BitmapImage(new Uri(@"/Silverlight_MazeGame;component/Res/草地.png", UriKind.Relative)) } }; } if (rect != null) { MazeRoot.Children.Add(rect); Canvas.SetLeft(rect, (i - 1) * nGradSize); Canvas.SetTop(rect, (j - 1) * nGradSize); rect = null; } if (i == nGrads_Width && j == nGrads_Height + 1 /*|| mazeGenerator.Map[i, 2 * nGrads_Width + 1]*/) { TransferPoint_ po = new TransferPoint_() { Width = nGradSize, Height = nGradSize }; MazeRoot.Children.Add(po); Canvas.SetLeft(po, (i - 1) * nGradSize + nGradSize /2); Canvas.SetTop(po, (j - 1) * nGradSize+ nGradSize); Canvas.SetZIndex(po, 1000); po.ANI_Show.Begin(); } } } } private void UserControl_Loaded(object sender, RoutedEventArgs e) { Canvas.SetZIndex(button_MazeGenerator, 1000); } private void button_MazeGenerator_Click(object sender, RoutedEventArgs e) { updateMazeWindow(); } }