예제 #1
0
 public void Reset(Position position)
 {
     Direction = Direction.Up;
     Position  = position;
     SnakeBodyParts.Clear();
     Grow();
     Grow();
     Grow();
 }
예제 #2
0
 /// <summary>
 /// Changes the values to default
 /// </summary>
 private void ResetGame()
 {
     SnakeBodyParts.Clear();
     Snake = new Snake();
     Apple = new Apple();
     Score = 0;
     Timer.Start();
     IsGameOver = false;
 }
예제 #3
0
 public void AddOneRectOfBody(SnakeDirection snakeDirection)
 {
     SnakeBodyParts.Add(new SnakeBodyPart()
     {
         BasicRectangle = RectangleGenerator.Generate(RectangleTypes.SnakeBody),
         Index          = ++MaxIndex,
         Location       = GetLocationForNewPart(snakeDirection)
     });
 }
예제 #4
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();
            }
        }
예제 #5
0
 public void Grow()
 {
     SnakeBodyParts.Enqueue(
         new SnakeBody
     {
         Position = new Position
         {
             X = Position.X,
             Y = Position.Y
         }
     }
         );
     Head = SnakeBodyParts.Peek();
 }
예제 #6
0
 private void UpdatePosition()
 {
     SnakeBodyParts.Dequeue();
     Grow();
 }