private static void HandleCollision(Snake snake, ConsoleRenderer renderer) { List<GameFieldCoords> snakeElements = new List<GameFieldCoords>(); foreach (GameFieldCoords element in snake.GetPosition()) { snakeElements.Add(element); } foreach (GameFieldCoords element in snakeElements) { if (element.Row >= renderer.GameFieldSize.Row || element.Row < 0 || element.Col >= renderer.GameFieldSize.Col || element.Col < 0) { snake.IsDestroyed = true; } } for (int element = 0; element < snakeElements.Count - 1; element++) { if (snakeElements.Last().Row == snakeElements[element].Row && snakeElements.Last().Col == snakeElements[element].Col) { snake.IsDestroyed = true; } } }
public static void Main() { ConsoleRenderer renderer = new ConsoleRenderer(new GameFieldCoords(20, 40)); IUserController keyboard = new KeyboardController(); Engine gameEngine = new Engine(renderer, keyboard, 5, 1); keyboard.OnUpPressed += (sender, eventInfo) => { gameEngine.SnakeDirectionChange("up"); }; keyboard.OnRightPressed += (sender, eventInfo) => { gameEngine.SnakeDirectionChange("right"); }; keyboard.OnDownPressed += (sender, eventInfo) => { gameEngine.SnakeDirectionChange("down"); }; keyboard.OnLeftPressed += (sender, eventInfo) => { gameEngine.SnakeDirectionChange("left"); }; gameEngine.RunGame(); }
public Engine(ConsoleRenderer renderer, IUserController controller, int snakeLength, int foodAmount) { this.userName = this.usersManager.RequestUserName(renderer); this.renderer = renderer; this.controller = controller; this.snakeLength = snakeLength; this.foodAmount = foodAmount; this.foodList = new List<Food>(foodAmount); this.snake = new Snake(snakeLength); for (int counter = 0; counter < foodAmount; counter++) { this.AddFood(); } }
public static void HandleCollisions(Snake snake, List<Food> foodList, ConsoleRenderer renderer) { HandleEating(snake, foodList); HandleCollision(snake, renderer); }