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); }
private void StartButton_Click(object sender, RoutedEventArgs e) { //UI preparation StartButton.IsEnabled = false; Score.Text = "Score: 0"; SnakeImage.Source = null; if (food != null && snake != null) { GameArea.Children.Remove(food.UiElement); RemoveSnakeFromScreen(); } //Setting back to defaults snake = Factory.CreateSnake(); food = Factory.CreateFood(snake); GameParameters.Score = 0; //Laying out new snake and food DrawSnake(); DrawFood(); //Setting sped back to initial and starting the game gameTimer.Interval = TimeSpan.FromMilliseconds(Constants.INITIALSNAKESPEED); gameTimer.IsEnabled = true; }
public Food(ISnakeObject snake) { Position = Randomizer.GetFoodPosition(snake); UiElement = new Ellipse() { Width = Constants.TILESIZE, Height = Constants.TILESIZE, Fill = Constants.FOODBRUSH }; }
public static Point GetFoodPosition(ISnakeObject snake) { //Getting maximum coordinates within game area and normalizing them against tile size int maxX = (int)(Constants.GAMEAREAWIDTH / Constants.TILESIZE); int maxY = (int)(Constants.GAMEAREAHEIGHT / Constants.TILESIZE); //Getting random tile coordinates for food positioning int foodX = random.Next(0, maxX) * Constants.TILESIZE; int foodY = random.Next(0, maxY) * Constants.TILESIZE; //Checking whether obtained food coordinates do not collide with all the snake parts coordinates, if so generating new coordinates foreach (ISnakePart snakePart in snake.SnakeParts) { if ((snakePart.Position.X == foodX) && (snakePart.Position.Y == foodY)) { return(GetFoodPosition(snake)); } } return(new Point(foodX, foodY)); }
public static IFood CreateFood(ISnakeObject snake) { return(new Food(snake)); }