pacman collision detection

Status
Niet open voor verdere reacties.

jannemoon

Nieuwe gebruiker
Lid geworden
6 mrt 2011
Berichten
2
C# - pacman collision detection

hey ik moet samen met een klasgenoot pacman programmeren in csharp maar nu stuiten we op het probleem om onze pacman te laten stoppen bij muren . Iemand die ons opweg kan zetten? alvast bedankt

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
    }
 
Laatst bewerkt door een moderator:
Waarom gebruik je niet je _level1 value, om te checken of hij als hij naar boven indrukt op een 1 komt, dan kan het niet.
 
JE moet gewoon checke of je tegen een muur aanloopt als je current possitie + 1 doet,

Ik ruik hieraan dat dit niet jouw code is of wel? Sorry als het wel zo is.
 
Iets als:

[cpp]
const int MAP_WIDTH = 28
const int MAP_HEIGHT = 28
const int WALL = 1;

private bool IsValidMove(int x, int y) {
return (x < MAP_WIDTH && y < MAP_HEIGHT && _level1[x][y] != WALL);
}
// ...
if (IsValidMove(newX, newY)) {
// geldige zet
}
[/cpp]
 
Om het gemakkelijk te maken zal ik als ik jouw was dit doen, maak 2 editboxes, met daarin 2 getallen, start je spel. Ik neem aan dat jouw pacman in het midden begint, dus dan kan je die coordinated (0,0) geven en dan als je een omhoog gaat, (0,1), naar links, (-1,1), etc, dan maak je de walls neit met 0 en 1en, maar via coordinaten, krijg je nog mooiere code ook.
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan