Thread: pacman collision detection 2D Array

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    pacman collision detection 2D Array

    i have to make pacman for school but now i am stuck because i don't have a clue how the collision detection works. can someone get me on my way? thanks in advance

    Code:
    namespace WindowsFormsApplication1
    {
       
        public partial class frmMain : Form
        {
            private Graphics paper;
            private Image imgWall, imgDot;
            private Pacman pacman;
            private Dots dots;
            int a;
            
            
    
            private const int breedte = 15;
            private int[,] _Level1;
            private int position = 2;
            
            public frmMain()
            {
                InitializeComponent();
                paper = pictureBox1.CreateGraphics();
                pacman = new Pacman(pictureBox1);
                dots = new Dots(pictureBox1);
                
            }
    
            private void frmMain_Load(object sender, EventArgs e)
            {
                
                const int iMapWidth = 28;
                const int iMapHeight = 28;
    
                _Level1 = new int[iMapWidth,iMapHeight]{
                {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1},
                {1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1},
                {1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,0,1},
                {1,0,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,0,1},
                {1,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,1},
                {1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1},
                {1,1,1,1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,1,1,1},
                {1,1,1,1,1,1,0,1,1,0,1,1,1,2,2,1,1,1,0,1,1,0,1,1,1,1,1,1},
                {1,1,1,1,1,1,0,0,0,0,1,1,2,2,2,2,1,1,0,0,0,0,1,1,1,1,1,1},
                {1,1,1,1,1,1,0,1,1,0,1,1,2,2,2,2,1,1,0,1,1,0,1,1,1,1,1,1},
                {1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1},
                {1,1,1,1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,1,1,1},
                {1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1},
                {1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1},
                {1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1},
                {1,1,1,0,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,0,1,1,1},
                {1,1,1,0,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,0,1,1,1},
                {1,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,1},
                {1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1},
                {1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};      
            }
    
            
    	    
    
            private void timer_Tick(object sender, EventArgs e)
            {
                paper.Clear(Color.Black);
                drawPacman();
                positionPacman();
                drawLevel();
                
            }
    
            private void drawLevel()
            {
                Brush potloodWall = new SolidBrush(Color.Gray);
                Brush potloodPath = new SolidBrush(Color.Black);
                imgWall = Image.FromFile("Sprites/Sprite_wall.png");
                imgDot = Image.FromFile("Sprites/dot.png");
                int x = 15;
                int y = 15;
    
                //  walls [i,j]   
    
                //.GetLength(0) => eerste dimensie (hoogte)
                //.Getlength(1) => tweede dimensie (breedte)
    
                for (int i = 0; i < _Level1.GetLength(0); i++)
                {
                    // DOE DIT ZOLANG "i" KLEINER IS DAN "3"
    
                    for (int j = 0; j < _Level1.GetLength(1); j++)
                    {
                        // DOE DIT ZOLANG "J" KLEINER IS DAN "3"
    
                        if (_Level1[i, j] == 0)   // als inhoud array [i,j] = 0 {teken path}
                        {
                            paper.DrawImage(imgDot, x, y, 15, 15);
                            x += 15;
                        }
    
                        if (_Level1[i, j] == 1)  // als inhoud array [i,j] != 0 {teken wall}
                        {
                            paper.DrawImage(imgWall, x, y, 15, 15);
                            x += 15;
                        }
    
                        if (_Level1[i, j] == 2)
                        {
                            paper.FillRectangle(potloodPath, x, y, 15, 15);
                            x += 15;
                        }
    
                        if (x > (breedte * _Level1.GetLength(1)))          // Begint nieuwe regel
                        {
                            y += 15;
                            x = 15;
                        }
                    }
                }
            }
    
            private void drawPacman()
            {
                pacman.Display(paper);
            }
    
            private void drawDot()
            {
                dots.Display(paper);
            }
    
            #region move-event
            private void frmMain_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Right)
                {
                    position = 1;
                    if (a == _Level1.GetLength(1))
                    {
                        pacman.MoveLeft();
                    }
                    else
                    {
                        pacman.MoveRight();
                    }
                }
    
                if (e.KeyCode == Keys.Left)
                {
                    position = 2;
                    if (a == _Level1.GetLength(1))
                    {
                        pacman.MoveRight();
                    }
                    else
                    {
                        pacman.MoveLeft();
                    }
                }
    
                if (e.KeyCode == Keys.Up)
                {
                    position = 3;
                    if (a == _Level1.GetLength(1))
                    {
                        pacman.MoveDown();
                    }
                    else
                    {
                        pacman.MoveUp();
                    }
                }
    
                if (e.KeyCode == Keys.Down)
                {
                    position = 4;
                    if (a == _Level1.GetLength(1))
                    {
                        pacman.MoveUp();
                    }
                    else
                    {
                        pacman.MoveDown();
                    }
                }
            }
    
            public void positionPacman()
            {
                if (position == 1)
                {
                    pacman.pacman_right = Image.FromFile("Sprites/pacman_rechts.png");
                }
                if (position == 2)
                {
                    pacman.pacman_left = Image.FromFile("Sprites/pacman_links.png");
                }
                if (position == 3)
                {
                    pacman.pacman_up = Image.FromFile("Sprites/pacman_boven.png");
                }
                if (position == 4)
                {
                    pacman.pacman_down = Image.FromFile("Sprites/pacman_onder.png");
                }
            }
            #endregion
    
        }

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    When a ghost lands on the pac-man cell and pac-man hasn't eaten a power pill then I would say pac-man is most assuredly dead.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    ^^

    very helpfull =S

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You could get away with simple collision detection for a pacman game. Just check to see if the rectangles for the objects intersect with each other.

    e.g.:
    Code:
    foreach(Ghost ghost in ghostList)
      if(ghost.Rectangle.IntersectsWith(pacman.Rectangle))
        pacman.Die();
    I'm assuming you have a rectangle for your sprites. If not, add a property like:
    Code:
      public Rectangle Rectangle { get { return new Rectangle(Location.X, Location.Y, Bitmap.Width, Bitmap.Height); } }
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  2. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM
  3. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM