Пример #1
0
        /// <summary>
        /// Changes PacMan's position to a new valid position
        /// </summary>
        /// <param name="dataGrid"></param>
        public void Move(Grid dataGrid)
        {
            //checks to see if pacman is aligned with a cell
            if ((location.X % IMAGE_SIZE == 0) && (location.Y % IMAGE_SIZE == 0))
            {
                Point location_cellIndex;
                location_cellIndex = new Point((location.X / IMAGE_SIZE), (location.Y / IMAGE_SIZE));

                Point attemptedLocation = location_cellIndex;
                //defines the location the user is attempting to move to
                switch (attemptedDirection)
                {
                    case Direction.Up:
                        attemptedLocation.Y--;
                        break;
                    case Direction.Left:
                        attemptedLocation.X--;
                        break;
                    case Direction.Right:
                        attemptedLocation.X++;
                        break;
                    case Direction.Down:
                        attemptedLocation.Y++;
                        break;
                    default:
                        break;
                }

                //checks to see if the next cell in the direction pacman wants to go in is valid
                if (dataGrid.CheckForWall(attemptedLocation) == false)
                {
                    direction = attemptedDirection;

                    imageToggle++;
                    imageToggle %= ANIMATION_FRAMES;
                }

                attemptedLocation = location_cellIndex;
                //attempted direction shift was unsuccessful, so instead pacman carries on the way he was going
                switch (direction)
                {
                    case Direction.Up:
                        attemptedLocation.Y--;
                        break;
                    case Direction.Left:
                        attemptedLocation.X--;
                        break;
                    case Direction.Right:
                        attemptedLocation.X++;
                        break;
                    case Direction.Down:
                        attemptedLocation.Y++;
                        break;
                    default:
                        break;
                }

                if (dataGrid.CheckForWall(attemptedLocation) == false)
                {
                    switch (direction)
                    {
                        case Direction.Up:
                            location.Y -= speed;
                            break;
                        case Direction.Left:
                            location.X -= speed;
                            break;
                        case Direction.Right:
                            location.X += speed;
                            break;
                        case Direction.Down:
                            location.Y += speed;
                            break;
                        default:
                            break;
                    }

                    imageToggle++;
                    imageToggle %= ANIMATION_FRAMES;
                }
            }

            else
            {
                switch (direction)
                {
                    case Direction.Up:
                        location.Y -= speed;
                        break;
                    case Direction.Left:
                        location.X -= speed;
                        break;
                    case Direction.Right:
                        location.X += speed;
                        break;
                    case Direction.Down:
                        location.Y += speed;
                        break;
                    default:
                        break;
                }

                imageToggle++;
                imageToggle %= ANIMATION_FRAMES;
            }
        }
Пример #2
0
        /// <summary>
        /// Randomises the direction the ghost moves in. 
        /// Specifies that a ghost should not move in the opposite direction of the one he's currently moving in.
        /// </summary>
        /// <param name="rand"></param>
        /// <param name="dataGrid"></param>
        public void RandomiseDirection(Random rand, Grid dataGrid)
        {
            int nPossibleDirections = Enum.GetNames(typeof(Direction)).Length;
            Direction tempDirection = (Direction)rand.Next(nPossibleDirections);

            Point attemptedLocation = GetNextLocation(tempDirection);

            //while the direction randomised is the opposite direction of the one he's currently moving in, randomise again.
            while (((int)tempDirection == (((int)direction + 2) % nPossibleDirections)) || (dataGrid.CheckForWall(attemptedLocation) == true))
            {
                tempDirection = (Direction)rand.Next(nPossibleDirections);
                attemptedLocation = GetNextLocation(tempDirection);
            }

            direction = tempDirection;
        }
        /// <summary>
        /// Randomises the direction the ghost moves in.
        /// Specifies that a ghost should not move in the opposite direction of the one he's currently moving in.
        /// </summary>
        /// <param name="rand"></param>
        /// <param name="dataGrid"></param>
        public void RandomiseDirection(Random rand, Grid dataGrid)
        {
            int       nPossibleDirections = Enum.GetNames(typeof(Direction)).Length;
            Direction tempDirection       = (Direction)rand.Next(nPossibleDirections);

            Point attemptedLocation = GetNextLocation(tempDirection);

            //while the direction randomised is the opposite direction of the one he's currently moving in, randomise again.
            while (((int)tempDirection == (((int)direction + 2) % nPossibleDirections)) || (dataGrid.CheckForWall(attemptedLocation) == true))
            {
                tempDirection     = (Direction)rand.Next(nPossibleDirections);
                attemptedLocation = GetNextLocation(tempDirection);
            }

            direction = tempDirection;
        }
        /// <summary>
        /// Changes PacMan's position to a new valid position
        /// </summary>
        /// <param name="dataGrid"></param>
        public void Move(Grid dataGrid)
        {
            //checks to see if pacman is aligned with a cell
            if ((location.X % IMAGE_SIZE == 0) && (location.Y % IMAGE_SIZE == 0))
            {
                Point location_cellIndex;
                location_cellIndex = new Point((location.X / IMAGE_SIZE), (location.Y / IMAGE_SIZE));

                Point attemptedLocation = location_cellIndex;
                //defines the location the user is attempting to move to
                switch (attemptedDirection)
                {
                case Direction.Up:
                    attemptedLocation.Y--;
                    break;

                case Direction.Left:
                    attemptedLocation.X--;
                    break;

                case Direction.Right:
                    attemptedLocation.X++;
                    break;

                case Direction.Down:
                    attemptedLocation.Y++;
                    break;

                default:
                    break;
                }

                //checks to see if the next cell in the direction pacman wants to go in is valid
                if (dataGrid.CheckForWall(attemptedLocation) == false)
                {
                    direction = attemptedDirection;

                    imageToggle++;
                    imageToggle %= ANIMATION_FRAMES;
                }

                attemptedLocation = location_cellIndex;
                //attempted direction shift was unsuccessful, so instead pacman carries on the way he was going
                switch (direction)
                {
                case Direction.Up:
                    attemptedLocation.Y--;
                    break;

                case Direction.Left:
                    attemptedLocation.X--;
                    break;

                case Direction.Right:
                    attemptedLocation.X++;
                    break;

                case Direction.Down:
                    attemptedLocation.Y++;
                    break;

                default:
                    break;
                }

                if (dataGrid.CheckForWall(attemptedLocation) == false)
                {
                    switch (direction)
                    {
                    case Direction.Up:
                        location.Y -= speed;
                        break;

                    case Direction.Left:
                        location.X -= speed;
                        break;

                    case Direction.Right:
                        location.X += speed;
                        break;

                    case Direction.Down:
                        location.Y += speed;
                        break;

                    default:
                        break;
                    }

                    imageToggle++;
                    imageToggle %= ANIMATION_FRAMES;
                }
            }

            else
            {
                switch (direction)
                {
                case Direction.Up:
                    location.Y -= speed;
                    break;

                case Direction.Left:
                    location.X -= speed;
                    break;

                case Direction.Right:
                    location.X += speed;
                    break;

                case Direction.Down:
                    location.Y += speed;
                    break;

                default:
                    break;
                }

                imageToggle++;
                imageToggle %= ANIMATION_FRAMES;
            }
        }