// Check if the snake ate food method. public bool Check() { // Assign the first element of the snake parts list // which is always the snake's head. SnakePart snakeHead = _snakeParts[0]; foreach (SnakeFood snakeFood in _snakeFoods) { if (snakeHead.CurrentPosition == snakeFood.CurrentPosition) { _eatenSnakeFood = snakeFood; return(true); } } return(false); }
// Check method. Return true if a snake's head collides with something, // false if not. public static bool Check(List <SnakePart> snakeParts, Canvas gameBoard) { SnakePart snakeHead = snakeParts[0]; int snakeSize = snakeHead.ElementSize; // Check if snake's head collides with one of the snake's parts. foreach (SnakePart snakePart in snakeParts.Skip(1)) { if (snakeHead.CurrentPosition == snakePart.CurrentPosition) { return(true); } } // Check if snake's head collides with one of the walls. if (snakeHead.CurrentPosition.X < 0 || snakeHead.CurrentPosition.X > gameBoard.Width - snakeSize || snakeHead.CurrentPosition.Y < 0 || snakeHead.CurrentPosition.Y > gameBoard.Height - snakeSize) { return(true); } return(false); }