void NewGame(float width, float height) { // The constructor creates the random maze layout mazeGrid = new MazeGrid(MAZE_HORZ_CHAMBERS, MAZE_VERT_CHAMBERS); // Initialize borders collection borders.Clear(); // Create Line2D objects for the border lines of the maze float cellWidth = width / mazeGrid.Width; float cellHeight = height / mazeGrid.Height; int halfWallWidth = WALL_WIDTH / 2; for (int x = 0; x < mazeGrid.Width; x++) { for (int y = 0; y < mazeGrid.Height; y++) { MazeCell mazeCell = mazeGrid.Cells[x, y]; Vector2 ll = new Vector2(x * cellWidth, (y + 1) * cellHeight); Vector2 ul = new Vector2(x * cellWidth, y * cellHeight); Vector2 ur = new Vector2((x + 1) * cellWidth, y * cellHeight); Vector2 lr = new Vector2((x + 1) * cellWidth, (y + 1) * cellHeight); Vector2 right = halfWallWidth * Vector2.UnitX; Vector2 left = -right; Vector2 down = halfWallWidth * Vector2.UnitY; Vector2 up = -down; if (mazeCell.HasLeft) { borders.Add(new Line2D(ll + down, ll + down + right)); borders.Add(new Line2D(ll + down + right, ul + up + right)); borders.Add(new Line2D(ul + up + right, ul + up)); } if (mazeCell.HasTop) { borders.Add(new Line2D(ul + left, ul + left + down)); borders.Add(new Line2D(ul + left + down, ur + right + down)); borders.Add(new Line2D(ur + right + down, ur + right)); } if (mazeCell.HasRight) { borders.Add(new Line2D(ur + up, ur + up + left)); borders.Add(new Line2D(ur + up + left, lr + down + left)); borders.Add(new Line2D(lr + down + left, lr + down)); } if (mazeCell.HasBottom) { borders.Add(new Line2D(lr + right, lr + right + up)); borders.Add(new Line2D(lr + right + up, ll + left + up)); borders.Add(new Line2D(ll + left + up, ll + left)); } } } // Prepare AbsoluteLayout for new children absoluteLayout.Children.Clear(); // "Draw" the walls of the maze using BoxView BoxView createBoxView() => new BoxView { Color = Color.Green }; for (int x = 0; x < mazeGrid.Width; x++) { for (int y = 0; y < mazeGrid.Height; y++) { MazeCell mazeCell = mazeGrid.Cells[x, y]; if (mazeCell.HasLeft) { Rectangle rect = new Rectangle(x * cellWidth, y * cellHeight - halfWallWidth, halfWallWidth, cellHeight + WALL_WIDTH); absoluteLayout.Children.Add(createBoxView(), rect); } if (mazeCell.HasRight) { Rectangle rect = new Rectangle((x + 1) * cellWidth - halfWallWidth, y * cellHeight - halfWallWidth, halfWallWidth, cellHeight + WALL_WIDTH); absoluteLayout.Children.Add(createBoxView(), rect); } if (mazeCell.HasTop) { Rectangle rect = new Rectangle(x * cellWidth - halfWallWidth, y * cellHeight, cellWidth + WALL_WIDTH, halfWallWidth); absoluteLayout.Children.Add(createBoxView(), rect); } if (mazeCell.HasBottom) { Rectangle rect = new Rectangle(x * cellWidth - halfWallWidth, (y + 1) * cellHeight - halfWallWidth, cellWidth + WALL_WIDTH, halfWallWidth); absoluteLayout.Children.Add(createBoxView(), rect); } } } // Randomly position ball in one of the corners bool isBallLeftCorner = random.Next(2) == 0; bool isBallTopCorner = random.Next(2) == 0; // Create the hole first (so Z order is under the ball) // and position it in the opposite corner from the ball hole = new EllipseView { Color = Color.Black, WidthRequest = 2 * HOLE_RADIUS, HeightRequest = 2 * HOLE_RADIUS }; holePosition = new Vector2((isBallLeftCorner ? 2 * mazeGrid.Width - 1 : 1) * (width / mazeGrid.Width / 2), (isBallTopCorner ? 2 * mazeGrid.Height - 1 : 1) * (height / mazeGrid.Height / 2)); absoluteLayout.Children.Add(hole, new Point(holePosition.X - HOLE_RADIUS, holePosition.Y - HOLE_RADIUS)); // Create the ball and set initial position ball = new EllipseView { Color = Color.Red, WidthRequest = 2 * BALL_RADIUS, HeightRequest = 2 * BALL_RADIUS }; ballPosition = new Vector2((isBallLeftCorner ? 1 : 2 * mazeGrid.Width - 1) * (width / mazeGrid.Width / 2), (isBallTopCorner ? 1 : 2 * mazeGrid.Height - 1) * (height / mazeGrid.Height / 2)); absoluteLayout.Children.Add(ball, new Point(ballPosition.X - BALL_RADIUS, ballPosition.Y - BALL_RADIUS)); }
protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); viewport = this.GraphicsDevice.Viewport; // Create texture for the walls of the maze tinyTexture = new Texture2D(this.GraphicsDevice, 1, 1); tinyTexture.SetData <Color>(new Color[] { Color.White }); // Create ball ball = Texture2DExtensions.CreateBall(this.GraphicsDevice, BALL_RADIUS * BALL_SCALE); ballCenter = new Vector2(ball.Width / 2, ball.Height / 2); ballPosition = new Vector2((viewport.Width / mazeGrid.Width) / 2, (viewport.Height / mazeGrid.Height) / 2); // Initialize borders collection borders.Clear(); // Create Line2D objects for walls of the maze int cellWidth = viewport.Width / mazeGrid.Width; int cellHeight = viewport.Height / mazeGrid.Height; int halfWallWidth = WALL_WIDTH / 2; for (int x = 0; x < mazeGrid.Width; x++) { for (int y = 0; y < mazeGrid.Height; y++) { MazeCell mazeCell = mazeGrid.Cells[x, y]; Vector2 ll = new Vector2(x * cellWidth, (y + 1) * cellHeight); Vector2 ul = new Vector2(x * cellWidth, y * cellHeight); Vector2 ur = new Vector2((x + 1) * cellWidth, y * cellHeight); Vector2 lr = new Vector2((x + 1) * cellWidth, (y + 1) * cellHeight); Vector2 right = halfWallWidth * Vector2.UnitX; Vector2 left = -right; Vector2 down = halfWallWidth * Vector2.UnitY; Vector2 up = -down; if (mazeCell.HasLeft) { borders.Add(new Line2D(ll + down, ll + down + right)); borders.Add(new Line2D(ll + down + right, ul + up + right)); borders.Add(new Line2D(ul + up + right, ul + up)); } if (mazeCell.HasTop) { borders.Add(new Line2D(ul + left, ul + left + down)); borders.Add(new Line2D(ul + left + down, ur + right + down)); borders.Add(new Line2D(ur + right + down, ur + right)); } if (mazeCell.HasRight) { borders.Add(new Line2D(ur + up, ur + up + left)); borders.Add(new Line2D(ur + up + left, lr + down + left)); borders.Add(new Line2D(lr + down + left, lr + down)); } if (mazeCell.HasBottom) { borders.Add(new Line2D(lr + right, lr + right + up)); borders.Add(new Line2D(lr + right + up, ll + left + up)); borders.Add(new Line2D(ll + left + up, ll + left)); } } } }