Пример #1
0
 public void AddOneRectOfBody(SnakeDirection snakeDirection)
 {
     SnakeBodyParts.Add(new SnakeBodyPart()
     {
         BasicRectangle = RectangleGenerator.Generate(RectangleTypes.SnakeBody),
         Index          = ++MaxIndex,
         Location       = GetLocationForNewPart(snakeDirection)
     });
 }
Пример #2
0
        /// <summary>
        /// If user didn't hit anything than he and his body parts can move
        /// </summary>
        private void Check()
        {
            // Move the snake
            Snake.X += Snake.XSpeed;
            Snake.Y += Snake.YSpeed;

            // Moves the body parts of a snake.
            for (int i = Score - 1; i >= 0; i--)
            {
                if (i == 0)
                {
                    SnakeBodyParts[i] = move.MoveSquare(Snake, SnakeBodyParts[i]);
                }
                else
                {
                    SnakeBodyParts[i] = move.MoveSquare(SnakeBodyParts[i - 1], SnakeBodyParts[i]);
                }
            }

            // Player eats the apple if he gets close
            if (Snake.X > Apple.X - 10 &&
                Snake.X < Apple.X + 10 &&
                Snake.Y > Apple.Y - 10 &&
                Snake.Y < Apple.Y + 10)
            {
                // Creates new body part
                SnakeSquare square = new SnakeSquare();

                // If this is the first body part to be created then use snake's coordinates
                if (Score == 0)
                {
                    square = create.CreateSquare(Snake, square);

                    SnakeBodyParts.Add(square);
                }
                // else use last snake body part
                else
                {
                    // Search for last snake body part
                    var lastBodyPart = SnakeBodyParts.ElementAt(Score - 1);

                    square = create.CreateSquare(SnakeBodyParts[Score - 1], square);

                    SnakeBodyParts.Add(square);
                }
                Score++;

                // Create new apple
                Apple.SpawnApple();
            }
        }