Exemplo n.º 1
0
        private const short DefaultSnakeLength = 4; //Start size of the snake

        #endregion Fields

        #region Constructors

        public Snake()
        {
            //Setting snake's start direction and placing all the snake parts
            Direction = SnakeDirection.Down;
            Position.X = 10;
            Position.Y = 10;
            NewHeadPosition = new Position(Position.X, Position.Y);
            for (var i = 0; i < DefaultSnakeLength; i++)
            {
                SnakeElements.Add(new Position (Position.X, Position.Y));
            }
        }
Exemplo n.º 2
0
        //Move snake based on the chosen direction.
        public void MoveSnake()
        {
            NewHeadPosition = new Position(Head);

            switch (Direction)
            {
                case SnakeDirection.Up:
                    NewHeadPosition.Y--;
                    break;
                case SnakeDirection.Right:
                    NewHeadPosition.X++;
                    break;
                case SnakeDirection.Down:
                    NewHeadPosition.Y++;
                    break;
                case SnakeDirection.Left:
                    NewHeadPosition.X--;
                    break;
                default:
                    NewHeadPosition.Y++;
                    break;
            }

            //Has snake moved out of bounds? If so, game is over.
            if (NewHeadPosition.X < 0 || NewHeadPosition.X >= Global.BoardWidth ||
                NewHeadPosition.Y < 0 || NewHeadPosition.Y >= Global.BoardHeight)
            {
                Global.IsGameOver = true;
                return;
            }
            Draw ();
        }