Help with pacman game

This is a discussion on Help with pacman game within the C# Programming forums, part of the General Programming Boards category; Me and a friend have to make pacman in csharp for school. And we're stuck. We got some kind of ...

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

    Help with pacman game

    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.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    First thing I'd do is create an array of those monsters so you can clean up your code. And I don't see why you'd need separate ranges for the moving.

    The next thing I'd do is create an enum to replace all those "magic numbers" you're using.

    Also, what do your c and d variables signify? Is that the position of the monster? That should live on the monster object instead so they can each track their own position.

    Once you do all that, then it's just a matter of:
    Code:
      foreach(Monster monster in monstersArray)
      {
        // Get a list of all the walkable directions
        List<Direction> walkableDirections = new List<Direction>();
    
        if(monster.Location.X > 0 && _Level1[monster.Location.Y, monster.Location.X - 1] == (int)TileType.Walkable)
          walkableDirections.Add(Direction.Left);
        if(monster.Location.X < _Level1.GetLength(1) - 1 && _Level1[monster.Location.Y, monster.Location.X + 1] == (int)TileType.Walkable)
          walkableDirections.Add(Direction.Right);
        if(monster.Location.Y > 0 && _Level1[monster.Location.Y - 1, monster.Location.X] == (int)TileType.Walkable)
          walkableDirections.Add(Direction.Up);
        if(monster.Location.Y < _Level1.GetLength(0) - 1 && _Level1[monster.Location.Y + 1, monster.Location.X] == (int)TileType.Walkable)
          walkableDirections.Add(Direction.Down);
    
        // Try to move the same direction as last time, otherwise pick a random walkable direction
        Direction direction;
        if(walkableDirections.Contains(monster.LastDirectionMoved))
          direction = monster.LastDirectionMoved;
        else
          direction = (Direction)monsterMove.Next(walkableDirections.Count);
    
        monster.Move(direction);
      }
    Something like that anyway. I didn't test the code, so YMMV.
    Last edited by itsme86; 03-09-2011 at 08:03 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    Am not sure how complex your final work is expected to be, but you could consider adding a pathfinding algorithm, like AStar to your monsters, this would mean they would then hunt the heroic pacman down, instead of moving randomly.
    Its not too difficult to do, but you might want to save it for future work and just stick with random moves.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  2. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  3. Try my game
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-15-2004, 11:58 AM
  4. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21