Exemple #1
0
        private List<Vector2> SectionPositions; // Where the worm has been

        #endregion Fields

        #region Constructors

        public Worm(int TileSize)
        {
            HeadPosition = new Vector2(6, 6);
            SectionPositions = new List<Vector2>();
            Length = 4;
            CameFrom = Direction.WEST;
            GridSize = TileSize;
            Tail = new Rectangle(2 * GridSize, 0 * GridSize, GridSize, GridSize);
            Head = new Rectangle(3 * GridSize, 0 * GridSize, GridSize, GridSize);
            Corner = new Rectangle(1 * GridSize, 0 * GridSize, GridSize, GridSize);
            Mid1 = new Rectangle(0 * GridSize, 0 * GridSize, GridSize, GridSize);
            Mid2 = new Rectangle(1 * GridSize, 1 * GridSize, GridSize, GridSize);
            MainColor = new Color(110, 48, 45);
        }
Exemple #2
0
        public bool Update(GameTime gameTime, List<Direction> Dirs)
        {
            Vector2 TempPosition = new Vector2(0, 0);
            if (Dirs.Count() > 0)
            {
                foreach(Direction Dir in Dirs) {
                    if (Dir.Ordinal() != CameFrom.Ordinal())
                    {
                        TempPosition = Dir.Move(HeadPosition);
                        CameFrom = Dir.Opposite();
                        break;
                    }
                    else
                    {
                        TempPosition = CameFrom.Opposite().Move(HeadPosition);
                    }
                }
            }
            else
            {
                // no input, so go in the opposite direction we came from
                TempPosition = CameFrom.Opposite().Move(HeadPosition);
            }

            // check for collision with self
            if (SectionPositions.Exists(x => x.X == TempPosition.X && x.Y == TempPosition.Y))
            {
                // do something
                return false;
            }
            else {
                // add a section where the head is and move the head
                SectionPositions.Add(HeadPosition);
                HeadPosition = TempPosition;

                // delete sections if we exceeded our length
                while (SectionPositions.Count() > Length)
                {
                    SectionPositions.RemoveAt(0);
                }
                return true;
            }
        }