public void Tick() { if (GameOver) { return; } Tile tile = _board.GetTileInDirection(_snake.Head.Tile, _input.GetInputDirection()); if (tile == null || tile.HasSnake) // End if head hits bounds or body segment { GameOver = true; return; } List <SnakeSegment> segments = _snake.Segments; int segmentsCount = segments.Count; SnakeSegment newSegment = null; Tile prevTile = _snake.Head.Tile; if (tile.HasFood) { newSegment = new SnakeSegment(); var lastSegment = segments[segmentsCount - 1]; newSegment.Tile = lastSegment.Tile; tile.HasFood = false; } MoveSegmentToTile(_snake.Head, tile); for (int i = 1; i < segmentsCount; i++) // Move Snake Segments { SnakeSegment segment = segments[i]; tile = prevTile; prevTile = segment.Tile; MoveSegmentToTile(segment, tile); } if (newSegment != null) { newSegment.Tile.HasSnake = true; _snake.Segments.Add(newSegment); if (CheckGameOver()) { return; } _foodSpawner.SpawnFood(_board); } }
private void MoveSegmentToTile(SnakeSegment segment, Tile tile) { segment.Tile.HasSnake = false; segment.Tile = tile; tile.HasSnake = true; }