Пример #1
0
        /// <summary>
        /// </summary>
        /// <param name="remove"></param>
        /// <returns></returns>
        public Position2D Tick(bool remove)
        {
            switch (_direction)
            {
            case Direction.Down:

                if (Position.X == SnakeConfig.RowCount - 1)
                {
                    CollisionVertical();
                }

                break;

            case Direction.Left:

                if (Position.Y == 0)
                {
                    CollisionHorizontal();
                }

                break;

            case Direction.Right:

                if (Position.Y == SnakeConfig.ColumnCount - 1)
                {
                    CollisionHorizontal();
                }

                break;

            default:

                if (Position.X == 0)
                {
                    CollisionVertical();
                }

                break;
            }

            _segment.Step(_direction);

            if (_body.GroupBy(segment => segment.Position).Any(grouping => grouping.Count() > 1))
            {
                return(null);
            }

            if (remove && _body.Count > 4)
            {
                _body.Dequeue().Reset();
            }

            _segment = new SnakeSegment(_snakeController.CellAt(Position), new Position2D(Position));
            _body.Enqueue(_segment);

            return(Position);
        }
Пример #2
0
 /// <summary>
 /// </summary>
 /// <param name="snakeController"></param>
 public Snake(SnakeController snakeController)
 {
     _snakeController = snakeController;
     _segment         = new SnakeSegment(_snakeController.CellAt(new Position2D(4, 5)), new Position2D(4, 5));
     _body.Enqueue(_segment);
 }