-1

I am making a simple Pacman game using c# in Visual studio. I decided to make a rectangles of a maze in which the main character can walk. I have a rectangle which covers main character's bounds:

Rectangle _pacmanBound = new Rectangle(Left, Top, 28, 28);

Then I take the 4 points of this rectangle (top left, bottom left, top right and bottom right). I also have a List with rectangle areas in which pacman can walk:

map = new List<Rectangle>();

map.Add(new Rectangle(12, 375, 430, 28)); 
map.Add(new Rectangle(12, 403, 28, 97)); 
map.Add(new Rectangle(12, 470, 430, 28));

Now I want to check if all these 4 points belong to any of the rectangles in a list (so it indicates that the area is walkable):

foreach (Rectangle r in _maze.map)
{
    if (r.Contains(_pacmanBound.X, _pacmanBound.Y))
    {
        topLeft = true;

    }
    else if (r.Contains(_pacmanBound.X, _pacmanBound.Y + _pacmanBound.Height))
    {
        bottomLeft = true;    
    } 
    else if (r.Contains(_pacmanBound.X + _pacmanBound.Width, _pacmanBound.Y))
    {
        topRight= true;
    } 
    else if (r.Contains(_pacmanBound.X + _pacmanBound.Width, _pacmanBound.Y + _pacmanBound.Height))
    {
        bottomRight = true;
    }
}

After loop the first one is true, second - false, third - false and fourth = false

To let you better understand, I add this picture: http://i.imgur.com/hvLKNOy.png

Any help is appreciated.

EtherPaul
  • 424
  • 10
  • 19

1 Answers1

1

Your problem is that you're using else clause. So, if the first if clause is true, the program will not check other if clauses. You need to remove else clause:

foreach (Rectangle r in _maze.map)
{
            if (r.Contains(_pacmanBound.X, _pacmanBound.Y))
            {
                topLeft = true;

            }
            if (r.Contains(_pacmanBound.X, _pacmanBound.Y + _pacmanBound.Height))
            {
                bottomLeft = true;

            }
            if (r.Contains(_pacmanBound.X + _pacmanBound.Width, _pacmanBound.Y))
            {
                topRight= true;

            }
            if (r.Contains(_pacmanBound.X + _pacmanBound.Width, _pacmanBound.Y + _pacmanBound.Height))
            {
                bottomRight = true;
            }
}

Also, you don't have to check each point of pacman rectangle. You can just check, if map rectangle contains pacman rectangle:

foreach (Rectangle r in _maze.map)
{
    if (r.Contains(_pacmanBound))
    {
        isPacmanInsideMaze = true;
        break;
    }
}
Paviel Kraskoŭski
  • 1,429
  • 9
  • 16
  • Thank you, such a silly mistake. Hovewer, your suggestion about checking the whole rectangle is bad cause my map consists of more than 1 rectangles so for example when pacman needs to continue moving from one red block to another, it means that first bloks contains pacman but second only intersects with it. That will return false... – EtherPaul Oct 24 '16 at 06:50
  • @HonorLT So, maybe you want to use `Rectangle.IntersectsWith(Rectangle)` method? – Paviel Kraskoŭski Oct 24 '16 at 07:47
  • it also doesnt work because then some points of pacman's rectangle may be out of bounds – EtherPaul Oct 24 '16 at 08:02
  • @HonorLT So the only one solution is to check all corners of pacman's bounds. – Paviel Kraskoŭski Oct 24 '16 at 16:23