public static CollisionType Check(ISnakeObject snake, IFood food)
        {
            ISnakePart snakeHead = snake.SnakeParts[0];

            //Checking heads coords against food coords
            if ((snakeHead.Position.X == food.Position.X) && (snakeHead.Position.Y == food.Position.Y))
            {
                return(CollisionType.Food);
            }

            //Checking heads coords against game area boundaries
            if ((snakeHead.Position.Y < 0) || (snakeHead.Position.Y >= Constants.GAMEAREAHEIGHT) ||
                (snakeHead.Position.X < 0) || (snakeHead.Position.X >= Constants.GAMEAREAWIDTH))
            {
                return(CollisionType.Obstacle);
            }

            //Checking heads coords against all snake parts coords (excluding head)
            foreach (ISnakePart snakeBodyPart in snake.SnakeParts.GetRange(1, snake.SnakeParts.Count - 1))
            {
                if ((snakeHead.Position.X == snakeBodyPart.Position.X) && (snakeHead.Position.Y == snakeBodyPart.Position.Y))
                {
                    return(CollisionType.Obstacle);
                }
            }

            //If none of above is true there is no collision
            return(CollisionType.None);
        }
Exemple #2
0
        public void Grow()
        {
            double newTailX = SnakeParts[SnakeParts.Count - 1].Position.X;
            double newTailY = SnakeParts[SnakeParts.Count - 1].Position.Y;

            switch (SnakeDirection)
            {
            case SnakeDirection.Left:
                newTailX -= Constants.SNAKESQUARESIZE;
                break;

            case SnakeDirection.Right:
                newTailX += Constants.SNAKESQUARESIZE;
                break;

            case SnakeDirection.Up:
                newTailY -= Constants.SNAKESQUARESIZE;
                break;

            case SnakeDirection.Down:
                newTailY += Constants.SNAKESQUARESIZE;
                break;
            }

            //Adding new tail to the snake to simulate its growth
            ISnakePart newTail = Factory.CreateSnakePart();

            newTail.Position = new Point(newTailX, newTailY);
            SnakeParts.Add(newTail);
        }