Search code examples
c#winformsmaze

How do I prevent the player from moving through the walls in a maze? (C#)


enter image description here

RED SQUARE : PlayerPictureBox;

BLUE SQUARE : End Point;

BLACK SQUARE : WALL;

I have a maze organized in a PictureBox.

The player is an object of a certain size whose Picturebox is known. I want to move the player smoothly through the maze with the walls preventing them from going through.

Currently having issues with the player going through walls as shown in the picture.

   public partial class Form1 : Form
{
    private int XTILES = 25;
    private int YTILES = 25;
    private int TILESIZE = 10;
    private PictureBox[,] mazeTiles;
}

  public void CreateNewMaze()
    {
        mazeTiles = new PictureBox[XTILES, YTILES];
        for (int i = 0; i < XTILES; i++)
        {
            for (int j = 0; j < YTILES; j++)
            {
                mazeTiles[i, j] = new PictureBox();
                int xPosition = (i * TILESIZE) + 25;
                int yPosition = (j * TILESIZE) + 10;
                mazeTiles[i, j].SetBounds(xPosition, yPosition, TILESIZE, TILESIZE);

                this.Controls.Add(mazeTiles[i, j]);
            }
        }
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        bool isRoad = mazeTiles[(PlayerPictureBox.Left - 25) / TILESIZE, (PlayerPictureBox.Top - 10) / TILESIZE].BackColor != Color.Black;
       
        switch (keyData)
        {

            case Keys.Left:

                if (isRoad)
                    PlayerPictureBox.Left -= 10;                                                   
                return true;

            case Keys.Right:
                if (isRoad)
                    PlayerPictureBox.Left += 10;                                                                        
                return true;

            case Keys.Up:
                if (isRoad)
                    PlayerPictureBox.Top -= 10;                                      
                return true;

            case Keys.Down:
                if (isRoad)
                    PlayerPictureBox.Top += 10;  
        
           return true;
         }
           return base.ProcessCmdKey(ref msg, keyData);            
      }

Solution

  • You need check if the wall is in the direction of intended move. What you are doing now is checking the road at current position.

    Try something like this:

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            int dx = 0;
            int dy = 0;
            switch (keyData)
            {
                case Keys.Left: dx = -1; break;
                case Keys.Right: dx = 1; break;
                case Keys.Up: dy = -1; break;
                case Keys.Down: dy = 1; break;
                default: return base.ProcessCmdKey(ref msg, keyData);
            }
    
            var x = dx + (PlayerPictureBox.Left - 25) / TILESIZE;
            var y = dy + (PlayerPictureBox.Top - 10) / TILESIZE;
    
            bool isRoad = mazeTiles[x, y].BackColor != Color.Black;
            if (isRoad)
            {
                PlayerPictureBox.Left += dx * TILESIZE;
                PlayerPictureBox.Top += dy * TILESIZE;
            }
            return true;
        }