Example #1
0
        public static void DoMovement()
        {
            Vector2 nextPosition = snakePositions.First().Move(direction, snakePieceSize);
            Texture background   = DrawManager.GetTexture("Background");

            //allow the snake to go through the borders of the screen
            //this if statement checks if it is still within the screen border. if not, it needs to put the snake on the other side
            if (!new System.Drawing.Rectangle(
                    background.getTexturePosition(DrawManager.ImagePosition.TopLeft)
                    .ToPoint(), new Size(background.Image.Width - snakePieceSize, background.Image.Height - snakePieceSize))
                .IsInside(nextPosition + background.Position))
            {
                if (direction == Direction.West)
                {
                    nextPosition.X = (background.getTexturePosition(DrawManager.ImagePosition.TopLeft) - background.Position).X + background.Image.Width - snakePieceSize;
                }
                if (direction == Direction.South)
                {
                    nextPosition.Y = 0;
                }
                if (direction == Direction.East)
                {
                    nextPosition.X = (background.getTexturePosition(DrawManager.ImagePosition.TopLeft) - background.Position).X;
                }
                if (direction == Direction.North)
                {
                    nextPosition.Y = background.Image.Height - snakePieceSize;
                }
            }

            //try to move the snake or destroy it
            if (!WillBeDestroyed(nextPosition))
            {
                if (eggPosition.Distance(nextPosition) < 25)
                {
                    snakePositions.Add(snakePositions[snakePositions.Count() - 1]);
                    eggPosition = GetNewEggPosition();

                    DrawManager.activeTextures.RemoveAll(new Predicate <Texture>(a => a.DisplayName == "Egg"));
                    DrawManager.CreateTexture("SnakePiece", "Egg", background.Position + eggPosition, DrawManager.ImagePosition.TopLeft);
                }

                //ends at 1 so it skips the first snake piece to override later
                for (int i = snakePositions.Count() - 1; i > 0; i--)
                {
                    snakePositions[i] = snakePositions[i - 1];
                }
                snakePositions[0] = nextPosition;

                //remove all active snakes
                DrawManager.activeTextures.RemoveAll(new Predicate <Texture>(a => a.DisplayName == "Snake Piece"));

                //draw the new ones
                foreach (Vector2 pos in snakePositions)
                {
                    DrawManager.CreateTexture("SnakePiece", "Snake Piece",
                                              background.Position + pos, DrawManager.ImagePosition.TopLeft);
                }
            }
            else
            {
                Chat.Print("You lose! Your snake was " + snakePositions.Count() + " in length with a speed setting of " + MenuHandler.GetSliderValue(MenuHandler.SnakeMenu, "Game Speed"));
                DrawManager.MenuState = DrawManager.HUDState.MainMenu;
            }
        }