help with pacman c#

Discussion in 'C#' started by jannemoon, Mar 9, 2011.

  1. jannemoon

    jannemoon New Member

    Joined:
    Mar 9, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Me and a friend have to make pacman in csharp for school. And we're stuck.
    We got some kind of code for the collision of pacman, although it's not the thing we want, because with this code pacman has to start in the left corner. So if anyone wants to help us with this, he would be greatful.

    Code:
    int a = 1;
    int b = 1;
    
    private void frmMain_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Right)
                {
                    position = 1;
                    if (b + 1 <= _Level1.GetLength(0))
                    {
                        if (_Level1[a, b + 1] == 0)
                        {
                            pacman.MoveRight();
                            b++;
                        }
                    }
                }
    
                if (e.KeyCode == Keys.Left)
                {
                    position = 2;
                    if (b - 1 >= 0)
                    {
                        if (_Level1[a, b - 1] == 0)
                        {
                            pacman.MoveLeft();
                            b--;
                        }
                    }
                }
    
                if (e.KeyCode == Keys.Up)
                {
                    position = 3;
                    if (a - 1 >= 0)
                    {
                        if (_Level1[a - 1, b] == 0)
                        {
                            pacman.MoveUp();
                            a--;
                        }
                    }
                }
    
                if (e.KeyCode == Keys.Down)
                {
                    position = 4;
                    if (a + 1 <= _Level1.GetLength(0))
                    {
                        if (_Level1[a + 1, b] == 0)
                        {
                            pacman.MoveDown();
                            a++;
                        }
                    }
                }
            }
    
    _Level1 is the name of our array, which is 28x28.
    In our array a '0' stands for a walkable tile.

    We were thinking about getting the value of the array and then checking if it is a walkable tile or not, but we don't have any clue how to do it.

    Another problem we have with letting our monsters walk randomly without getting through the walls. We got the right code, atleast we think we have, because it works when we apply it to 1 monster. If we try to use the code on 4 monsters they start walking through the walls. And again, with this code the monsters have to start in the top left corner.

    Code:
    private int _monsterMove1, _monsterMove2, _monsterMove3, _monsterMove4;
    int c = 1;
    int d = 1;
    private bool noLeft = false;
    private bool noRight = false;
    private bool noUp = false;
    private bool noDown = false;
    
    private void monsterMove()
            { 
                Random monsterMove1 = new Random();
                _monsterMove1 = monsterMove1.Next(1, 5);
                Random monsterMove2 = new Random();
                _monsterMove2 = monsterMove2.Next(6, 10);
                Random monsterMove3 = new Random();
                _monsterMove3 = monsterMove3.Next(11, 15);
                Random monsterMove4 = new Random();
                _monsterMove4 = monsterMove4.Next(16, 20);
            }
    
    private void monsterBlock()
            {
                monsterMove();
                if (noRight)
                {
                    if (_monsterMove1 == 1)
                    {
                        _monsterMove1 = 2;
                        noRight = false;
                    }
    
                    if (_monsterMove2 == 6)
                    {
                        _monsterMove2 = 7;
                        noRight = false;
                    }
    
                    if (_monsterMove3 == 11)
                    {
                        _monsterMove3 = 12;
                        noRight = false;
                    }
    
                    if (_monsterMove4 == 16)
                    {
                        _monsterMove4 = 17;
                        noRight = false;
                    }
                }
                else
                {                
                    if (noLeft)
                    {
                        if (_monsterMove1 == 2)
                        {
                            _monsterMove1 = 3;
                            noLeft = false;
                        }
    
                        if (_monsterMove2 == 7)
                        {
                            _monsterMove2 = 8;
                            noLeft = false;
                        }
    
                        if (_monsterMove3 == 12)
                        {
                            _monsterMove3 = 13;
                            noLeft = false;
                        }
    
                        if (_monsterMove4 == 17)
                        {
                            _monsterMove4 = 18;
                            noLeft = false;
                        }
                    }
                    else
                    {
                        if (noUp)
                        { 
                            if (_monsterMove1 == 3)
                            {
                                _monsterMove1 = 4;
                                 noUp = false;
                            }
    
                            if (_monsterMove2 == 8)
                            {
                                _monsterMove2 = 9;
                                noUp = false;
                            }
    
                            if (_monsterMove3 == 13)
                            {
                                _monsterMove3 = 14;
                                noUp = false;
                            }
    
                            if (_monsterMove4 == 18)
                            {
                                _monsterMove4 = 19;
                                noUp = false;
                            }
                        }
                        else
                        {
                            if (noDown)
                            {
                                if (_monsterMove1 == 4)
                                {
                                    _monsterMove1 = 1;
                                    noDown = false;
                                }
    
                                if (_monsterMove2 == 9)
                                {
                                    _monsterMove2 = 6;
                                    noDown = false;
                                }
    
                                if (_monsterMove3 == 14)
                                {
                                    _monsterMove3 = 11;
                                    noDown = false;
                                }
    
                                if (_monsterMove4 == 19)
                                {
                                    _monsterMove4 = 16;
                                    noDown = false;
                                }
                            }
                        }
                    }
                }
                StepRight();
                StepLeft();
                StepDown();
                StepUp();
            }
    
            private void StepRight()
            {
                if (_monsterMove1 == 1 && _Level1[c, d - 1] == 0)
                {
                    monster1.MoveRight();
                    d--;
                }
    
                if (_monsterMove2 == 6 && _Level1[c, d + 1] == 0)
                {
                    monster2.MoveRight();
                    d++;
                }
    
                if (_monsterMove3 == 11 && _Level1[c, d + 1] == 0)
                {
                    monster3.MoveRight();
                    d++;
                }
    
                if (_monsterMove4 == 16 && _Level1[c, d + 1] == 0)
                {
                    monster4.MoveRight();
                    d++;
                }
            }
            private void StepLeft()
            {
                if (_monsterMove1 == 2 && _Level1[c, d - 1] == 0)
                {
                    monster1.MoveLeft();
                    d--;
                }
    
                if (_monsterMove2 == 7 && _Level1[c, d - 1] == 0)
                {
                    monster2.MoveLeft();
                    d--;
                }
    
                if (_monsterMove3 == 12 && _Level1[c, d - 1] == 0)
                {
                    monster3.MoveLeft();
                    d--;
                }
    
                if (_monsterMove4 == 17 && _Level1[c, d - 1] == 0)
                {
                    monster4.MoveLeft();
                    d--;
                }
            }
            private void StepDown()
            {
                if (_monsterMove1 == 3 && _Level1[c + 1, d] == 0)
                {
                    monster1.MoveDown();
                    c++;
                }
    
                if (_monsterMove2 == 8 && _Level1[c + 1, d] == 0)
                {
                    monster2.MoveDown();
                    c++;
                }
    
                if (_monsterMove3 == 13 && _Level1[c + 1, d] == 0)
                {
                    monster3.MoveDown();
                    c++;
                }
    
                if (_monsterMove4 == 18 && _Level1[c + 1, d] == 0)
                {
                    monster4.MoveDown();
                    c++;
                }
            }
            private void StepUp()
            {
                if (_monsterMove1 == 4 && _Level1[c - 1, d] == 0)
                {
                    monster1.MoveUp();
                    c--;
                }
    
                if (_monsterMove2 == 9 && _Level1[c - 1, d] == 0)
                {
                    monster2.MoveUp();
                    c--;
                }
    
                if (_monsterMove3 == 14 && _Level1[c - 1, d] == 0)
                {
                    monster3.MoveUp();
                    c--;
                }
    
                if (_monsterMove4 == 19 && _Level1[c - 1, d] == 0)
                {
                    monster4.MoveUp();
                    c--;
                }
            }
    
    Thanks in advance.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice