예제 #1
0
        /// <summary>
        /// Move the snake one block on the game area
        /// </summary>
        private void MoveSnake()
        {
            // Remove the last part of the snake, in preparation of the new part added below
            while (snakeParts.Count >= snakeLength)
            {
                GameArea.Children.Remove(snakeParts[0].UiElement);
                snakeParts.RemoveAt(0);
            }

            // Let all existing snake parts be snake body parts
            foreach (SnakePart snakePart in snakeParts)
            {
                (snakePart.UiElement as Rectangle).Fill = snakeBodyBrush;
                snakePart.IsHead = false;
            }

            // Determine which direction to expand the snake, based on the current direction
            SnakePart snakeHead = snakeParts[snakeParts.Count - 1];
            double    nextX     = snakeHead.Position.X;
            double    nextY     = snakeHead.Position.Y;

            switch (snakeDirection)
            {
            case SnakeDirection.Left:
                nextX -= SnakeSquareSize;
                break;

            case SnakeDirection.Right:
                nextX += SnakeSquareSize;
                break;

            case SnakeDirection.Up:
                nextY -= SnakeSquareSize;
                break;

            case SnakeDirection.Down:
                nextY += SnakeSquareSize;
                break;
            }

            // Add new head to snake
            snakeParts.Add(new SnakePart(new Point(nextX, nextY), true));

            // Draw snake part
            DrawSnake();
        }
예제 #2
0
        private void SetUpBody()
        {
            Body = new List <SnakePart>();
            SnakePart temp;

            for (int i = 0; i < Length; i++)
            {
                if (i == 0)
                {
                    this.Body.Add(new SnakePart(Start, Start));
                }
                if (i > 0)
                {
                    temp = new SnakePart(Body[i - 1].X, Body[i - 1].Y + 15);
                    Body.Add(temp);
                }
            }
        }
예제 #3
0
        public void AttachPart()
        {
            int start_x = Position_X - MovementDirection[0];
            int start_y = Position_Y - MovementDirection[1];

            SnakePart new_part = new SnakePart(start_x, start_y);

            new_part.Width  = 30;
            new_part.Height = 30;

            new_part.LastDirection     = MovementDirection;
            new_part.MovementDirection = MovementDirection;

            new_part.PrevPart = null;
            new_part.NextPart = this;

            PrevPart = new_part;
        }
예제 #4
0
        private void MoveSnake()
        {
            while (SnakeParts.Count >= SnakeLength)
            {
                GameArea.Children.Remove(SnakeParts[0].UiElement);
                SnakeParts.RemoveAt(0);
            }
            foreach (var SnakePart in SnakeParts)
            {
                (SnakePart.UiElement as Rectangle).Fill = SnakeBodyColor;
                SnakePart.IsHead = false;
            }

            SnakePart SnakeHead = SnakeParts[SnakeParts.Count - 1];
            double    nextX     = SnakeHead.Position.X;
            double    nextY     = SnakeHead.Position.Y;

            switch (snakeDirection)
            {
            case SnakeDirection.Left:
                nextX -= CELL_WIDTH;
                break;

            case SnakeDirection.Right:
                nextX += CELL_WIDTH;
                break;

            case SnakeDirection.Up:
                nextY -= CELL_HEIGHT;
                break;

            case SnakeDirection.Down:
                nextY += CELL_HEIGHT;
                break;
            }
            SnakeParts.Add(new SnakePart()
            {
                Position = new Point(nextX, nextY),
                IsHead   = true
            });

            DrawSnake();
            CheckCollisions();
        }
예제 #5
0
 private void HandleMovingSnakeOutsideMape(SnakePart SnakeHead)
 {
     if (SnakeHead.Position.X < 0)
     {
         SnakeHead.Position = new Point(GameArea.ActualWidth, SnakeHead.Position.Y);
     }
     else if (SnakeHead.Position.X + CELL_WIDTH > GameArea.Width)
     {
         SnakeHead.Position = new Point(0 - CELL_WIDTH, SnakeHead.Position.Y);
     }
     else if (SnakeHead.Position.Y < 0)
     {
         SnakeHead.Position = new Point(SnakeHead.Position.X, GameArea.ActualHeight);
     }
     else if (SnakeHead.Position.Y + CELL_HEIGHT > GameArea.Height)
     {
         SnakeHead.Position = new Point(SnakeHead.Position.X, 0 - CELL_HEIGHT);
     }
 }
예제 #6
0
파일: Snake.cs 프로젝트: lyyka/snake-game
        public void UpdateMatrix(SnakePart part)
        {
            // get matrix size
            int x = Game.Game_Matrix.GetLength(0);
            int y = Game.Game_Matrix.GetLength(1);

            // remove 1 from old position
            if (part.Position_X - part.MovementDirection[0] >= 0 && part.Position_X - part.MovementDirection[0] < x &&
                part.Position_Y - part.MovementDirection[1] >= 0 && part.Position_Y - part.MovementDirection[1] < y)
            {
                Game.Game_Matrix[part.Position_X - part.MovementDirection[0], part.Position_Y - part.MovementDirection[1]] = 0;
            }

            // update with new position
            if (part.Position_X >= 0 && part.Position_X < x &&
                part.Position_Y >= 0 && part.Position_Y < y)
            {
                Game.Game_Matrix[part.Position_X, part.Position_Y] = 1;
            }
        }
예제 #7
0
파일: Snake.cs 프로젝트: lyyka/snake-game
        public void CheckNewPositionForCollisions(SnakePart head)
        {
            // Get matrix size
            int x = Game.Game_Matrix.GetLength(0);
            int y = Game.Game_Matrix.GetLength(1);

            // If the position is inside the matrix
            if (head.Position_X >= 0 && head.Position_X < x &&
                head.Position_Y >= 0 && head.Position_Y < y)
            {
                // check if on that position is already some part
                if (Game.Game_Matrix[head.Position_X, head.Position_Y] == 1)
                {
                    // destroy the snake
                    Destroy();
                }

                // check if food is hit
                if (Game.Game_Matrix[head.Position_X, head.Position_Y] == 2)
                {
                    Game.IncreasePoints();

                    Game.Sounds["success"].controls.play();

                    Game.Game_Matrix[head.Position_X, head.Position_Y] = 0;
                    // attach one more part to the snake
                    SnakePart prev    = null;
                    SnakePart current = Head;
                    while (current != null)
                    {
                        prev    = current;
                        current = current.PrevPart;
                    }

                    prev.AttachPart();

                    // spawn another food
                    Game.SpawnFood();
                }
            }
        }
예제 #8
0
파일: Snake.cs 프로젝트: lyyka/snake-game
        // Construct new snake
        // Main form is a must becase of game matrix size
        // Second argument is the starting length of a snake
        public Snake(Main main, Game game, int snake_width, int snake_height, int length = 6)
        {
            Game = game;
            // set the global form to this main
            form = main;

            int max_x = Game.Game_Matrix.GetLength(0);
            int max_y = Game.Game_Matrix.GetLength(1);

            // Starting x and y coordinates of snake
            int start_x = max_x / 2 + length;
            int start_y = max_y / 2;

            // define new part of snake
            SnakePart new_part = null;
            // Tells if head is created so just the first part is set as head
            bool head_created = false;

            // until length is zero
            while (length != 0)
            {
                // Set the previous part so we can link it with next
                // At the begining this will be null
                SnakePart previous_part = new_part;

                // Define new part on x,y coordinates
                new_part        = new SnakePart(start_x, start_y);
                new_part.Width  = snake_width;
                new_part.Height = snake_height;
                // Default direction will be to the right
                new_part.LastDirection = new int[2] {
                    1, 0
                };
                new_part.MovementDirection = new int[2] {
                    1, 0
                };
                // If the head is not created, create it
                if (!head_created)
                {
                    // set that head is created so no part after this doesn't get set as head
                    head_created    = true;
                    new_part.IsHead = true;
                    Head            = new_part;
                }

                // next part of current part is previous
                new_part.NextPart = previous_part;

                // if there is a previous part (every part after Head has it)
                if (previous_part != null)
                {
                    // set it's previous part to this new part
                    previous_part.PrevPart = new_part;
                }

                // Write the position to the game matrx
                Game.Game_Matrix[start_x, start_y] = 1;

                // Decrease length
                length--;
                // Generate next part on the left side of the snake
                start_x--;
            }
        }